Standard library / crypto/cipher

crypto/cipher.NewGCM()

Standard AES-GCM uses a 12-byte nonce and 16-byte tag. Keep nonces unique per key; random 96-bit nonces require a per-key usage limit (never exceed 2^32 encryptions; choose a lower operational limit). Public fixed key for an isolated test vector only; do not use it to protect real data. Expected stdout: '12 16\n'.

Formal Syntax & Structure

go syntaxgo
func NewGCM(cipher Block) (AEAD, error)

Parameters & Attributes

NameTypeDescription
cipherBlock128-bit block cipher, typically AES.

Return Value

(AEAD, error)

Authenticated encryption instance, or invalid configuration error.

Working Example

Example Snippetgo
package main
import (  "crypto/aes"  "crypto/cipher"  "fmt")
func main() {  // Public known-answer test key only. Never protect real data with this key.  b := must(aes.NewCipher(make([]byte, 16)))  g := must(cipher.NewGCM(b))  fmt.Println(g.NonceSize(), g.Overhead())}func must[T any](v T, e error) T {  if e != nil {    panic(e)  }  return v}func check(e error) {  if e != nil {    panic(e)  }}
Go Reference