feat: initial release

This commit is contained in:
2026-01-16 02:39:40 +01:00
parent 15af32d382
commit 2af23db0ad
21 changed files with 1163 additions and 0 deletions
+70
View File
@@ -0,0 +1,70 @@
package crypto
import (
"crypto/aes"
"crypto/cipher"
"crypto/sha256"
"encoding/base64"
"encoding/binary"
"io"
)
const (
GCMChunkSize = 64 * 1024
NonceSize = 12
)
func DeriveKey(r io.Reader) ([]byte, error) {
h := sha256.New()
if _, err := io.Copy(h, r); err != nil {
return nil, err
}
return h.Sum(nil)[:16], nil
}
func GetID(key []byte, ext string) string {
h := sha256.New()
h.Write(key)
h.Write([]byte(ext))
return base64.RawURLEncoding.EncodeToString(h.Sum(nil)[:9])
}
type GCMStreamer struct {
AEAD cipher.AEAD
}
func NewGCMStreamer(key []byte) (*GCMStreamer, error) {
b, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
g, err := cipher.NewGCM(b)
if err != nil {
return nil, err
}
return &GCMStreamer{AEAD: g}, nil
}
func (g *GCMStreamer) EncryptStream(dst io.Writer, src io.Reader) error {
buf := make([]byte, GCMChunkSize)
var chunkIdx uint64 = 0
for {
n, err := io.ReadFull(src, buf)
if n > 0 {
nonce := make([]byte, NonceSize)
binary.BigEndian.PutUint64(nonce[4:], chunkIdx)
ciphertext := g.AEAD.Seal(nil, nonce, buf[:n], nil)
if _, werr := dst.Write(ciphertext); werr != nil {
return werr
}
chunkIdx++
}
if err == io.EOF || err == io.ErrUnexpectedEOF {
break
}
if err != nil {
return err
}
}
return nil
}
+92
View File
@@ -0,0 +1,92 @@
package crypto
import (
"crypto/cipher"
"encoding/binary"
"errors"
"io"
)
type Decryptor struct {
rs io.ReadSeeker
aead cipher.AEAD
size int64
offset int64
}
func NewDecryptor(rs io.ReadSeeker, aead cipher.AEAD, encryptedSize int64) *Decryptor {
overhead := int64(aead.Overhead())
fullBlocks := encryptedSize / (GCMChunkSize + overhead)
remainder := encryptedSize % (GCMChunkSize + overhead)
plainSize := (fullBlocks * GCMChunkSize)
if remainder > overhead {
plainSize += (remainder - overhead)
}
return &Decryptor{
rs: rs,
aead: aead,
size: plainSize,
}
}
func (d *Decryptor) Read(p []byte) (int, error) {
if d.offset >= d.size {
return 0, io.EOF
}
chunkIdx := d.offset / GCMChunkSize
overhang := d.offset % GCMChunkSize
overhead := int64(d.aead.Overhead())
actualChunkSize := int64(GCMChunkSize + overhead)
_, err := d.rs.Seek(chunkIdx*actualChunkSize, io.SeekStart)
if err != nil {
return 0, err
}
encrypted := make([]byte, actualChunkSize)
n, err := io.ReadFull(d.rs, encrypted)
if err != nil && err != io.ErrUnexpectedEOF {
return 0, err
}
nonce := make([]byte, NonceSize)
binary.BigEndian.PutUint64(nonce[4:], uint64(chunkIdx))
plaintext, err := d.aead.Open(nil, nonce, encrypted[:n], nil)
if err != nil {
return 0, err
}
if overhang >= int64(len(plaintext)) {
return 0, io.EOF
}
available := plaintext[overhang:]
nCopied := copy(p, available)
d.offset += int64(nCopied)
return nCopied, nil
}
func (d *Decryptor) Seek(offset int64, whence int) (int64, error) {
var abs int64
switch whence {
case io.SeekStart:
abs = offset
case io.SeekCurrent:
abs = d.offset + offset
case io.SeekEnd:
abs = d.size + offset
default:
return 0, errors.New("invalid whence")
}
if abs < 0 {
return 0, errors.New("negative bias")
}
d.offset = abs
return abs, nil
}