Skip to content
/ smx Public
forked from ZZMarquis/gm

Go语言 国密SM2/SM3/SM4算法

License

Notifications You must be signed in to change notification settings

dcto/smx

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

62 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Go语言SM2,SM3,SM4实现

  • 支持JAVA, C1C3C2, C1C2C3 排序算法。
  • 支持招行JAVA Sm2SignWithSm3实现
  • 国密SM2算法实现,支持国密SM2签名,国密SM2加密,国密SM2验签,
  • 国密SM2密钥交换,国密SM2密钥生成,国密SM2密钥转换,
  • 国密SM2密钥导入导出,国密SM2密钥对生成

使用方法

  • 签名:
import (
	"encoding/base64"
	"encoding/hex"
	"errors"
    "byte"
    "github.com/dcto/smn"
)

func Sign(data []byte) ([]byte, error) {
	privateKeyByte, err := hex.DecodeString("私钥HEX")
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	
	privateKey, err := sm2.RawBytesToPrivateKey(privateKeyByte)
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	//国密局SM2密码
	signBytes, err := sm2.Sign(privateKey, []byte("1234567812345678"), []byte(data))

	if err != nil {
		logx.Error(err)
		return nil, err
	}
	return signBytes, nil

}

加密:

import (
	"encoding/base64"
	"encoding/hex"
	"errors"
    "byte"
    "github.com/dcto/smn"
)

func Encrypt(data []byte) ([]byte, error) {

	pubkeyBytes, err := hex.DecodeString("公钥HEX")
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	pubkey, err := sm2.RawBytesToPublicKey(pubkeyBytes)

	if err != nil {
		logx.Error(err)
		return nil, err
	}

	ciphertxt, err := sm2.Encrypt(pubkey, data, 2)

	if err != nil {
		logx.Error(err)
		return nil,err
	}
	return ciphertxt, nil
}

解密:

import (
	"encoding/base64"
	"encoding/hex"
	"errors"
    "byte"
    "github.com/dcto/smn"
)

func Decrypt(data []byte) ([]byte, error) {

	privateKeyByte, err := hex.DecodeString(s.Config.Cert.PriKey)
	if err != nil {
		logx.Error(err)
		return nil, err
	}
	
	privateKey, err := sm2.RawBytesToPrivateKey(privateKeyByte)

	if err != nil {
		logx.Error(err)
		return nil, err
	}
	dataPlain , err := sm2.Decrypt(privateKey, data, 2)
	if err != nil {
		logx.Error(err)
		return nil, err
	}

	logx.Debug(string(dataPlain))
	return nil, nil
}

声明

本库Fork自原作者的声明:

梧桐链: gjfoc

ZZMarquis: ZZMarquis

ZZMarquis版本修改,增加部份说明及招行Java接口 HEX密钥对实现。

About

Go语言 国密SM2/SM3/SM4算法

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Go 100.0%