Created
May 10, 2021 04:42
-
-
Save boseji/f76f6b0ebe05261044cd227485c06c2f to your computer and use it in GitHub Desktop.
AES GCM Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Abhijit Bose. All rights reserved. | |
// Use of this source code is governed by a Apache 2.0 license | |
package main | |
import ( | |
"crypto/aes" | |
"crypto/cipher" | |
"encoding/hex" | |
"fmt" | |
) | |
func main() { | |
// Load your secret key from a safe place and reuse it across multiple | |
// Seal/Open calls. (Obviously don't use this example key for anything | |
// real.) If you want to convert a passphrase to a key, use a suitable | |
// package like bcrypt or scrypt. | |
// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256). | |
key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574") | |
ciphertext, _ := hex.DecodeString("022100c774487456c404b3bb9b3938c7234b3837746a27fbd84a91df5d3ba62e") | |
nonce, _ := hex.DecodeString("eed01d5099dc428d44bb18f1") | |
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", plaintext) | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Copyright 2021 Abhijit Bose. All rights reserved. | |
// Use of this source code is governed by a Apache 2.0 license | |
package main | |
import ( | |
"crypto/aes" | |
"crypto/cipher" | |
"crypto/rand" | |
"encoding/hex" | |
"fmt" | |
"io" | |
) | |
func main() { | |
// Load your secret key from a safe place and reuse it across multiple | |
// Seal/Open calls. (Obviously don't use this example key for anything | |
// real.) If you want to convert a passphrase to a key, use a suitable | |
// package like bcrypt or scrypt. | |
// When decoded the key should be 16 bytes (AES-128) or 32 (AES-256). | |
key, _ := hex.DecodeString("6368616e676520746869732070617373776f726420746f206120736563726574") | |
plaintext := []byte("exampleplaintext") | |
block, err := aes.NewCipher(key) | |
if err != nil { | |
panic(err.Error()) | |
} | |
aesgcm, err := cipher.NewGCM(block) | |
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, aesgcm.NonceSize()) | |
if _, err := io.ReadFull(rand.Reader, nonce); err != nil { | |
panic(err.Error()) | |
} | |
fmt.Printf("nonce : %x\n", nonce) | |
ciphertext := aesgcm.Seal(nil, nonce, plaintext, nil) | |
fmt.Printf("cipher: %x\n", ciphertext) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment