From 8907fdc36e31d87b031656336e9e6bd6bfe703c9 Mon Sep 17 00:00:00 2001 From: Randall Winkhart Date: Mon, 19 May 2025 20:10:03 -0400 Subject: [PATCH] Optimize salt+nonce appends --- wrappers/aes.go | 7 +------ wrappers/chacha.go | 7 +------ wrappers/highLevel.go | 5 +---- 3 files changed, 3 insertions(+), 16 deletions(-) diff --git a/wrappers/aes.go b/wrappers/aes.go index 96e8da6..c780fc3 100644 --- a/wrappers/aes.go +++ b/wrappers/aes.go @@ -27,12 +27,7 @@ func encryptAES(decBytes, key2, salt2 []byte) []byte { ciphertext := aesGCM.Seal(nil, nonce, decBytes, nil) // format: salt2 + nonce + ciphertext - result := make([]byte, 0, saltSize2+nonceSizeAES+len(ciphertext)) - result = append(result, salt2...) - result = append(result, nonce...) - result = append(result, ciphertext...) - - return result + return append(append(append(make([]byte, 0, saltSize2+nonceSizeAES+len(ciphertext)), salt2...), nonce...), ciphertext...) } // DecryptAES decrypts data using AES256-GCM. diff --git a/wrappers/chacha.go b/wrappers/chacha.go index bacd6ff..a75ce4d 100644 --- a/wrappers/chacha.go +++ b/wrappers/chacha.go @@ -22,12 +22,7 @@ func encryptCha(decBytes, key2, salt2 []byte) []byte { ciphertext := stream.Seal(nil, nonce, decBytes, nil) // format: salt2 + nonce + ciphertext - result := make([]byte, 0, saltSize2+nonceSizeCha+len(ciphertext)) - result = append(result, salt2...) - result = append(result, nonce...) - result = append(result, ciphertext...) - - return result + return append(append(append(make([]byte, 0, saltSize2+nonceSizeCha+len(ciphertext)), salt2...), nonce...), ciphertext...) } // DecryptCha decrypts data using ChaCha20-Poly1305. diff --git a/wrappers/highLevel.go b/wrappers/highLevel.go index b22a763..5e247b3 100644 --- a/wrappers/highLevel.go +++ b/wrappers/highLevel.go @@ -28,8 +28,5 @@ func Encrypt(decBytes []byte, passphrase []byte) []byte { decBytes = encryptAES(decBytes, key2AES, salt2AES) decBytes = encryptCha(decBytes, key2Cha, salt2Cha) // format: salt1 + decBytes per algorithm (salt2* + nonce + ciphertext) - encBytes := make([]byte, 0, saltSize1+len(decBytes)) - encBytes = append(encBytes, salt1...) - encBytes = append(encBytes, decBytes...) - return encBytes + return append(append(make([]byte, 0, saltSize1+len(decBytes)), salt1...), decBytes...) }