From 03ccbb16df4ca9cbd4a014836aee0f54b2ff3002 Mon Sep 17 00:00:00 2001 From: najeira Date: Tue, 14 Sep 2021 13:36:09 +0900 Subject: [PATCH] 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 --- src/crypto/cipher/example_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/crypto/cipher/example_test.go b/src/crypto/cipher/example_test.go index 9c32d6a934f..86dbda9ec02 100644 --- a/src/crypto/cipher/example_test.go +++ b/src/crypto/cipher/example_test.go @@ -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()) }