Skip to content

Instantly share code, notes, and snippets.

@kkirsche
Last active November 12, 2024 02:47
Show Gist options
  • Select an option

  • Save kkirsche/e28da6754c39d5e7ea10 to your computer and use it in GitHub Desktop.

Select an option

Save kkirsche/e28da6754c39d5e7ea10 to your computer and use it in GitHub Desktop.

Revisions

  1. kkirsche revised this gist Jan 26, 2016. 1 changed file with 10 additions and 1 deletion.
    11 changes: 10 additions & 1 deletion aes256-gcm.go
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,12 @@
    package example_test

    import (
    "crypto/aes"
    "crypto/cipher"
    "hex"
    "io"
    )

    // AES-GCM should be used because the operation is an authenticated encryption
    // algorithm designed to provide both data authenticity (integrity) as well as
    // confidentiality.
    @@ -54,4 +63,4 @@ func ExampleNewGCMDecrypter() {
    }

    fmt.Printf("%s\n", string(plaintext))
    }
    }
  2. kkirsche revised this gist Jan 26, 2016. No changes.
  3. kkirsche created this gist Jan 26, 2016.
    57 changes: 57 additions & 0 deletions aes256-gcm.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,57 @@
    // AES-GCM should be used because the operation is an authenticated encryption
    // algorithm designed to provide both data authenticity (integrity) as well as
    // confidentiality.

    // Merged into Golang in https://go-review.googlesource.com/#/c/18803/

    func ExampleNewGCMEncrypter() {
    // The key argument should be the AES key, either 16 or 32 bytes
    // to select AES-128 or AES-256.
    key := []byte("AES256Key-32Characters1234567890")
    plaintext := []byte("exampleplaintext")

    block, err := aes.NewCipher(key)
    if err != nil {
    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 {
    panic(err.Error())
    }

    aesgcm, err := cipher.NewGCM(block)
    if err != nil {
    panic(err.Error())
    }

    ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil)
    fmt.Printf("%x\n", ciphertext)
    }

    func ExampleNewGCMDecrypter() {
    // The key argument should be the AES key, either 16 or 32 bytes
    // to select AES-128 or AES-256.
    key := []byte("AES256Key-32Characters1234567890")
    ciphertext, _ := hex.DecodeString("f90fbef747e7212ad7410d0eee2d965de7e890471695cddd2a5bc0ef5da1d04ad8147b62141ad6e4914aee8c512f64fba9037603d41de0d50b718bd665f019cdcd")

    nonce, _ := hex.DecodeString("bb8ef84243d2ee95a41c6c57")

    block, err := aes.NewCipher(key)
    if err != nil {
    panic(err.Error())
    }

    aesgcm, err := cipher.NewGCM(block)
    if err != nil {
    panic(err.Error())
    }

    plaintext, err := aesgcm.Open(nil, nonce, ciphertext, nil)
    if err != nil {
    panic(err.Error())
    }

    fmt.Printf("%s\n", string(plaintext))
    }