1
0
mirror of https://github.com/golang/go synced 2024-11-24 21:00:09 -07:00

crypto/cipher: use AEAD.NonceSize to determine the size of nonce in the example

The existing example uses hard-coded constant to make nonce buffer.
Using AEAD.NonceSize makes it a more portable and appropriate example.

Fixes: #48372
This commit is contained in:
najeira 2021-09-14 13:36:09 +09:00
parent 4a4221e818
commit 03ccbb16df

View File

@ -29,14 +29,14 @@ func ExampleNewGCM_encrypt() {
panic(err.Error())
}
// Never use more than 2^32 random nonces with a given key because of the risk of a repeat.
nonce := make([]byte, 12)
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
aesgcm, err := cipher.NewGCM(block)
if err != nil {
panic(err.Error())
}
aesgcm, err := cipher.NewGCM(block)
if err != nil {
// Never use more than 2^32 random nonces with a given key because of the risk of a repeat.
nonce := make([]byte, aesgcm.NonceSize())
if _, err := io.ReadFull(rand.Reader, nonce); err != nil {
panic(err.Error())
}