diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-14 19:45:27 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-14 19:45:27 +0530 |
| commit | 4d49c0782aa8ef1c0df9547474ca8e0834a00787 (patch) | |
| tree | 261705b8e786dec67fa8efa1055369a75550dd9b /internal/crypt/rawDecrypt.go | |
| parent | 6c6c4a05267e93597bb009c7d6c36383b6af7bf7 (diff) | |
fix can't edit when no personal key
Diffstat (limited to 'internal/crypt/rawDecrypt.go')
| -rw-r--r-- | internal/crypt/rawDecrypt.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/crypt/rawDecrypt.go b/internal/crypt/rawDecrypt.go index b94ea60..d1f0193 100644 --- a/internal/crypt/rawDecrypt.go +++ b/internal/crypt/rawDecrypt.go @@ -119,3 +119,48 @@ func DecryptBytesSSH(inputBytes []byte, privKeyPath string) ([]byte, error) { return plaintext, nil } + +func DecryptBytesSSHWithKey(inputBytes []byte, noteKey []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) + } + + if headerLen < 0 || idx+1+headerLen > len(inputBytes) { + return nil, fmt.Errorf("decrypt: invalid header length %d", headerLen) + } + + headerBytes := inputBytes[idx+1 : idx+1+headerLen] + headers, err := GetFileHeaders(headerBytes) + if err != nil { + return nil, fmt.Errorf("decrypt: %w", err) + } + + if headers.Auth != SSH { + return nil, fmt.Errorf("decrypt: expected SSH auth, got %s", headers.Auth) + } + + aead, err := chacha20poly1305.New(noteKey) + 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 +} |