github.com/geph-official/geph2@v0.22.6-0.20210211030601-f527cb59b0df/libs/c25519/keygen.go (about)

     1  package c25519
     2  
     3  import (
     4  	"crypto/rand"
     5  	"crypto/sha256"
     6  
     7  	"golang.org/x/crypto/curve25519"
     8  )
     9  
    10  // GenSK makes a new Curve25519 secret key.
    11  func GenSK() [32]byte {
    12  	var toret [32]byte
    13  	rand.Read(toret[:])
    14  	toret[0] &= 248
    15  	toret[31] &= 127
    16  	toret[31] |= 64
    17  	return toret
    18  }
    19  
    20  // GenSKWithSeed makes a new Curve25519 secret key from a seed.
    21  func GenSKWithSeed(seed []byte) [32]byte {
    22  	toret := sha256.Sum256(seed)
    23  	toret[0] &= 248
    24  	toret[31] &= 127
    25  	toret[31] |= 64
    26  	return toret
    27  }
    28  
    29  // ToPK converts a secret key to a public key.
    30  func ToPK(sk [32]byte) [32]byte {
    31  	var pk [32]byte
    32  	curve25519.ScalarBaseMult(&pk, &sk)
    33  	return pk
    34  }