golang中crypto.subtle.exportKey的替代方案是什么



我正在尝试在 golang 中实现下面的 JS 代码,但我在 golang 的加密包中找不到任何导出密钥方法:

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)
}

对于其他导出格式和密钥,您可能需要查看 x509、pem 和每算法密钥生成函数。

最新更新