diff options
| author | ishaan <contact@ishaan.cc> | 2026-07-19 18:33:40 +0530 |
|---|---|---|
| committer | ishaan <contact@ishaan.cc> | 2026-07-19 18:33:40 +0530 |
| commit | e48a02646f60b6fdff8db5c4105896756584ac63 (patch) | |
| tree | 3371785e68bfe6b14065589ba7fd417d75742595 /internal/crypt/headers.go | |
| parent | d6449a232e6bd9eb46132c7403a05ac1a756b619 (diff) | |
reorganize
Diffstat (limited to 'internal/crypt/headers.go')
| -rw-r--r-- | internal/crypt/headers.go | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/internal/crypt/headers.go b/internal/crypt/headers.go index 8f9dac2..c47d39c 100644 --- a/internal/crypt/headers.go +++ b/internal/crypt/headers.go @@ -4,6 +4,7 @@ import ( "bytes" "encoding/gob" "fmt" + "strconv" ) type DataType string @@ -55,3 +56,35 @@ func FileHeadersBytes(headers FileHeaders) ([]byte, error) { return buf.Bytes(), nil } + +func ParseFileHeaders(data []byte) (FileHeaders, []byte, error) { + idx := -1 + for i, b := range data { + if b == ':' { + idx = i + break + } + } + if idx == -1 { + return FileHeaders{}, nil, fmt.Errorf("invalid file format: no header length prefix found") + } + + headerLen, err := strconv.Atoi(string(data[:idx])) + if err != nil { + return FileHeaders{}, nil, fmt.Errorf("failed to parse header length: %w", err) + } + + if headerLen <= 0 || idx+1+headerLen > len(data) { + return FileHeaders{}, nil, fmt.Errorf("invalid header length %d", headerLen) + } + + headerBytes := data[idx+1 : idx+1+headerLen] + headers, err := GetFileHeaders(headerBytes) + if err != nil { + return FileHeaders{}, nil, fmt.Errorf("failed to decode headers: %w", err) + } + + ciphertext := data[idx+1+headerLen:] + + return headers, ciphertext, nil +} |