2022-01-28 11:51:08 +08:00
|
|
|
package smx509
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto"
|
|
|
|
"crypto/rand"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/emmansun/gmsm/sm2"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestEqual(t *testing.T) {
|
|
|
|
private, _ := sm2.GenerateKey(rand.Reader)
|
|
|
|
public := &private.PublicKey
|
|
|
|
|
|
|
|
if !public.Equal(public) {
|
|
|
|
t.Errorf("public key is not equal to itself: %q", public)
|
|
|
|
}
|
|
|
|
if !public.Equal(crypto.Signer(private).Public()) {
|
|
|
|
t.Errorf("private.Public() is not Equal to public: %q", public)
|
|
|
|
}
|
|
|
|
if !private.Equal(private) {
|
2023-12-15 17:47:32 +08:00
|
|
|
t.Errorf("private key is not equal to itself: %q", private.PrivateKey)
|
2022-01-28 11:51:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := MarshalPKCS8PrivateKey(private)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
decoded, err := ParsePKCS8PrivateKey(enc)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !public.Equal(decoded.(crypto.Signer).Public()) {
|
|
|
|
t.Errorf("public key is not equal to itself after decoding: %v", public)
|
|
|
|
}
|
|
|
|
if !private.Equal(decoded) {
|
|
|
|
t.Errorf("private key is not equal to itself after decoding: %v", private)
|
|
|
|
}
|
|
|
|
}
|