Add high-level Decrypt() and Encrypt() functions for use by importing applications

This commit is contained in:
2025-05-03 19:34:26 -04:00
parent 18ea71e7e4
commit e4ae5b4a9d
4 changed files with 31 additions and 22 deletions
+2 -3
View File
@@ -13,13 +13,12 @@ const (
)
// EncryptCha encrypts data using ChaCha20-Poly1305.
func EncryptCha(data []byte, passphrase []byte) []byte {
func encryptCha(data []byte, passphrase []byte) []byte {
// generate a random salt
salt := make([]byte, saltSize)
io.ReadFull(rand.Reader, salt)
// derive key from passphrase using the salt
// TODO ensure the passphrase is consistent (store a hashed version to compare against)
key := deriveKey(passphrase, salt)
// create ChaCha20-Poly1305 cipher
@@ -42,7 +41,7 @@ func EncryptCha(data []byte, passphrase []byte) []byte {
}
// DecryptCha decrypts data using ChaCha20-Poly1305.
func DecryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
func decryptCha(encryptedData []byte, passphrase []byte) ([]byte, error) {
if len(encryptedData) < saltSize+nonceSizeCha {
return nil, errors.New("ChaCha20-Poly1305: Encrypted data is too short")
}