go - golang中crypto.subtle.exportKey的替代品是什么
问题描述
我正在尝试在golang中实现下面的JS代码,但是我在golang的crypto包中找不到任何导出密钥方法:
JS代码:
return Crypto.subtle.generateKey({
name: 'AES-GCM',
length: 256
}, !0, ['encrypt', 'decrypt']).then(function(t) {
const n = A.subtle.exportKey('raw', t);
return n
}).then(function(n) {
console.log(n)
}).catch(function(t) {
throw t
})
解决方案
的等价物crypto.subtle.exportKey('raw', key)
只是简单地使用该[]byte
值。
这已经是对称密钥的格式。例如:aes使用:func NewCipher(key []byte) (cipher.Block, error)
.
因此,为 AES256 生成密钥并打印其原始值将类似于以下内容(游乐场链接):
import (
"crypto/rand" // Careful: do not use "math/rand".
"fmt"
)
func main() {
key := make([]byte, 32) // 32 bytes for AES256.
_, err := rand.Read(key) // read from random source
if err != nil {
// handle error
}
fmt.Println(key)
}
推荐阅读
- django - 如何检索已提交表单的登录用户的最新模型对象?
- macos - 在 Mac OSX Catalina 上安装 Perl 模块 - 代码签名二进制库
- node.js - 使用 Node 和 Express 的交互式搜索栏
- python - InterfaceError:错误绑定参数 4 - 可能不支持 type.for 图像和 blob 图像不会显示在 QTLabel
- mqtt - 什么是最常见的、行业首选/使用的有效负载格式化方法?
- powershell - Powershell foreach 创建多个 csv 文件,我希望在创建后通过电子邮件发送这些文件。这如何实现?
- windows-subsystem-for-linux - Windows 10 wsl2 上的 Ubuntu - chown chmod 不适用于复制的文件
- java - 多线程 BlockingQueue - 在头部添加
- javascript - 如何从我的 websocket 一次获取多个值
- python - 我不明白导致程序冻结的问题是什么