Created
September 24, 2018 01:26
-
-
Save gitsrc/b5d885dece0246139b3ff6462bf247ce to your computer and use it in GitHub Desktop.
chacha20x
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
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