MCPcopy
hub / github.com/tjfoc/gmsm / GenerateKey

Function GenerateKey

sm2/sm2.go:630–652  ·  view source on GitHub ↗
(random io.Reader)

Source from the content-addressed store, hash-verified

628}
629
630func GenerateKey(random io.Reader) (*PrivateKey, error) {
631 c := P256Sm2()
632 if random == nil {
633 random = rand.Reader //If there is no external trusted random source,please use rand.Reader to instead of it.
634 }
635 params := c.Params()
636 b := make([]byte, params.BitSize/8+8)
637 _, err := io.ReadFull(random, b)
638 if err != nil {
639 return nil, err
640 }
641
642 k := new(big.Int).SetBytes(b)
643 n := new(big.Int).Sub(params.N, two)
644 k.Mod(k, n)
645 k.Add(k, one)
646 priv := new(PrivateKey)
647 priv.PublicKey.Curve = c
648 priv.D = k
649 priv.PublicKey.X, priv.PublicKey.Y = c.ScalarBaseMult(k.Bytes())
650
651 return priv, nil
652}
653
654type zr struct {
655 io.Reader

Callers 4

Test_P12EncryptFunction · 0.92
TestX509Function · 0.92
TestSm2Function · 0.85
BenchmarkSM2Function · 0.85

Calls 4

P256Sm2Function · 0.85
ParamsMethod · 0.80
ScalarBaseMultMethod · 0.80
AddMethod · 0.45

Tested by 4

Test_P12EncryptFunction · 0.74
TestX509Function · 0.74
TestSm2Function · 0.68
BenchmarkSM2Function · 0.68

Used in the wild real call sites across dependent graphs

searching dependent graphs…