初始提交:OCI 面板后端(含 GenAI 网关一期)
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package crypto
|
||||
|
||||
import (
|
||||
"crypto/aes"
|
||||
"crypto/cipher"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// Cipher 用主密钥派生的 AES-256-GCM 做敏感字段加解密。
|
||||
type Cipher struct {
|
||||
aead cipher.AEAD
|
||||
}
|
||||
|
||||
// NewCipher 以 SHA-256 从主密钥字符串派生 32 字节密钥,
|
||||
// 允许 DATA_KEY 是任意长度的强随机字符串。
|
||||
func NewCipher(masterKey string) (*Cipher, error) {
|
||||
if masterKey == "" {
|
||||
return nil, fmt.Errorf("new cipher: empty master key")
|
||||
}
|
||||
sum := sha256.Sum256([]byte(masterKey))
|
||||
block, err := aes.NewCipher(sum[:])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new cipher: %w", err)
|
||||
}
|
||||
aead, err := cipher.NewGCM(block)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("new cipher: %w", err)
|
||||
}
|
||||
return &Cipher{aead: aead}, nil
|
||||
}
|
||||
|
||||
// EncryptString 加密明文,返回 base64(nonce | ciphertext)。
|
||||
func (c *Cipher) EncryptString(plaintext string) (string, error) {
|
||||
nonce := make([]byte, c.aead.NonceSize())
|
||||
if _, err := rand.Read(nonce); err != nil {
|
||||
return "", fmt.Errorf("encrypt: %w", err)
|
||||
}
|
||||
out := c.aead.Seal(nonce, nonce, []byte(plaintext), nil)
|
||||
return base64.StdEncoding.EncodeToString(out), nil
|
||||
}
|
||||
|
||||
// DecryptString 解密 EncryptString 的输出。
|
||||
func (c *Cipher) DecryptString(encoded string) (string, error) {
|
||||
raw, err := base64.StdEncoding.DecodeString(encoded)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt: %w", err)
|
||||
}
|
||||
ns := c.aead.NonceSize()
|
||||
if len(raw) < ns {
|
||||
return "", fmt.Errorf("decrypt: ciphertext too short")
|
||||
}
|
||||
plain, err := c.aead.Open(nil, raw[:ns], raw[ns:], nil)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("decrypt: %w", err)
|
||||
}
|
||||
return string(plain), nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package crypto
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestCipherRoundTrip(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
plaintext string
|
||||
}{
|
||||
{name: "普通文本", plaintext: "hello oci-portal"},
|
||||
{name: "空串", plaintext: ""},
|
||||
{name: "PEM 多行", plaintext: "-----BEGIN PRIVATE KEY-----\nabc\n-----END PRIVATE KEY-----"},
|
||||
}
|
||||
c, err := NewCipher("test-master-key")
|
||||
if err != nil {
|
||||
t.Fatalf("NewCipher: %v", err)
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
enc, err := c.EncryptString(tt.plaintext)
|
||||
if err != nil {
|
||||
t.Fatalf("EncryptString: %v", err)
|
||||
}
|
||||
got, err := c.DecryptString(enc)
|
||||
if err != nil {
|
||||
t.Fatalf("DecryptString: %v", err)
|
||||
}
|
||||
if got != tt.plaintext {
|
||||
t.Errorf("roundtrip: got %q, want %q", got, tt.plaintext)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCipherWrongKeyFails(t *testing.T) {
|
||||
c1, _ := NewCipher("key-one")
|
||||
c2, _ := NewCipher("key-two")
|
||||
enc, err := c1.EncryptString("secret")
|
||||
if err != nil {
|
||||
t.Fatalf("EncryptString: %v", err)
|
||||
}
|
||||
if _, err := c2.DecryptString(enc); err == nil {
|
||||
t.Error("decrypt with wrong key: got nil error, want failure")
|
||||
}
|
||||
}
|
||||
|
||||
func TestCipherRejectsGarbage(t *testing.T) {
|
||||
c, _ := NewCipher("key")
|
||||
tests := []struct {
|
||||
name string
|
||||
input string
|
||||
}{
|
||||
{name: "非 base64", input: "not-base64!!"},
|
||||
{name: "过短密文", input: "YWJj"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if _, err := c.DecryptString(tt.input); err == nil {
|
||||
t.Errorf("DecryptString(%q): got nil error, want failure", tt.input)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNewCipherEmptyKey(t *testing.T) {
|
||||
if _, err := NewCipher(""); err == nil {
|
||||
t.Error("NewCipher(\"\"): got nil error, want failure")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
// Package crypto 提供敏感字段的 AES-GCM 加解密。
|
||||
package crypto
|
||||
Reference in New Issue
Block a user