aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/rawDecrypt.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/crypt/rawDecrypt.go')
-rw-r--r--internal/crypt/rawDecrypt.go48
1 files changed, 48 insertions, 0 deletions
diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go
new file mode 100644
index 0000000..30a202f
--- /dev/null
+++ b/internal/crypt/rawDecrypt.go
@@ -0,0 +1,48 @@
+package crypt
+
+import (
+ "fmt"
+ "strconv"
+
+ "gitlab.com/whiteboardguy/eno/internal/utils"
+ "golang.org/x/crypto/argon2"
+ "golang.org/x/crypto/chacha20poly1305"
+)
+
+func DecryptRawBytes(inputBytes []byte, password []byte) ([]byte, error) {
+ idx := -1
+ for i, b := range inputBytes {
+ if b == ':' {
+ idx = i
+ break
+ }
+ }
+ if idx == -1 {
+ return nil, fmt.Errorf("decrypt: invalid file format: no header length prefix found")
+ }
+
+ headerLen, err := strconv.Atoi(string(inputBytes[:idx]))
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to parse header length: %w", err)
+ }
+
+ headerBytes := inputBytes[idx+1 : idx+1+headerLen]
+ headers, err := utils.GetFileHeaders(headerBytes)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ key := argon2.IDKey(password, headers.Salt[:], 1, 2*1024*1024, 2, 32)
+ aead, err := chacha20poly1305.New(key)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: failed to create cipher: %w", err)
+ }
+
+ ciphertext := inputBytes[idx+1+headerLen:]
+ plaintext, err := aead.Open(nil, headers.Nonce[:], ciphertext, nil)
+ if err != nil {
+ return nil, fmt.Errorf("decrypt: %w", err)
+ }
+
+ return plaintext, nil
+}