为什么 golang 加密示例不使用随机 IV?

Why doesn't the golang crypto example use a random IV?

根据 CWE-329,非随机 IV 允许字典攻击的可能性。 但是,in the AES crypto example,golang 文档使用非随机 IV:

ciphertext := make([]byte, aes.BlockSize+len(plaintext))
iv := ciphertext[:aes.BlockSize]

这个实现安全吗?还是我应该使用随机函数来获取我的 IV?

它是安全的,因为 IV 是由加密安全伪随机数生成器 (CSPRNG) 填充的,默认情况下它是 /dev/urandom 并由 OS 提供。来自 ExampleNewCBCEncrypter 函数:

iv := ciphertext[:aes.BlockSize]
if _, err := io.ReadFull(rand.Reader, iv); err != nil {
    panic(err)
}