mirror of
https://github.com/schollz/croc.git
synced 2025-10-11 05:11:06 +02:00
generate key once
This commit is contained in:
parent
420030998f
commit
5b0883e1fe
1 changed files with 4 additions and 12 deletions
|
@ -76,7 +76,7 @@ func Decrypt(encrypted []byte, key []byte) (plaintext []byte, err error) {
|
||||||
// NewArgon2 generates a new key based on a passphrase and salt
|
// NewArgon2 generates a new key based on a passphrase and salt
|
||||||
// using argon2
|
// using argon2
|
||||||
// https://pkg.go.dev/golang.org/x/crypto/argon2
|
// https://pkg.go.dev/golang.org/x/crypto/argon2
|
||||||
func NewArgon2(passphrase []byte, usersalt []byte) (key []byte, salt []byte, err error) {
|
func NewArgon2(passphrase []byte, usersalt []byte) (aead cipher.AEAD, salt []byte, err error) {
|
||||||
if len(passphrase) < 1 {
|
if len(passphrase) < 1 {
|
||||||
err = fmt.Errorf("need more than that for passphrase")
|
err = fmt.Errorf("need more than that for passphrase")
|
||||||
return
|
return
|
||||||
|
@ -91,17 +91,13 @@ func NewArgon2(passphrase []byte, usersalt []byte) (key []byte, salt []byte, err
|
||||||
} else {
|
} else {
|
||||||
salt = usersalt
|
salt = usersalt
|
||||||
}
|
}
|
||||||
key = argon2.IDKey(passphrase, salt, 1, 64*1024, 4, 32)
|
aead, err = chacha20poly1305.NewX(argon2.IDKey(passphrase, salt, 1, 64*1024, 4, 32))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// EncryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
|
// EncryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
|
||||||
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
|
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
|
||||||
func EncryptChaCha(plaintext []byte, key []byte) (encrypted []byte, err error) {
|
func EncryptChaCha(plaintext []byte, aead cipher.AEAD) (encrypted []byte, err error) {
|
||||||
aead, err := chacha20poly1305.NewX(key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead())
|
nonce := make([]byte, aead.NonceSize(), aead.NonceSize()+len(plaintext)+aead.Overhead())
|
||||||
if _, err := rand.Read(nonce); err != nil {
|
if _, err := rand.Read(nonce); err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
@ -114,11 +110,7 @@ func EncryptChaCha(plaintext []byte, key []byte) (encrypted []byte, err error) {
|
||||||
|
|
||||||
// DecryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
|
// DecryptChaCha will encrypt ChaCha20-Poly1305 using the pre-generated key
|
||||||
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
|
// https://pkg.go.dev/golang.org/x/crypto/chacha20poly1305
|
||||||
func DecryptChaCha(encryptedMsg []byte, key []byte) (encrypted []byte, err error) {
|
func DecryptChaCha(encryptedMsg []byte, aead cipher.AEAD) (encrypted []byte, err error) {
|
||||||
aead, err := chacha20poly1305.NewX(key)
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if len(encryptedMsg) < aead.NonceSize() {
|
if len(encryptedMsg) < aead.NonceSize() {
|
||||||
err = fmt.Errorf("ciphertext too short")
|
err = fmt.Errorf("ciphertext too short")
|
||||||
return
|
return
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue