fix: align customer payment encryption and protect original values
Deploy olons-admin production / deploy (push) Successful in 9m19s

This commit is contained in:
richjohnrambo
2026-10-22 22:33:42 +08:00
parent 70ef3d9d04
commit 3d69d68a5d
8 changed files with 371 additions and 46 deletions
+20 -18
View File
@@ -54,30 +54,32 @@ func DecryptField(ctx context.Context, encoded string) (string, error) {
if encoded == "" {
return "", nil
}
key, err := fieldKey(ctx)
raw, err := decodeFieldPayload(encoded)
if err != nil {
return "", err
}
raw, err := base64.RawURLEncoding.DecodeString(encoded)
if err != nil {
return "", gerror.Wrap(err, "敏感字段密文格式错误")
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
gcm, err := cipher.NewGCM(block)
if err != nil {
return "", err
}
if len(raw) < gcm.NonceSize() {
if len(raw) < 12+16 {
return "", gerror.New("敏感字段密文不完整")
}
plain, err := gcm.Open(nil, raw[:gcm.NonceSize()], raw[gcm.NonceSize():], nil)
if err != nil {
return "", gerror.Wrap(err, "敏感字段解密失败")
keys := financeFieldReadKeys(ctx)
if key, keyErr := fieldKey(ctx); keyErr == nil {
keys = append(keys, key)
}
return string(plain), nil
for _, key := range keys {
block, keyErr := aes.NewCipher(key)
if keyErr != nil {
continue
}
gcm, keyErr := cipher.NewGCM(block)
if keyErr != nil {
continue
}
plain, decryptErr := gcm.Open(nil, raw[:gcm.NonceSize()], raw[gcm.NonceSize():], nil)
if decryptErr == nil {
return string(plain), nil
}
}
return "", gerror.New("敏感字段解密失败,请核对当前及历史密钥配置")
}
// HashField creates a deterministic, non-reversible lookup key with a secret pepper.