Last active
February 20, 2022 19:17
-
-
Save ushis/0367b05bbe82f13a321c1f27f2f0b40a to your computer and use it in GitHub Desktop.
golang curve25519 chacha20poly1305
This file contains 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 ( | |
"crypto/cipher" | |
"crypto/rand" | |
"encoding/base64" | |
"fmt" | |
"golang.org/x/crypto/chacha20poly1305" | |
"golang.org/x/crypto/curve25519" | |
) | |
func main() { | |
alicePrivKey := make([]byte, 32) | |
bobbiPrivKey := make([]byte, 32) | |
rand.Reader.Read(alicePrivKey) | |
rand.Reader.Read(bobbiPrivKey) | |
alicePubKey, err := curve25519.X25519(alicePrivKey, curve25519.Basepoint) | |
if err != nil { | |
panic(err) | |
} | |
bobbiPubKey, err := curve25519.X25519(bobbiPrivKey, curve25519.Basepoint) | |
if err != nil { | |
panic(err) | |
} | |
aliceSharedKey, err := curve25519.X25519(alicePrivKey, bobbiPubKey) | |
if err != nil { | |
panic(err) | |
} | |
bobbiSharedKey, err := curve25519.X25519(bobbiPrivKey, alicePubKey) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("alice prv:", base64.StdEncoding.EncodeToString(alicePrivKey)) | |
fmt.Println("alice pub:", base64.StdEncoding.EncodeToString(alicePubKey)) | |
fmt.Println("alice shr:", base64.StdEncoding.EncodeToString(aliceSharedKey)) | |
fmt.Println("bobbi prv:", base64.StdEncoding.EncodeToString(bobbiPrivKey)) | |
fmt.Println("bobbi pub:", base64.StdEncoding.EncodeToString(bobbiPubKey)) | |
fmt.Println("bobbi shr:", base64.StdEncoding.EncodeToString(bobbiSharedKey)) | |
aliceAEAD, err := chacha20poly1305.New(aliceSharedKey) | |
if err != nil { | |
panic(err) | |
} | |
bobbiAEAD, err := chacha20poly1305.New(bobbiSharedKey) | |
if err != nil { | |
panic(err) | |
} | |
ciphertext, err := encrypt(aliceAEAD, []byte("Hello Bobbi")) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("cpher txt:", base64.StdEncoding.EncodeToString(ciphertext)) | |
plaintext, err := decrypt(bobbiAEAD, ciphertext) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("plain txt:", string(plaintext)) | |
ciphertext, err = encrypt(bobbiAEAD, []byte("Hello Alice")) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("cpher txt:", base64.StdEncoding.EncodeToString(ciphertext)) | |
plaintext, err = decrypt(aliceAEAD, ciphertext) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("plain txt:", string(plaintext)) | |
} | |
func encrypt(aead cipher.AEAD, msg []byte) ([]byte, error) { | |
buf := make([]byte, aead.NonceSize(), aead.NonceSize()+aead.Overhead()+len(msg)) | |
if _, err := rand.Read(buf); err != nil { | |
return nil, err | |
} | |
return aead.Seal(buf, buf, msg, nil), nil | |
} | |
func decrypt(aead cipher.AEAD, msg []byte) ([]byte, error) { | |
return aead.Open(msg[:0], msg[:aead.NonceSize()], msg[aead.NonceSize():], nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment