Documentation
¶
Overview ¶
gm-plugins包提供国密算法实现接口,包含SM2、SM3、SM4等,并且提供了基于纯Go的国密算法默认实现。
下面是一个使用gm-plugins包的示例:
package main import ( "fmt" gmplugins "github.com/zhigui-projects/gm-plugins" ) func main() { suite := gmplugins.GetSmCryptoSuite() msg := []byte("hello") pri, err := suite.GenPrivateKey() if err != nil { panic(err) } sign, err := suite.Sign(pri, msg, nil) if err != nil { panic(err) } ok, err := suite.Verify(suite.PublicKey(pri), sign, msg, nil) fmt.Println(ok , err) }
Output:
true <nil>
详情请访问: https://github.com/zhigui-projects/gm-plugins
Example (Sm2) ¶
package main import ( "fmt" "log" gmplugins "github.com/zhigui-projects/gm-plugins" ) func main() { suite := gmplugins.GetSmCryptoSuite() msg := []byte("hello") pri, err := suite.GenPrivateKey() if err != nil { log.Fatal(err) } sign, err := suite.Sign(pri, msg, nil) if err != nil { log.Fatal(err) } ok, err := suite.Verify(suite.PublicKey(pri), sign, msg, nil) if err != nil { log.Fatal(err) } fmt.Println("verify result is: ", ok) }
Output: verify result is: true
Example (Sm3) ¶
package main import ( "encoding/hex" "fmt" gmplugins "github.com/zhigui-projects/gm-plugins" ) func main() { suite := gmplugins.GetSmCryptoSuite() h := suite.NewSm3() h.Write([]byte("hello")) val := h.Sum(nil) fmt.Println(hex.EncodeToString(val)) }
Output: becbbfaae6548b8bf0cfcad5a27183cd1be6093b1cceccc303d9c61d0a645268
Example (Sm4) ¶
package main import ( "fmt" "log" gmplugins "github.com/zhigui-projects/gm-plugins" ) func main() { suite := gmplugins.GetSmCryptoSuite() sk := []byte("1234567890abcdef") block, err := suite.NewSm4Cipher(sk) if err != nil { log.Fatal(err) } d0 := make([]byte, 16) data := []byte{0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10} block.Encrypt(d0, data) d1 := make([]byte, 16) block.Decrypt(d1, d0) fmt.Println(d1) }
Output: [1 35 69 103 137 171 205 239 254 220 186 152 118 84 50 16]
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetSmCryptoSuite ¶
Types ¶
type SmCryptoSuite ¶
Click to show internal directories.
Click to hide internal directories.