1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import ( "bytes" "crypto/rand" "fmt" "github.com/tjfoc/gmsm/sm2" "github.com/tjfoc/gmsm/sm3" "log" "testing" )
func TestSM3(t *testing.T) { data := "test" h := sm3.New() h.Write([]byte(data)) sum := h.Sum(nil) fmt.Printf("digest value is: %x\n",sum) }
func TestSM2(t *testing.T) { priv, err := sm2.GenerateKey(rand.Reader) if err != nil { log.Fatal(err) } msg := []byte("Tongji Fintech Research Institute") pub := &priv.PublicKey ciphertxt, err := pub.EncryptAsn1(msg,rand.Reader) if err != nil { log.Fatal(err) } fmt.Printf("加密结果:%x\n",ciphertxt) plaintxt,err := priv.DecryptAsn1(ciphertxt) if err != nil { log.Fatal(err) } if !bytes.Equal(msg,plaintxt){ log.Fatal("原文不匹配") }
sign,err := priv.Sign(rand.Reader, msg, nil) if err != nil { log.Fatal(err) } isok := pub.Verify(msg, sign) fmt.Printf("Verified: %v\n", isok) }
func TestX509(t *testing.T) { priv, err := sm2.GenerateKey(nil) if err != nil { t.Fatal(err) } privPem, err := WritePrivateKeyToPem(priv, nil) if err != nil { t.Fatal(err) } pubKey, _ := priv.Public().(*sm2.PublicKey) pubkeyPem, err := WritePublicKeyToPem(pubKey) privKey, err := ReadPrivateKeyFromPem(privPem, nil) if err != nil { t.Fatal(err) } pubKey, err = ReadPublicKeyFromPem(pubkeyPem) if err != nil { t.Fatal(err) } templateReq := CertificateRequest{ Subject: pkix.Name{ CommonName: "test.example.com", Organization: []string{"Test"}, }, SignatureAlgorithm: SM2WithSM3, } reqPem, err := CreateCertificateRequestToPem(&templateReq, privKey) if err != nil { t.Fatal(err) } req, err := ReadCertificateRequestFromPem(reqPem) if err != nil { t.Fatal(err) } err = req.CheckSignature() if err != nil { t.Fatalf("Request CheckSignature error:%v", err) } else { fmt.Printf("CheckSignature ok\n") }
testExtKeyUsage := []ExtKeyUsage{ExtKeyUsageClientAuth, ExtKeyUsageServerAuth} testUnknownExtKeyUsage := []asn1.ObjectIdentifier{[]int{1, 2, 3}, []int{2, 59, 1}} extraExtensionData := []byte("extra extension") commonName := "test.example.com" template := Certificate{ SerialNumber: big.NewInt(-1), Subject: pkix.Name{ CommonName: commonName, Organization: []string{"TEST"}, Country: []string{"China"}, ExtraNames: []pkix.AttributeTypeAndValue{ { Type: []int{2, 5, 4, 42}, Value: "Gopher", }, { Type: []int{2, 5, 4, 6}, Value: "NL", }, }, }, NotBefore: time.Now(), NotAfter: time.Date(2021, time.October, 10, 12, 1, 1, 1, time.UTC),
SignatureAlgorithm: SM2WithSM3,
SubjectKeyId: []byte{1, 2, 3, 4}, KeyUsage: KeyUsageCertSign,
ExtKeyUsage: testExtKeyUsage, UnknownExtKeyUsage: testUnknownExtKeyUsage,
BasicConstraintsValid: true, IsCA: true,
OCSPServer: []string{"http://ocsp.example.com"}, IssuingCertificateURL: []string{"http://crt.example.com/ca1.crt"},
DNSNames: []string{"test.example.com"}, EmailAddresses: []string{"gopher@golang.org"}, IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1).To4(), net.ParseIP("2001:4860:0:2001::68")},
PolicyIdentifiers: []asn1.ObjectIdentifier{[]int{1, 2, 3}}, PermittedDNSDomains: []string{".example.com", "example.com"},
CRLDistributionPoints: []string{"http://crl1.example.com/ca1.crl", "http://crl2.example.com/ca1.crl"},
ExtraExtensions: []pkix.Extension{ { Id: []int{1, 2, 3, 4}, Value: extraExtensionData, }, }, } pubKey, _ = priv.Public().(*sm2.PublicKey) certpem, err := CreateCertificateToPem(&template, &template, pubKey, privKey) if err != nil { t.Fatal("failed to create cert file") }
cert, err := ReadCertificateFromPem(certpem) if err != nil { t.Fatal("failed to read cert file") } err = cert.CheckSignature(cert.SignatureAlgorithm, cert.RawTBSCertificate, cert.Signature) if err != nil { t.Fatal(err) } else { fmt.Printf("CheckSignature ok\n") } }
|