Skip to content

Commit 2cb9e7f

Browse files
najeiragopherbot
authored andcommitted
crypto/cipher: use AEAD.NonceSize to make 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 Change-Id: I7c7a38ed48aff46ca11ef4f5654c778eac13dde6 GitHub-Last-Rev: 03ccbb1 GitHub-Pull-Request: #48373 Reviewed-on: https://go-review.googlesource.com/c/go/+/349603 LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Sean Liao <[email protected]> Reviewed-by: Dmitri Shuralyov <[email protected]> Auto-Submit: Sean Liao <[email protected]> Reviewed-by: Roland Shoemaker <[email protected]>
1 parent 5413abc commit 2cb9e7f

File tree

1 file changed

+5
-5
lines changed

1 file changed

+5
-5
lines changed

src/crypto/cipher/example_test.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@ func ExampleNewGCM_encrypt() {
2929
panic(err.Error())
3030
}
3131

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

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

0 commit comments

Comments
 (0)