|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | +) |
| 6 | + |
| 7 | +func main() { |
| 8 | + fmt.Printf("GmSSL-Go Version: %s\n", gmssl.GmSSLGoVersion) |
| 9 | + fmt.Printf("GmSSL Library Version: %s\n", gmssl.GetGmSSLLibraryVersion()) |
| 10 | + |
| 11 | + key, _ := gmssl.RandBytes(16) |
| 12 | + fmt.Printf("RandBytes(32) : %x\n", key) |
| 13 | + |
| 14 | + fmt.Printf("Sm3DigestSize : %d\n", gmssl.Sm3DigestSize) |
| 15 | + |
| 16 | + sm3 := gmssl.NewSm3() |
| 17 | + sm3.Update([]byte("abc")) |
| 18 | + dgst := sm3.Digest() |
| 19 | + fmt.Printf("Sm3('abc') : %x\n", dgst) |
| 20 | + |
| 21 | + fmt.Printf("Sm3HmacMinKeySize = %d\n", gmssl.Sm3HmacMinKeySize) |
| 22 | + fmt.Printf("Sm3HmacMaxKeySize = %d\n", gmssl.Sm3HmacMaxKeySize) |
| 23 | + fmt.Printf("Sm3HmacSize = %d\n", gmssl.Sm3HmacSize) |
| 24 | + |
| 25 | + hmac, _ := gmssl.NewSm3Hmac(key) |
| 26 | + hmac.Update([]byte("abc")) |
| 27 | + mac := hmac.GenerateMac() |
| 28 | + fmt.Printf("Sm3Hmac('abc') : %x\n", mac) |
| 29 | + |
| 30 | + fmt.Printf("Sm3Pbkdf2MinIter = %d\n", gmssl.Sm3Pbkdf2MinIter) |
| 31 | + fmt.Printf("Sm3Pbkdf2MaxIter = %d\n", gmssl.Sm3Pbkdf2MaxIter) |
| 32 | + fmt.Printf("Sm3Pbkdf2MaxSaltSize = %d\n", gmssl.Sm3Pbkdf2MaxSaltSize) |
| 33 | + fmt.Printf("Sm3Pbkdf2DefaultSaltSize = %d\n", gmssl.Sm3Pbkdf2DefaultSaltSize) |
| 34 | + fmt.Printf("Sm3Pbkdf2MaxKeySize = %d\n", gmssl.Sm3Pbkdf2MaxKeySize) |
| 35 | + |
| 36 | + salt, _ := gmssl.RandBytes(gmssl.Sm3Pbkdf2DefaultSaltSize) |
| 37 | + kdf_key, _ := gmssl.Sm3Pbkdf2("Password", salt, gmssl.Sm3Pbkdf2MinIter, gmssl.Sm3HmacMinKeySize) |
| 38 | + fmt.Printf("Sm3Pbkdf2('Password') : %x\n", kdf_key) |
| 39 | + |
| 40 | + fmt.Printf("Sm4KeySize = %d\n", gmssl.Sm4KeySize) |
| 41 | + fmt.Printf("Sm4BlockSize = %d\n", gmssl.Sm4BlockSize) |
| 42 | + |
| 43 | + block, _ := gmssl.RandBytes(gmssl.Sm4BlockSize) |
| 44 | + sm4_enc, _ := gmssl.NewSm4(key, true) |
| 45 | + cblock, _ := sm4_enc.Encrypt(block) |
| 46 | + fmt.Printf("SM4 Plaintext : %x\n", block) |
| 47 | + fmt.Printf("SM4 Ciphertext: %x\n", cblock) |
| 48 | + |
| 49 | + sm4_dec, _ := gmssl.NewSm4(key, false) |
| 50 | + dblock, _ := sm4_dec.Encrypt(cblock) |
| 51 | + fmt.Printf("SM4 Decrypted : %x\n", dblock) |
| 52 | + |
| 53 | + fmt.Printf("Sm4CbcIvSize = %d\n", gmssl.Sm4CbcIvSize) |
| 54 | + iv, _ := gmssl.RandBytes(gmssl.Sm4CbcIvSize) |
| 55 | + |
| 56 | + sm4_cbc_enc, _ := gmssl.NewSm4Cbc(key, iv, true) |
| 57 | + cbc_ciphertext, _ := sm4_cbc_enc.Update([]byte("abc")) |
| 58 | + cbc_ciphertext_last, _ := sm4_cbc_enc.Finish() |
| 59 | + cbc_ciphertext = append(cbc_ciphertext, cbc_ciphertext_last...) |
| 60 | + fmt.Printf("ciphertext = %x\n", cbc_ciphertext) |
| 61 | + sm4_cbc_dec, _ := gmssl.NewSm4Cbc(key, iv, false) |
| 62 | + cbc_plaintext, _ := sm4_cbc_dec.Update(cbc_ciphertext) |
| 63 | + cbc_plaintext_last, _ := sm4_cbc_dec.Finish() |
| 64 | + cbc_plaintext = append(cbc_plaintext, cbc_plaintext_last...) |
| 65 | + fmt.Printf("plaintext = %x\n", cbc_plaintext) |
| 66 | + |
| 67 | + sm4_ctr, _ := gmssl.NewSm4Ctr(key, iv) |
| 68 | + ctr_ciphertext, _ := sm4_ctr.Update([]byte("abc")) |
| 69 | + ctr_ciphertext_last, _ := sm4_ctr.Finish() |
| 70 | + ctr_ciphertext = append(ctr_ciphertext, ctr_ciphertext_last...) |
| 71 | + fmt.Printf("ciphertext = %x\n", ctr_ciphertext) |
| 72 | + |
| 73 | + sm4_ctr, _ = gmssl.NewSm4Ctr(key, iv) |
| 74 | + ctr_plaintext, _ := sm4_ctr.Update(ctr_ciphertext) |
| 75 | + ctr_plaintext_last, _ := sm4_ctr.Finish() |
| 76 | + ctr_plaintext = append(ctr_plaintext, ctr_plaintext_last...) |
| 77 | + fmt.Printf("plaintext = %x\n", ctr_plaintext) |
| 78 | + |
| 79 | + |
| 80 | + fmt.Printf("Sm4GcmMinIvSize = %d\n", gmssl.Sm4GcmMinIvSize) |
| 81 | + fmt.Printf("Sm4GcmMinIvSize = %d\n", gmssl.Sm4GcmMinIvSize) |
| 82 | + fmt.Printf("Sm4GcmDefaultIvSize = %d\n", gmssl.Sm4GcmDefaultIvSize) |
| 83 | + fmt.Printf("Sm4GcmDefaultTagSize = %d\n", gmssl.Sm4GcmDefaultTagSize) |
| 84 | + fmt.Printf("Sm4GcmMaxTagSize = %d\n", gmssl.Sm4GcmMaxTagSize) |
| 85 | + aad, _ := gmssl.RandBytes(20) |
| 86 | + taglen := gmssl.Sm4GcmDefaultTagSize |
| 87 | + iv, _ = gmssl.RandBytes(gmssl.Sm4GcmDefaultIvSize) |
| 88 | + |
| 89 | + sm4_gcm_enc, _ := gmssl.NewSm4Gcm(key, iv, aad, taglen, true) |
| 90 | + gcm_ciphertext, _ := sm4_gcm_enc.Update([]byte("abc")) |
| 91 | + gcm_ciphertext_last, _ := sm4_gcm_enc.Finish() |
| 92 | + gcm_ciphertext = append(gcm_ciphertext, gcm_ciphertext_last...) |
| 93 | + fmt.Printf("ciphertext = %x\n", gcm_ciphertext) |
| 94 | + sm4_gcm_dec, _ := gmssl.NewSm4Gcm(key, iv, aad, taglen, false) |
| 95 | + gcm_plaintext, _ := sm4_gcm_dec.Update(gcm_ciphertext) |
| 96 | + gcm_plaintext_last, _ := sm4_gcm_dec.Finish() |
| 97 | + gcm_plaintext = append(gcm_plaintext, gcm_plaintext_last...) |
| 98 | + fmt.Printf("plaintext = %x\n", gcm_plaintext) |
| 99 | + |
| 100 | + |
| 101 | + fmt.Printf("ZucKeySize = %d\n", gmssl.ZucKeySize) |
| 102 | + fmt.Printf("ZucIvSize = %d\n", gmssl.ZucIvSize) |
| 103 | + iv, _ = gmssl.RandBytes(gmssl.ZucIvSize) |
| 104 | + |
| 105 | + zuc, _ := gmssl.NewZuc(key, iv) |
| 106 | + zuc_ciphertext, _ := zuc.Update([]byte("abc")) |
| 107 | + zuc_ciphertext_last, _ := zuc.Finish() |
| 108 | + zuc_ciphertext = append(zuc_ciphertext, zuc_ciphertext_last...) |
| 109 | + zuc, _ = gmssl.NewZuc(key, iv) |
| 110 | + zuc_plaintext, _ := zuc.Update(zuc_ciphertext) |
| 111 | + zuc_plaintext_last, _ := zuc.Finish() |
| 112 | + zuc_plaintext = append(zuc_plaintext, zuc_plaintext_last...) |
| 113 | + fmt.Printf("plaintext = %x\n", zuc_plaintext) |
| 114 | + |
| 115 | + fmt.Printf("Sm2DefaultId = %s\n", gmssl.Sm2DefaultId) |
| 116 | + fmt.Printf("Sm2MaxSignatureSize = %d\n", gmssl.Sm2MaxSignatureSize) |
| 117 | + fmt.Printf("Sm2MinPlaintextSize = %d\n", gmssl.Sm2MinPlaintextSize) |
| 118 | + fmt.Printf("Sm2MaxPlaintextSize = %d\n", gmssl.Sm2MaxPlaintextSize) |
| 119 | + fmt.Printf("Sm2MinCiphertextSize = %d\n", gmssl.Sm2MinCiphertextSize) |
| 120 | + fmt.Printf("Sm2MaxCiphertextSize = %d\n", gmssl.Sm2MaxCiphertextSize) |
| 121 | + |
| 122 | + sm2, _ := gmssl.GenerateSm2Key() |
| 123 | + sm2.ExportEncryptedPrivateKeyInfoPem("Password", "sm2.pem") |
| 124 | + sm2.ExportPublicKeyInfoPem("sm2pub.pem") |
| 125 | + sm2pri, _ := gmssl.ImportSm2EncryptedPrivateKeyInfoPem("Password", "sm2.pem") |
| 126 | + sm2pub, _ := gmssl.ImportSm2PublicKeyInfoPem("sm2pub.pem") |
| 127 | + z, _ := sm2pub.ComputeZ(gmssl.Sm2DefaultId) |
| 128 | + fmt.Printf("Z = %x\n", z) |
| 129 | + Z, _ := sm2pri.ComputeZ(gmssl.Sm2DefaultId) |
| 130 | + fmt.Printf("Z = %x\n", Z) |
| 131 | + |
| 132 | + signature, _ := sm2pri.Sign(dgst) |
| 133 | + fmt.Printf("Signature = %x\n", signature) |
| 134 | + ret := sm2pub.Verify(dgst, signature) |
| 135 | + fmt.Print("Verify success = ", ret, "\n") |
| 136 | + |
| 137 | + sm2_ciphertext, _ := sm2pub.Encrypt([]byte("abc")) |
| 138 | + sm2_plaintext, _ := sm2pri.Decrypt(sm2_ciphertext) |
| 139 | + fmt.Printf("SM2 Ciphertext : %x\n", sm2_ciphertext) |
| 140 | + fmt.Printf("SM2 Plaintext : %s\n", sm2_plaintext) |
| 141 | + |
| 142 | + fmt.Printf("Sm9MaxIdSize = %d\n", gmssl.Sm9MaxIdSize) |
| 143 | + fmt.Printf("Sm9MaxPlaintextSize = %d\n", gmssl.Sm9MaxPlaintextSize) |
| 144 | + fmt.Printf("Sm9MaxCiphertextSize = %d\n", gmssl.Sm9MaxCiphertextSize) |
| 145 | + fmt.Printf("Sm9SignatureSize = %d\n", gmssl.Sm9SignatureSize) |
| 146 | + |
| 147 | + sm9_enc_master, _ := gmssl.GenerateSm9EncMasterKey() |
| 148 | + sm9_enc_master.ExportEncryptedMasterKeyInfoPem("sm9enc.pem", "password") |
| 149 | + sm9_enc_master.ExportMasterPublicKeyPem("sm9encpub.pem") |
| 150 | + |
| 151 | + sm9_enc_master, _ = gmssl.ImportEncryptedSm9EncMasterKeyInfoPem("sm9enc.pem", "password") |
| 152 | + sm9_enc_master_pub, _ := gmssl.ImportSm9EncMasterPublicKeyPem("sm9encpub.pem") |
| 153 | + |
| 154 | + sm9_ciphertext, _ := sm9_enc_master_pub.Encrypt([]byte("plaintext"), "Alice") |
| 155 | + fmt.Printf("SM9 Ciphertext : %x\n", sm9_ciphertext) |
| 156 | + |
| 157 | + sm9_enc_key, _ := sm9_enc_master.ExtractKey("Alice") |
| 158 | + fmt.Printf("Id : %s\n", sm9_enc_key.GetId()) |
| 159 | + sm9_enc_key.ExportEncryptedPrivateKeyInfoPem("sm9encpri.pem", "password") |
| 160 | + sm9_enc_key, _ = gmssl.ImportEncryptedSm9EncPrivateKeyInfoPem("sm9encpri.pem", "password", "Alice") |
| 161 | + |
| 162 | + sm9_plaintext, _ := sm9_enc_key.Decrypt(sm9_ciphertext) |
| 163 | + fmt.Printf("SM9 Plaintext : %s\n", sm9_plaintext) |
| 164 | + |
| 165 | + sm9_sign_master, _ := gmssl.GenerateSm9SignMasterKey() |
| 166 | + sm9_sign_master.ExportEncryptedMasterKeyInfoPem("sm9sign.pem", "password") |
| 167 | + sm9_sign_master.ExportMasterPublicKeyPem("sm9signpub.pem") |
| 168 | + |
| 169 | + sm9_sign_master, _ = gmssl.ImportEncryptedSm9SignMasterKeyInfoPem("sm9sign.pem", "password") |
| 170 | + sm9_sign_master_pub, _ := gmssl.ImportSm9SignMasterPublicKeyPem("sm9signpub.pem") |
| 171 | + |
| 172 | + sm9_sign_key, _ := sm9_sign_master.ExtractKey("Alice") |
| 173 | + fmt.Printf("Id : %s\n", sm9_sign_key.GetId()) |
| 174 | + sm9_sign_key.ExportEncryptedPrivateKeyInfoPem("sm9signpri.pem", "password") |
| 175 | + sm9_sign_key, _ = gmssl.ImportEncryptedSm9SignPrivateKeyInfoPem("sm9signpri.pem", "password", "Alice") |
| 176 | + |
| 177 | + sm9_sign, _ := gmssl.NewSm9Signature(true) |
| 178 | + sm9_sign.Update([]byte("abc")) |
| 179 | + sm9_signature, _ := sm9_sign.Sign(sm9_sign_key) |
| 180 | + fmt.Printf("SM9 Signature : %x\n", sm9_signature) |
| 181 | + |
| 182 | + sm9_verify, _ := gmssl.NewSm9Signature(false) |
| 183 | + sm9_verify.Update([]byte("abc")) |
| 184 | + sm9_verify_ret := sm9_verify.Verify(sm9_signature, sm9_sign_master_pub, "Alice") |
| 185 | + fmt.Print("Sm9 Verify success : ", sm9_verify_ret, "\n") |
| 186 | + |
| 187 | + /* |
| 188 | + cert, _ := gmssl.ImportSm2CertificatePem("ROOTCA.pem") |
| 189 | + serial, _ := cert.GetSerialNumber() |
| 190 | + fmt.Printf("SerialNumber : %x\n", serial) |
| 191 | +
|
| 192 | + not_before, not_after, _ := cert.GetValidity() |
| 193 | + fmt.Println("NotBefore : ", not_before) |
| 194 | + fmt.Println("NotAfter : ", not_after) |
| 195 | +
|
| 196 | +
|
| 197 | + issuer_raw, issuer, _ := cert.GetIssuer() |
| 198 | + fmt.Println("Issuer: ", issuer) |
| 199 | + fmt.Printf("Issuer (raw) : %x\n", issuer_raw) |
| 200 | +
|
| 201 | + subject_raw, subject, _ := cert.GetSubject() |
| 202 | + fmt.Println("Subject: ", subject) |
| 203 | + fmt.Printf("Subject (raw) : %x\n", subject_raw) |
| 204 | +
|
| 205 | +
|
| 206 | + subject_public_key, _ := cert.GetSubjectPublicKey() |
| 207 | + subject_public_key.ExportPublicKeyInfoPem("subject_public_key.pem") |
| 208 | +
|
| 209 | + cert_verify_ret := cert.VerifyByCaCertificate(cert, gmssl.Sm2DefaultId) |
| 210 | + fmt.Println("Cert Verify success : ", cert_verify_ret) |
| 211 | + */ |
| 212 | +} |
| 213 | + |
| 214 | + |
| 215 | + |
0 commit comments