| 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) | |
| } | |
| } | |