mirror of
https://github.com/chenyme/grok2api.git
synced 2026-12-06 08:02:14 +00:00
33 lines
619 B
Go
33 lines
619 B
Go
package security
|
|
|
|
import (
|
|
"crypto/rand"
|
|
"encoding/base64"
|
|
"testing"
|
|
)
|
|
|
|
func TestCipherRoundTrip(t *testing.T) {
|
|
key := make([]byte, 32)
|
|
if _, err := rand.Read(key); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
cipher, err := NewCipher(base64.StdEncoding.EncodeToString(key))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
encrypted, err := cipher.Encrypt("refresh-secret")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if encrypted == "refresh-secret" {
|
|
t.Fatal("密文不应等于明文")
|
|
}
|
|
plain, err := cipher.Decrypt(encrypted)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if plain != "refresh-secret" {
|
|
t.Fatalf("解密结果 = %q", plain)
|
|
}
|
|
}
|