diff options
| author | ishaan <contact@ishaan.cc> | 2026-08-03 11:14:58 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-08-03 11:14:58 +0530 |
| commit | b65769aadfc79d22672d4bd512443f0dda8cf6ad (patch) | |
| tree | d3f216e0139e665478ee799297c00ed089ea612c /internal/crypt/raw_encrypt.go | |
| parent | cc4b5b69f1befe84551c0dbcb3df61eeea165cf7 (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.go | 25 |
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 } |