Skip to content

Instantly share code, notes, and snippets.

@gitsrc
Created September 24, 2018 01:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gitsrc/b5d885dece0246139b3ff6462bf247ce to your computer and use it in GitHub Desktop.
Save gitsrc/b5d885dece0246139b3ff6462bf247ce to your computer and use it in GitHub Desktop.
chacha20x
package main
import (
"fmt"
"golang.org/x/crypto/chacha20poly1305"
"log"
)
func main() {
key := []byte("D403842636B7D8E66FE5FB4E7BE6973A")
aead, err := chacha20poly1305.NewX(key)
if err != nil {
log.Fatalln("Failed to instantiate XChaCha20-Poly1305:", err)
}
for _, msg := range []string{
"test msg",
"The eagle has landed.",
"Gophers, gophers, gophers everywhere!",
} {
// Encryption.
nonce := key[5 : chacha20poly1305.NonceSizeX+5]
ciphertext := aead.Seal(nil, nonce, []byte(msg), nil)
// Decryption.
plaintext, err := aead.Open(nil, nonce, ciphertext, nil)
if err != nil {
log.Fatalln("Failed to decrypt or authenticate message:", err)
}
fmt.Printf("%s\n", plaintext)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment