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 ¶
Types ¶
type PrivateKey ¶
PrivateKey represents an SM2 private key.
func GenerateKey ¶
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.