gm_plugins

package module
v0.0.0-...-dc235c6 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 21, 2020 License: Apache-2.0 Imports: 3 Imported by: 2

README

gm-plugins

gm-plugins库是对gm-go库的封装

详细文档访问:https://pkg.go.dev/github.com/zhigui-projects/gm-plugins

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

func GetSmCryptoSuite() primitive.Context

Types

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL