50 lines
1.3 KiB
Go
50 lines
1.3 KiB
Go
package xkapi
|
|
|
|
import (
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"testing"
|
|
)
|
|
|
|
// 固定向量须与 xk-api scripts/hy_transit_crypto_vector.php 输出一致。
|
|
const (
|
|
vectorTransitToken = "test-transit-api-token-for-golden-vector"
|
|
vectorPlainJSON = `{"batch_id":12,"organID":"o1","organName":"测试机构"}`
|
|
vectorDerivedKeyHex = "0887a3ab67abbbeda0901b562799c248"
|
|
vectorEncrypted = "V5D6xp9Txjj6lUB42NMgySnLw2ZC+nEVdYkbiufEtKEyYbMpNhuxeiAhnpg0xmw+ktGYuwnf9oeS8HbwBjJBBw=="
|
|
)
|
|
|
|
func TestDeriveTransitAESKeyGolden(t *testing.T) {
|
|
got := hex.EncodeToString(DeriveTransitAESKey(vectorTransitToken))
|
|
if got != vectorDerivedKeyHex {
|
|
t.Fatalf("derived key %s != %s", got, vectorDerivedKeyHex)
|
|
}
|
|
}
|
|
|
|
func TestEncryptGoldenMatchesVector(t *testing.T) {
|
|
enc, err := EncryptBody(vectorPlainJSON, vectorTransitToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if enc != vectorEncrypted {
|
|
t.Fatalf("encrypted %s != %s", enc, vectorEncrypted)
|
|
}
|
|
}
|
|
|
|
func TestDecryptGoldenVector(t *testing.T) {
|
|
plain, err := DecryptBody(vectorEncrypted, vectorTransitToken)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if plain != vectorPlainJSON {
|
|
t.Fatalf("plain %q != %q", plain, vectorPlainJSON)
|
|
}
|
|
var decoded map[string]any
|
|
if err := json.Unmarshal([]byte(plain), &decoded); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if decoded["batch_id"] != float64(12) {
|
|
t.Fatalf("batch_id %v", decoded["batch_id"])
|
|
}
|
|
}
|