sm2

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Apr 6, 2022 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package sm2 is implemented based on GB/T 32918.1-2016, GB/T 32918.2-2016, and GB/T 32918.5-2017.

Example
package main

import (
	"crypto/rand"
	"fmt"

	"github.com/need-being/gmcrypto/sm2"
)

func main() {
	privateKey, err := sm2.GenerateKey(sm2.Curve(), rand.Reader)
	if err != nil {
		panic(err)
	}
	privateKey.ID = []byte("example ID")

	// Sign message
	message := []byte("hello world")
	signature, err := sm2.Sign(rand.Reader, privateKey, message)
	if err != nil {
		panic(err)
	}
	fmt.Println("message signed")

	// Verify message
	publicKey := &privateKey.PublicKey
	if sm2.Verify(publicKey, message, signature) {
		fmt.Println("message verified")
	}

	// Verify with tampered message
	message = []byte("foobar")
	if !sm2.Verify(publicKey, message, signature) {
		fmt.Println("verification failed as expected")
	}
}
Output:

message signed
message verified
verification failed as expected

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Curve

func Curve() elliptic.Curve

Curve returns an elliptic curve for SM2.

func Sign

func Sign(rand io.Reader, priv *PrivateKey, message []byte) ([]byte, error)

Sign signs the message with a private key and returns a signature. The signature is in the form of (r, s) where r and s have the same length. SM3 is used for hash algorithm.

func Verify

func Verify(pub *PublicKey, message, sig []byte) bool

Verify reports whether sig is a valid signature of message by the given public key.

Types

type PrivateKey

type PrivateKey struct {
	PublicKey
	D *big.Int
}

PrivateKey represents an SM2 private key.

func GenerateKey

func GenerateKey(c elliptic.Curve, rand io.Reader) (*PrivateKey, error)

GenerateKey generates a public and private key pair.

func (*PrivateKey) Equal

func (priv *PrivateKey) Equal(x crypto.PrivateKey) bool

Equal reports whether priv and x have the same value.

func (*PrivateKey) Public

func (priv *PrivateKey) Public() crypto.PublicKey

Public returns the public key corresponding to priv.

func (*PrivateKey) Sign

func (priv *PrivateKey) Sign(rand io.Reader, message []byte, opts crypto.SignerOpts) (signature []byte, err error)

Sign signs the given message with priv. SM2 relies on hash over message and the identity of the signer, and therefore cannot handle pre-hashed messages. Thus opts.HashFunc() must return zero to indicate the message hasn't been hashed if opts presents. This can be achieved by passing crypto.Hash(0) as the value for opts.

type PublicKey

type PublicKey struct {
	elliptic.Curve
	X, Y *big.Int

	// ID is the identifier of the signer.
	// The max size of an ID is 65,535 bytes.
	//
	// ID is not a part of the public key defined in GB/T 32918.2-2016.
	// Since it is commonly a good practice to use one key pair per identity,
	// it makes more sense to bind the ID with the public key.
	// In the scenario that a key pair is associated with multiple identities,
	// multiple instances of PublicKey or PrivateKey should be created for each
	// identity with other fields remaining the same.
	// This also enables SM2 to support crypto.Signer.
	ID []byte
}

PublicKey represents an SM2 public key.

func (*PublicKey) Digest

func (pub *PublicKey) Digest() ([]byte, error)

Digest returns the value Z defined in GB/T 32918.2-2016 by hashing. SM3 is used for hash algorithm.

func (*PublicKey) Equal

func (pub *PublicKey) Equal(x crypto.PublicKey) bool

Equal reports whether pub and x have the same value.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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