feat: add aispeech dialog APIs (#887)

* feat: add aispeech dialog APIs

* test: fix aispeech lint failures

* test: satisfy aispeech interfacer lint
This commit is contained in:
GargantuaX authored and GitHub committed 2026-07-20 14:10:56 +08:00
1 parent 0e34286d78
commit 2bc22dc4ae
18 files changed
+1562 -3

No files matched your search

+100
View File
@@ -0,0 +1,100 @@
package encryptor
import (
"bytes"
"crypto/aes"
"crypto/cipher"
"encoding/base64"
"errors"
"fmt"
)
var (
// ErrInvalidBlockSize block size 不合法.
ErrInvalidBlockSize = errors.New("invalid block size")
// ErrInvalidPKCS7Data PKCS7 数据不合法.
ErrInvalidPKCS7Data = errors.New("invalid PKCS7 data")
// ErrInvalidPKCS7Padding padding 不合法.
ErrInvalidPKCS7Padding = errors.New("invalid PKCS7 padding")
)
// Encrypt encrypts plaintext with AES-CBC and returns base64 ciphertext.
func Encrypt(aesKey string, plaintext []byte) (string, error) {
key, err := decodeAESKey(aesKey)
if err != nil {
return "", err
}
block, err := aes.NewCipher(key)
if err != nil {
return "", err
}
plaintext = pkcs7Pad(plaintext, block.BlockSize())
ciphertext := make([]byte, len(plaintext))
mode := cipher.NewCBCEncrypter(block, key[:aes.BlockSize])
mode.CryptBlocks(ciphertext, plaintext)
return base64.StdEncoding.EncodeToString(ciphertext), nil
}
// Decrypt decrypts base64 AES-CBC ciphertext.
func Decrypt(aesKey, ciphertext string) ([]byte, error) {
key, err := decodeAESKey(aesKey)
if err != nil {
return nil, err
}
data, err := base64.StdEncoding.DecodeString(ciphertext)
if err != nil {
return nil, err
}
if len(data) == 0 || len(data)%aes.BlockSize != 0 {
return nil, ErrInvalidPKCS7Data
}
block, err := aes.NewCipher(key)
if err != nil {
return nil, err
}
plaintext := make([]byte, len(data))
mode := cipher.NewCBCDecrypter(block, key[:aes.BlockSize])
mode.CryptBlocks(plaintext, data)
return pkcs7Unpad(plaintext, block.BlockSize())
}
func decodeAESKey(encodingAESKey string) ([]byte, error) {
key, err := base64.StdEncoding.DecodeString(encodingAESKey + "=")
if err != nil {
return nil, err
}
if len(key) != 32 {
return nil, fmt.Errorf("encodingAESKey invalid")
}
return key, nil
}
func pkcs7Pad(data []byte, blockSize int) []byte {
padding := blockSize - len(data)%blockSize
padText := bytes.Repeat([]byte{byte(padding)}, padding)
return append(data, padText...)
}
func pkcs7Unpad(data []byte, blockSize int) ([]byte, error) {
if blockSize <= 0 {
return nil, ErrInvalidBlockSize
}
if len(data) == 0 || len(data)%blockSize != 0 {
return nil, ErrInvalidPKCS7Data
}
padding := int(data[len(data)-1])
if padding == 0 || padding > blockSize || padding > len(data) {
return nil, ErrInvalidPKCS7Padding
}
for i := len(data) - padding; i < len(data); i++ {
if int(data[i]) != padding {
return nil, ErrInvalidPKCS7Padding
}
}
return data[:len(data)-padding], nil
}
+33
View File
@@ -0,0 +1,33 @@
package encryptor
import "testing"
func TestSign(t *testing.T) {
sign := Sign("YV78Pyj1VvqdNGpMJ1pHic0bIBOWMv", 1711001766, "abc", nil)
if sign != "fff8dae1356e7867ea98743439f0e9f8" {
t.Fatalf("bad sign: %s", sign)
}
}
func TestEncryptDecrypt(t *testing.T) {
aesKey := "q1Os1ZMe0nG28KUEx9lg3HjK7V5QyXvi212fzsgDqgz"
plainText := []byte(`{"query":"hello"}`)
cipherText, err := Encrypt(aesKey, plainText)
if err != nil {
t.Fatalf("Encrypt error: %v", err)
}
got, err := Decrypt(aesKey, cipherText)
if err != nil {
t.Fatalf("Decrypt error: %v", err)
}
if string(got) != string(plainText) {
t.Fatalf("bad plaintext: %s", got)
}
}
func TestDecryptInvalidKey(t *testing.T) {
if _, err := Encrypt("bad-key", []byte("hello")); err == nil {
t.Fatal("Encrypt should return error")
}
}
+20
View File
@@ -0,0 +1,20 @@
package encryptor
import (
"crypto/md5"
"encoding/hex"
"strconv"
)
// Sign calculates the aispeech request signature.
func Sign(token string, timestamp int64, nonce string, body []byte) string {
bodyMD5 := md5.Sum(body)
bodySign := hex.EncodeToString(bodyMD5[:])
h := md5.New()
h.Write([]byte(token))
h.Write([]byte(strconv.FormatInt(timestamp, 10)))
h.Write([]byte(nonce))
h.Write([]byte(bodySign))
return hex.EncodeToString(h.Sum(nil))
}