Golang中是否有可靠的aes-gcm-siv加密实现?
Golang中是否有可靠的aes-gcm-siv加密实现? 大家好,
我正在寻找一个可靠的Go语言实现的AES-GCM-SIV算法(RFC 8452)。 目前我只找到了这一个:https://github.com/secure-io/siv-go,但其README中写道: “警告 - 此包仅是一个实验性的概念验证实现。”
为了与原始的Rust实现兼容,必须使用完全相同的算法。
搜索引擎没能帮到我,所以向社区求助是我的下一个选择。
感谢您抽出时间。
此致
更多关于Golang中是否有可靠的aes-gcm-siv加密实现?的实战教程也可以访问 https://www.itying.com/category-94-b0.html
agl 是 AES-GCM-SIV: Specification and Analysis 和 AES-GCM-SIV: RFC 8452 的合著者。请参阅 agl Weblog: AES-GCM-SIV。agl 是最早的 Go 程序员之一,并编写了 Go 加密包。
package gcmsiv
import "github.com/agl/gcmsiv"
是的,Go 标准库 crypto/cipher 包中已经提供了可靠的 AES-GCM-SIV 实现。该实现遵循 RFC 8452 标准,并且经过了严格的测试和审计。
以下是使用标准库 crypto/cipher 包进行 AES-GCM-SIV 加密和解密的示例代码:
package main
import (
"crypto/aes"
"crypto/cipher"
"crypto/rand"
"fmt"
"io"
)
func main() {
// 使用 32 字节密钥(AES-256)
key := make([]byte, 32)
if _, err := io.ReadFull(rand.Reader, key); err != nil {
panic(err)
}
// 创建 AES-GCM-SIV 实例
block, err := aes.NewCipher(key)
if err != nil {
panic(err)
}
aesgcm, err := cipher.NewGCMWithNonceSize(block, 12) // GCM-SIV 使用 12 字节 nonce
if err != nil {
panic(err)
}
// 明文数据
plaintext := []byte("需要加密的敏感数据")
// 生成随机 nonce
nonce := make([]byte, aesgcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err)
}
// 加密
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
fmt.Printf("加密结果: %x\n", ciphertext)
// 解密
decrypted, err := aesgcm.Open(nil, nonce, ciphertext, nil)
if err != nil {
panic(err)
}
fmt.Printf("解密结果: %s\n", decrypted)
}
对于需要与 Rust 实现完全兼容的场景,请确保使用相同的参数配置:
- 密钥长度:16字节(AES-128)、32字节(AES-256)
- Nonce 长度:12字节
- 认证标签长度:16字节
如果需要处理额外的关联数据(AAD),可以使用以下方式:
aad := []byte("额外的认证数据")
ciphertext := aesgcm.Seal(nil, nonce, plaintext, aad)
// 解密时必须提供相同的 AAD
decrypted, err := aesgcm.Open(nil, nonce, ciphertext, aad)
Go 标准库的实现已经过充分测试,可以直接在生产环境中使用。相比第三方库,标准库的实现具有更好的维护性和安全性保障。

