github.com/emmansun/gmsm@v0.29.1/sm9/example_test.go (about) 1 package sm9_test 2 3 import ( 4 "crypto/rand" 5 "encoding/hex" 6 "fmt" 7 "math/big" 8 "os" 9 10 "github.com/emmansun/gmsm/sm9" 11 "golang.org/x/crypto/cryptobyte" 12 ) 13 14 func ExampleSignPrivateKey_Sign() { 15 // real user sign private key should be from secret storage, e.g. password protected pkcs8 file 16 kb, _ := hex.DecodeString("0130E78459D78545CB54C587E02CF480CE0B66340F319F348A1D5B1F2DC5F4") 17 var b cryptobyte.Builder 18 b.AddASN1BigInt(new(big.Int).SetBytes(kb)) 19 kb, _ = b.Bytes() 20 masterkey := new(sm9.SignMasterPrivateKey) 21 err := masterkey.UnmarshalASN1(kb) 22 if err != nil { 23 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 24 return 25 } 26 hid := byte(0x01) 27 uid := []byte("Alice") 28 userKey, err := masterkey.GenerateUserKey(uid, hid) 29 if err != nil { 30 fmt.Fprintf(os.Stderr, "Error from GenerateUserKey: %s\n", err) 31 return 32 } 33 34 // sm9 sign 35 hash := []byte("Chinese IBS standard") 36 sig, err := userKey.Sign(rand.Reader, hash, nil) 37 if err != nil { 38 fmt.Fprintf(os.Stderr, "Error from Sign: %s\n", err) 39 return 40 } 41 42 // Since sign is a randomized function, signature will be 43 // different each time. 44 fmt.Printf("%x\n", sig) 45 } 46 47 func ExampleVerifyASN1() { 48 // get master public key, can be from pem 49 masterPubKey := new(sm9.SignMasterPublicKey) 50 keyBytes, _ := hex.DecodeString("03818200049f64080b3084f733e48aff4b41b565011ce0711c5e392cfb0ab1b6791b94c40829dba116152d1f786ce843ed24a3b573414d2177386a92dd8f14d65696ea5e3269850938abea0112b57329f447e3a0cbad3e2fdb1a77f335e89e1408d0ef1c2541e00a53dda532da1a7ce027b7a46f741006e85f5cdff0730e75c05fb4e3216d") 51 err := masterPubKey.UnmarshalASN1(keyBytes) 52 if err != nil { 53 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 54 return 55 } 56 hid := byte(0x01) 57 uid := []byte("Alice") 58 hash := []byte("Chinese IBS standard") 59 sig, _ := hex.DecodeString("30660420b0d0c0bb1b57ea0d5b51cb5c96be850b8c2eef6b0fff5fcccb524b972574e6eb03420004901819575c9211c7b4e6e137794d23d0095608bcdad5c82dbff05777c5b49c763e4425acea2aaedf9e48d4784b4e4a5621cc3663fe0aae44dcbeac183fee9b0f") 60 ok := sm9.VerifyASN1(masterPubKey, uid, hid, hash, sig) 61 62 fmt.Printf("%v\n", ok) 63 // Output: true 64 } 65 66 func ExampleSignMasterPublicKey_Verify() { 67 // get master public key, can be from pem 68 masterPubKey := new(sm9.SignMasterPublicKey) 69 keyBytes, _ := hex.DecodeString("03818200049f64080b3084f733e48aff4b41b565011ce0711c5e392cfb0ab1b6791b94c40829dba116152d1f786ce843ed24a3b573414d2177386a92dd8f14d65696ea5e3269850938abea0112b57329f447e3a0cbad3e2fdb1a77f335e89e1408d0ef1c2541e00a53dda532da1a7ce027b7a46f741006e85f5cdff0730e75c05fb4e3216d") 70 err := masterPubKey.UnmarshalASN1(keyBytes) 71 if err != nil { 72 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 73 return 74 } 75 hid := byte(0x01) 76 uid := []byte("Alice") 77 hash := []byte("Chinese IBS standard") 78 sig, _ := hex.DecodeString("30660420b0d0c0bb1b57ea0d5b51cb5c96be850b8c2eef6b0fff5fcccb524b972574e6eb03420004901819575c9211c7b4e6e137794d23d0095608bcdad5c82dbff05777c5b49c763e4425acea2aaedf9e48d4784b4e4a5621cc3663fe0aae44dcbeac183fee9b0f") 79 ok := masterPubKey.Verify(uid, hid, hash, sig) 80 81 fmt.Printf("%v\n", ok) 82 // Output: true 83 } 84 85 func ExampleEncryptPrivateKey_UnwrapKey() { 86 // real user encrypt private key should be from secret storage, e.g. password protected pkcs8 file 87 kb, _ := hex.DecodeString("038182000494736acd2c8c8796cc4785e938301a139a059d3537b6414140b2d31eecf41683115bae85f5d8bc6c3dbd9e5342979acccf3c2f4f28420b1cb4f8c0b59a19b1587aa5e47570da7600cd760a0cf7beaf71c447f3844753fe74fa7ba92ca7d3b55f27538a62e7f7bfb51dce08704796d94c9d56734f119ea44732b50e31cdeb75c1") 88 userKey := new(sm9.EncryptPrivateKey) 89 err := userKey.UnmarshalASN1(kb) 90 if err != nil { 91 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 92 return 93 } 94 95 cipherDer, _ := hex.DecodeString("0342000447689629d1fa57e8def447f42b75e28518a1b692891528ca596f7bcbf581c7cf429ed01b114ce157ed4eadd0b2ded9a7e475e347f67b6affa3a6cf654573f978") 96 key, err := userKey.UnwrapKey([]byte("Bob"), cipherDer, 32) 97 if err != nil { 98 fmt.Fprintf(os.Stderr, "Error from UnwrapKey: %s\n", err) 99 return 100 } 101 fmt.Printf("%s\n", hex.EncodeToString(key)) 102 // Output: 270c42505bca90a8084064ea8af279364405a8195f30664082ead3d6991ed70f 103 } 104 105 func ExampleEncryptMasterPublicKey_WrapKey() { 106 // get master public key, can be from pem 107 masterPubKey := new(sm9.EncryptMasterPublicKey) 108 keyBytes, _ := hex.DecodeString("03420004787ed7b8a51f3ab84e0a66003f32da5c720b17eca7137d39abc66e3c80a892ff769de61791e5adc4b9ff85a31354900b202871279a8c49dc3f220f644c57a7b1") 109 err := masterPubKey.UnmarshalASN1(keyBytes) 110 if err != nil { 111 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 112 return 113 } 114 hid := byte(0x03) 115 uid := []byte("Bob") 116 key, cipherDer, err := masterPubKey.WrapKey(rand.Reader, uid, hid, 32) 117 if err != nil { 118 fmt.Fprintf(os.Stderr, "Error from WrapKeyASN1: %s\n", err) 119 return 120 } 121 122 // Since WrapKey is a randomized function, result will be 123 // different each time. 124 fmt.Printf("%s %s\n", hex.EncodeToString(key), hex.EncodeToString(cipherDer)) 125 } 126 127 func ExampleEncryptPrivateKey_Decrypt() { 128 // real user encrypt private key should be from secret storage, e.g. password protected pkcs8 file 129 kb, _ := hex.DecodeString("038182000494736acd2c8c8796cc4785e938301a139a059d3537b6414140b2d31eecf41683115bae85f5d8bc6c3dbd9e5342979acccf3c2f4f28420b1cb4f8c0b59a19b1587aa5e47570da7600cd760a0cf7beaf71c447f3844753fe74fa7ba92ca7d3b55f27538a62e7f7bfb51dce08704796d94c9d56734f119ea44732b50e31cdeb75c1") 130 userKey := new(sm9.EncryptPrivateKey) 131 err := userKey.UnmarshalASN1(kb) 132 if err != nil { 133 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 134 return 135 } 136 uid := []byte("Bob") 137 cipherDer, _ := hex.DecodeString("307f020100034200042cb3e90b0977211597652f26ee4abbe275ccb18dd7f431876ab5d40cc2fc563d9417791c75bc8909336a4e6562450836cc863f51002e31ecf0c4aae8d98641070420638ca5bfb35d25cff7cbd684f3ed75f2d919da86a921a2e3e2e2f4cbcf583f240414b7e776811774722a8720752fb1355ce45dc3d0df") 138 plaintext, err := userKey.DecryptASN1(uid, cipherDer) 139 if err != nil { 140 fmt.Fprintf(os.Stderr, "Error from Decrypt: %s\n", err) 141 return 142 } 143 fmt.Printf("%s\n", plaintext) 144 // Output: Chinese IBE standard 145 } 146 147 func ExampleEncryptMasterPublicKey_Encrypt() { 148 // get master public key, can be from pem 149 masterPubKey := new(sm9.EncryptMasterPublicKey) 150 keyBytes, _ := hex.DecodeString("03420004787ed7b8a51f3ab84e0a66003f32da5c720b17eca7137d39abc66e3c80a892ff769de61791e5adc4b9ff85a31354900b202871279a8c49dc3f220f644c57a7b1") 151 err := masterPubKey.UnmarshalASN1(keyBytes) 152 if err != nil { 153 fmt.Fprintf(os.Stderr, "Error from UnmarshalASN1: %s\n", err) 154 return 155 } 156 hid := byte(0x03) 157 uid := []byte("Bob") 158 159 ciphertext, err := masterPubKey.Encrypt(rand.Reader, uid, hid, []byte("Chinese IBE standard"), sm9.DefaultEncrypterOpts) 160 if err != nil { 161 fmt.Fprintf(os.Stderr, "Error from Encrypt: %s\n", err) 162 return 163 } 164 // Since Encrypt is a randomized function, result will be 165 // different each time. 166 fmt.Printf("%s\n", hex.EncodeToString(ciphertext)) 167 }