Crypto and input management packages.
This commit is contained in:
parent
f0867a1063
commit
b8a4ad02d7
5 changed files with 163 additions and 0 deletions
35
pkg/crypto/encryption.go
Normal file
35
pkg/crypto/encryption.go
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
|
||||
"golang.org/x/crypto/chacha20poly1305"
|
||||
)
|
||||
|
||||
func Encrypt(key, msg []byte) []byte {
|
||||
aead, err := chacha20poly1305.NewX(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(msg)+aead.Overhead())
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return aead.Seal(nonce, nonce, msg, nil)
|
||||
}
|
||||
|
||||
func Decrypt(key, encryptedMsg []byte) []byte {
|
||||
aead, err := chacha20poly1305.NewX(key)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
nonce, ciphertext := encryptedMsg[:aead.NonceSize()], encryptedMsg[aead.NonceSize():]
|
||||
msg, err := aead.Open(nil, nonce, ciphertext, nil)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
return msg
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue