本文整理匯總了Golang中crypto/elliptic.GenerateKey函數的典型用法代碼示例。如果您正苦於以下問題:Golang GenerateKey函數的具體用法?Golang GenerateKey怎麽用?Golang GenerateKey使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了GenerateKey函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: main
func main() {
u := must(url.Parse("http://SUCCESS"))
b, x, y := must(elliptic.GenerateKey(elliptic.P224(), ConstWriter(0)))
if must(url.Parse("http://example.com/a/b")).IsAbs() {
fmt.Println(u.Host)
}
}
示例2: WrapNewKey
func (alg *Ecdh) WrapNewKey(cekSizeBits int, key interface{}, header map[string]interface{}) (cek []byte, encryptedCek []byte, err error) {
if pubKey, ok := key.(*ecdsa.PublicKey); ok {
if _, ok := header[alg.idHeader()].(string); !ok {
return nil, nil, errors.New(fmt.Sprintf("Ecdh.WrapNewKey(): expected '%v' param in JWT header, but was not found.", alg.idHeader()))
}
var d []byte
var x, y *big.Int
if d, x, y, err = elliptic.GenerateKey(pubKey.Curve, rand.Reader); err != nil {
return nil, nil, err
}
ephemeral := ecc.NewPrivate(x.Bytes(), y.Bytes(), d)
xBytes := padding.Align(x.Bytes(), pubKey.Curve.Params().BitSize)
yBytes := padding.Align(y.Bytes(), pubKey.Curve.Params().BitSize)
epk := map[string]string{
"kty": "EC",
"x": base64url.Encode(xBytes),
"y": base64url.Encode(yBytes),
"crv": name(pubKey.Curve),
}
header["epk"] = epk
return alg.deriveKey(pubKey, ephemeral, cekSizeBits, header), nil, nil
}
return nil, nil, errors.New("Ecdh.WrapNewKey(): expected key to be '*ecdsa.PublicKey'")
}
示例3: offer
func (e *ellipticECDHCurve) offer(rand io.Reader) (publicKey []byte, err error) {
var x, y *big.Int
e.privateKey, x, y, err = elliptic.GenerateKey(e.curve, rand)
if err != nil {
return nil, err
}
return elliptic.Marshal(e.curve, x, y), nil
}
示例4: CreateKey
// Create a new Public-Private ECC-256 Keypair.
func CreateKey(log chan string) ([]byte, *big.Int, *big.Int) {
priv, x, y, err := elliptic.GenerateKey(elliptic.P256(), rand.Reader)
if err != nil {
log <- "Key Generation Error"
return nil, nil, nil
}
return priv, x, y
}
示例5: GenerateKey
// GenerateKey returns a new keypair
func (curve Curve) GenerateKey() (priv []byte, pub *Point, err error) {
priv, x, y, err := elliptic.GenerateKey(curve.Curve, curve.Rand)
if err != nil {
return nil, nil, err
}
pub = new(Point)
pub.X, pub.Y = x, y
return priv, pub, nil
}
示例6: generateServerKeyExchange
func (ka *ecdheRSAKeyAgreement) generateServerKeyExchange(config *Config, cert *Certificate, clientHello *clientHelloMsg, hello *serverHelloMsg) (*serverKeyExchangeMsg, error) {
var curveid uint16
Curve:
for _, c := range clientHello.supportedCurves {
switch c {
case curveP256:
ka.curve = elliptic.P256()
curveid = c
break Curve
case curveP384:
ka.curve = elliptic.P384()
curveid = c
break Curve
case curveP521:
ka.curve = elliptic.P521()
curveid = c
break Curve
}
}
if curveid == 0 {
return nil, errors.New("tls: no supported elliptic curves offered")
}
var x, y *big.Int
var err error
ka.privateKey, x, y, err = elliptic.GenerateKey(ka.curve, config.rand())
if err != nil {
return nil, err
}
ecdhePublic := elliptic.Marshal(ka.curve, x, y)
// http://tools.ietf.org/html/rfc4492#section-5.4
serverECDHParams := make([]byte, 1+2+1+len(ecdhePublic))
serverECDHParams[0] = 3 // named curve
serverECDHParams[1] = byte(curveid >> 8)
serverECDHParams[2] = byte(curveid)
serverECDHParams[3] = byte(len(ecdhePublic))
copy(serverECDHParams[4:], ecdhePublic)
md5sha1 := md5SHA1Hash(clientHello.random, hello.random, serverECDHParams)
sig, err := rsa.SignPKCS1v15(config.rand(), cert.PrivateKey.(*rsa.PrivateKey), crypto.MD5SHA1, md5sha1)
if err != nil {
return nil, errors.New("failed to sign ECDHE parameters: " + err.Error())
}
skx := new(serverKeyExchangeMsg)
skx.key = make([]byte, len(serverECDHParams)+2+len(sig))
copy(skx.key, serverECDHParams)
k := skx.key[len(serverECDHParams):]
k[0] = byte(len(sig) >> 8)
k[1] = byte(len(sig))
copy(k[2:], sig)
return skx, nil
}
示例7: BenchmarkVerify
func BenchmarkVerify(b *testing.B) {
c := elliptic.P256()
_, px, py, _ := elliptic.GenerateKey(c, rand.Reader)
box := VoteZero(c, px, py)
b.ResetTimer()
for i := 0; i < b.N; i++ {
IsValidBox(c, box, px, py)
}
}
示例8: eciesEncrypt
func eciesEncrypt(rand io.Reader, pub *ecdsa.PublicKey, s1, s2 []byte, plain []byte) ([]byte, error) {
params := pub.Curve
// Select an ephemeral elliptic curve key pair associated with
// elliptic curve domain parameters params
priv, Rx, Ry, err := elliptic.GenerateKey(pub.Curve, rand)
//fmt.Printf("Rx %s\n", utils.EncodeBase64(Rx.Bytes()))
//fmt.Printf("Ry %s\n", utils.EncodeBase64(Ry.Bytes()))
// Convert R=(Rx,Ry) to an octed string R bar
// This is uncompressed
Rb := elliptic.Marshal(pub.Curve, Rx, Ry)
// Derive a shared secret field element z from the ephemeral secret key k
// and convert z to an octet string Z
z, _ := params.ScalarMult(pub.X, pub.Y, priv)
Z := z.Bytes()
//fmt.Printf("Z %s\n", utils.EncodeBase64(Z))
// generate keying data K of length ecnKeyLen + macKeyLen octects from Z
// ans s1
kE := make([]byte, 32)
kM := make([]byte, 32)
hkdf := hkdf.New(primitives.GetDefaultHash(), Z, s1, nil)
_, err = hkdf.Read(kE)
if err != nil {
return nil, err
}
_, err = hkdf.Read(kM)
if err != nil {
return nil, err
}
// Use the encryption operation of the symmetric encryption scheme
// to encrypt m under EK as ciphertext EM
EM, err := aesEncrypt(kE, plain)
// Use the tagging operation of the MAC scheme to compute
// the tag D on EM || s2
mac := hmac.New(primitives.GetDefaultHash(), kM)
mac.Write(EM)
if len(s2) > 0 {
mac.Write(s2)
}
D := mac.Sum(nil)
// Output R,EM,D
ciphertext := make([]byte, len(Rb)+len(EM)+len(D))
//fmt.Printf("Rb %s\n", utils.EncodeBase64(Rb))
//fmt.Printf("EM %s\n", utils.EncodeBase64(EM))
//fmt.Printf("D %s\n", utils.EncodeBase64(D))
copy(ciphertext, Rb)
copy(ciphertext[len(Rb):], EM)
copy(ciphertext[len(Rb)+len(EM):], D)
return ciphertext, nil
}
示例9: keypairGen
func keypairGen() KeyPair {
if fakeRandom {
priv := new(Scalar).fromSmallInt(1)
pub := priv.toPoint()
return KeyPair{*priv, pub}
} else {
priv, x, y, _ := elliptic.GenerateKey(getCurve(), rand.Reader)
return KeyPair{Scalar{priv}, Point{x, y}}
}
}
示例10: GenerateKey
func (g genericCurve) GenerateKey(rand io.Reader) (private PrivateKey, public PublicKey, err error) {
if rand == nil {
rand = cryptorand.Reader
}
private, x, y, err := elliptic.GenerateKey(g.curve, rand)
if err != nil {
private = nil
return
}
public = elliptic.Marshal(g.curve, x, y)
return
}
示例11: GenerateECKey
// Q = curve.G * k
// Q => k is ECDLP
func GenerateECKey(curve elliptic.Curve) (*ECKey, error) {
k, xq, yq, e := elliptic.GenerateKey(curve, rand.Reader)
if e != nil {
return nil, e
}
return &ECKey{
curve: curve,
priv: k,
xQ: xq,
yQ: yq,
}, nil
}
示例12: NewECDH_P256
// NewECDH_P256 returns an Elliptic Curve Diffie-Hellman P-256 KeyExchange algorithm.
func NewECDH_P256() (error, KeyExchange) {
curve := elliptic.P256()
priv, x, y, err := elliptic.GenerateKey(curve, rand.Reader)
if err != nil {
return err, nil
}
return nil, &p256{
curve: curve,
publicX: x,
publicY: y,
privateKey: priv}
}
示例13: GenerateKey
// GenerateKey generates an appropriate private and public keypair for
// use in box.
func GenerateKey() (PrivateKey, PublicKey, bool) {
key, x, y, err := elliptic.GenerateKey(curve, PRNG)
if err != nil {
return nil, nil, false
}
peer := elliptic.Marshal(curve, x, y)
if peer == nil {
}
if len(key) != privateKeySize || len(peer) != publicKeySize {
return nil, nil, false
}
return key, peer, true
}
示例14: main
func main() {
c := elliptic.P256()
priv, x, y, err := elliptic.GenerateKey(c, rand.Reader)
if err != nil {
fmt.Printf("Error!\n")
} else {
fmt.Printf("Public Key is %s\n Private key is %s\n",
base64.StdEncoding.EncodeToString(
(elliptic.Marshal(c, x, y))),
hex.EncodeToString(priv))
}
}
示例15: generateKey
func generateKey() (*key, error) {
var (
k = &key{}
err error
)
k.prv.d, k.pub.x, k.pub.y, err = elliptic.GenerateKey(secp160r1.P160(), rand.Reader)
if err != nil {
return nil, err
}
return k, nil
}