mirror of
https://github.com/guanzhi/GmSSL.git
synced 2026-08-09 07:23:56 +08:00
update go-api
GetDigestNames() and GetCipherNames get supported algorithms with callbacks.
This commit is contained in:
@@ -28,13 +28,7 @@ func main() {
|
||||
}
|
||||
fmt.Println("");
|
||||
|
||||
fmt.Println("Supported Message Authentication Codes:")
|
||||
macs := gmssl.GetMacNames()
|
||||
for _, mac := range macs {
|
||||
fmt.Println(" " + mac)
|
||||
}
|
||||
fmt.Println("");
|
||||
|
||||
/* sm3 digest */
|
||||
sm3, err := gmssl.NewDigestContext("SM3", nil)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
@@ -55,45 +49,92 @@ func main() {
|
||||
}
|
||||
fmt.Printf("sm3(\"abc\") = %x\n", sm3digest)
|
||||
|
||||
/* hmac-sm3 */
|
||||
hmac_sm3, err := gmssl.NewHMACContext("SM3", nil, []byte("this is the key"))
|
||||
if err != nil {
|
||||
fmt.Println("shit1")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err := hmac_sm3.Update([]byte("hello")); err != nil {
|
||||
fmt.Println("shit2")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
if err := hmac_sm3.Update([]byte("world")); err != nil {
|
||||
fmt.Println("shit3")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
mactag, err := hmac_sm3.Final()
|
||||
if err != nil {
|
||||
fmt.Println("shit4")
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
fmt.Printf("hmac-sm3() = %x\n", mactag)
|
||||
|
||||
/*
|
||||
key := []byte("key")
|
||||
iv := []byte("iv")
|
||||
sms4, err := gmssl.NewCipherContext("SMS4", nil, key, iv, true)
|
||||
/* generate random key */
|
||||
keylen, err := gmssl.GetCipherKeyLength("SMS4")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
key, err := gmssl.GenerateRandom(keylen)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
ciphertext1, err := sms4.Update([]byte("hello"))
|
||||
/* generate random iv */
|
||||
ivlen, err := gmssl.GetCipherIVLength("SMS4")
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
iv, err := gmssl.GenerateRandom(ivlen)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
ciphertext2, err := sms4.Final()
|
||||
/* encrypt */
|
||||
encryptor, err := gmssl.NewCipherContext("SMS4", nil, key, iv, true)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
ciphertext1, err := encryptor.Update([]byte("hello"))
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
ciphertext2, err := encryptor.Final()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
ciphertext := make([]byte, 0, len(ciphertext1) + len(ciphertext2))
|
||||
ciphertext = append(ciphertext, ciphertext1...)
|
||||
ciphertext = append(ciphertext, ciphertext2...)
|
||||
fmt.Printf("%x", ciphertext)
|
||||
*/
|
||||
fmt.Printf("sms4(\"hello\") = %x\n", ciphertext)
|
||||
|
||||
/* decrypt */
|
||||
decryptor, err := gmssl.NewCipherContext("SMS4", nil, key, iv, false)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
plaintext1, err := decryptor.Update(ciphertext)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
plaintext2, err := decryptor.Final()
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
plaintext := make([]byte, 0, len(plaintext1) + len(plaintext2))
|
||||
plaintext = append(plaintext, plaintext1...)
|
||||
plaintext = append(plaintext, plaintext2...)
|
||||
fmt.Println(string(plaintext))
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user