mirror of
https://github.com/ghostersk/gowebmail.git
synced 2026-04-17 08:36:01 +01:00
135 lines
3.1 KiB
Go
135 lines
3.1 KiB
Go
// Package crypto provides AES-256-GCM encryption for sensitive data at rest.
|
|
package crypto
|
|
|
|
import (
|
|
"crypto/aes"
|
|
"crypto/cipher"
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"errors"
|
|
"io"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
)
|
|
|
|
const (
|
|
// BcryptCost is the bcrypt work factor for password hashing.
|
|
BcryptCost = 12
|
|
)
|
|
|
|
// Encryptor wraps AES-256-GCM for field-level encryption.
|
|
type Encryptor struct {
|
|
key []byte // 32 bytes
|
|
}
|
|
|
|
// New creates an Encryptor from a 32-byte key.
|
|
func New(key []byte) (*Encryptor, error) {
|
|
if len(key) != 32 {
|
|
return nil, errors.New("encryption key must be exactly 32 bytes")
|
|
}
|
|
k := make([]byte, 32)
|
|
copy(k, key)
|
|
return &Encryptor{key: k}, nil
|
|
}
|
|
|
|
// Encrypt encrypts plaintext using AES-256-GCM and returns a base64-encoded ciphertext.
|
|
// Format: base64(nonce || ciphertext || tag)
|
|
func (e *Encryptor) Encrypt(plaintext string) (string, error) {
|
|
if plaintext == "" {
|
|
return "", nil
|
|
}
|
|
|
|
block, err := aes.NewCipher(e.key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
nonce := make([]byte, gcm.NonceSize())
|
|
if _, err = io.ReadFull(rand.Reader, nonce); err != nil {
|
|
return "", err
|
|
}
|
|
|
|
// Seal appends ciphertext+tag to nonce
|
|
ciphertext := gcm.Seal(nonce, nonce, []byte(plaintext), nil)
|
|
return base64.StdEncoding.EncodeToString(ciphertext), nil
|
|
}
|
|
|
|
// Decrypt decrypts a base64-encoded ciphertext produced by Encrypt.
|
|
func (e *Encryptor) Decrypt(encoded string) (string, error) {
|
|
if encoded == "" {
|
|
return "", nil
|
|
}
|
|
|
|
data, err := base64.StdEncoding.DecodeString(encoded)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
block, err := aes.NewCipher(e.key)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
gcm, err := cipher.NewGCM(block)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
|
|
nonceSize := gcm.NonceSize()
|
|
if len(data) < nonceSize {
|
|
return "", errors.New("ciphertext too short")
|
|
}
|
|
|
|
nonce, ciphertext := data[:nonceSize], data[nonceSize:]
|
|
plaintext, err := gcm.Open(nil, nonce, ciphertext, nil)
|
|
if err != nil {
|
|
return "", errors.New("decryption failed: data may be corrupted or key is wrong")
|
|
}
|
|
|
|
return string(plaintext), nil
|
|
}
|
|
|
|
// EncryptBytes encrypts raw bytes and returns base64 encoding.
|
|
func (e *Encryptor) EncryptBytes(data []byte) (string, error) {
|
|
return e.Encrypt(string(data))
|
|
}
|
|
|
|
// DecryptBytes decrypts to raw bytes.
|
|
func (e *Encryptor) DecryptBytes(encoded string) ([]byte, error) {
|
|
s, err := e.Decrypt(encoded)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return []byte(s), nil
|
|
}
|
|
|
|
// ---- Password hashing ----
|
|
|
|
// HashPassword hashes a password using bcrypt.
|
|
func HashPassword(password string) (string, error) {
|
|
hash, err := bcrypt.GenerateFromPassword([]byte(password), BcryptCost)
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
return string(hash), nil
|
|
}
|
|
|
|
// CheckPassword compares a plaintext password against a bcrypt hash.
|
|
func CheckPassword(password, hash string) error {
|
|
return bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
|
|
}
|
|
|
|
// GenerateToken generates a cryptographically secure random token of n bytes, base64-encoded.
|
|
func GenerateToken(n int) (string, error) {
|
|
b := make([]byte, n)
|
|
if _, err := rand.Read(b); err != nil {
|
|
return "", err
|
|
}
|
|
return base64.URLEncoding.EncodeToString(b), nil
|
|
}
|