aboutsummaryrefslogtreecommitdiff
path: root/internal/crypt/raw_encrypt.go
diff options
context:
space:
mode:
authorishaan <contact@ishaan.cc>2026-08-03 11:14:58 +0530
committerishaan <contact@ishaan.cc>2026-08-03 11:14:58 +0530
commitb65769aadfc79d22672d4bd512443f0dda8cf6ad (patch)
treed3f216e0139e665478ee799297c00ed089ea612c /internal/crypt/raw_encrypt.go
parentcc4b5b69f1befe84551c0dbcb3df61eeea165cf7 (diff)
feat: add rekeying for notes with passphrases or SSH
Diffstat (limited to 'internal/crypt/raw_encrypt.go')
-rw-r--r--internal/crypt/raw_encrypt.go25
1 files changed, 14 insertions, 11 deletions
diff --git a/internal/crypt/raw_encrypt.go b/internal/crypt/raw_encrypt.go
index 03f742f..620a345 100644
--- a/internal/crypt/raw_encrypt.go
+++ b/internal/crypt/raw_encrypt.go
@@ -32,16 +32,25 @@ func EncryptBytesPass(inputBytes []byte, password []byte) ([]byte, error) {
encrypted := aead.Seal(nil, headers.Nonce[:], inputBytes, nil)
- headerBytes, err := FileHeadersBytes(headers)
+ packed, err := packFile(headers, encrypted)
if err != nil {
return nil, fmt.Errorf("encrypt: %w", err)
}
+ return packed, nil
+}
+
+func packFile(headers FileHeaders, ciphertext []byte) ([]byte, error) {
+ headerBytes, err := FileHeadersBytes(headers)
+ if err != nil {
+ return nil, fmt.Errorf("failed to encode file headers: %w", err)
+ }
+
lenLen := len(strconv.Itoa(len(headerBytes)))
- target := make([]byte, 0, lenLen+1+len(headerBytes)+len(encrypted))
+ target := make([]byte, 0, lenLen+1+len(headerBytes)+len(ciphertext))
target = fmt.Appendf(target, "%d:", len(headerBytes))
target = append(target, headerBytes...)
- target = append(target, encrypted...)
+ target = append(target, ciphertext...)
return target, nil
}
@@ -85,17 +94,11 @@ func EncryptBytesSSH(inputBytes []byte, pubKeys [][]byte) ([]byte, []byte, error
encrypted := aead.Seal(nil, headers.Nonce[:], inputBytes, nil)
- headerBytes, err := FileHeadersBytes(headers)
+ packed, err := packFile(headers, encrypted)
if err != nil {
clear(noteKey)
return nil, nil, fmt.Errorf("encrypt: %w", err)
}
- lenLen := len(strconv.Itoa(len(headerBytes)))
- target := make([]byte, 0, lenLen+1+len(headerBytes)+len(encrypted))
- target = fmt.Appendf(target, "%d:", len(headerBytes))
- target = append(target, headerBytes...)
- target = append(target, encrypted...)
-
- return target, noteKey, nil
+ return packed, noteKey, nil
}