...

Source file src/golang.org/x/crypto/ed25519/ed25519.go

Documentation: golang.org/x/crypto/ed25519

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package ed25519 implements the Ed25519 signature algorithm. See
     6  // https://ed25519.cr.yp.to/.
     7  //
     8  // These functions are also compatible with the “Ed25519” function defined in
     9  // RFC 8032. However, unlike RFC 8032's formulation, this package's private key
    10  // representation includes a public key suffix to make multiple signing
    11  // operations with the same key more efficient. This package refers to the RFC
    12  // 8032 private key as the “seed”.
    13  //
    14  // Beginning with Go 1.13, the functionality of this package was moved to the
    15  // standard library as crypto/ed25519. This package only acts as a compatibility
    16  // wrapper.
    17  package ed25519
    18  
    19  import (
    20  	"crypto/ed25519"
    21  	"io"
    22  )
    23  
    24  const (
    25  	// PublicKeySize is the size, in bytes, of public keys as used in this package.
    26  	PublicKeySize = 32
    27  	// PrivateKeySize is the size, in bytes, of private keys as used in this package.
    28  	PrivateKeySize = 64
    29  	// SignatureSize is the size, in bytes, of signatures generated and verified by this package.
    30  	SignatureSize = 64
    31  	// SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
    32  	SeedSize = 32
    33  )
    34  
    35  // PublicKey is the type of Ed25519 public keys.
    36  //
    37  // This type is an alias for crypto/ed25519's PublicKey type.
    38  // See the crypto/ed25519 package for the methods on this type.
    39  type PublicKey = ed25519.PublicKey
    40  
    41  // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
    42  //
    43  // This type is an alias for crypto/ed25519's PrivateKey type.
    44  // See the crypto/ed25519 package for the methods on this type.
    45  type PrivateKey = ed25519.PrivateKey
    46  
    47  // GenerateKey generates a public/private key pair using entropy from rand.
    48  // If rand is nil, crypto/rand.Reader will be used.
    49  func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
    50  	return ed25519.GenerateKey(rand)
    51  }
    52  
    53  // NewKeyFromSeed calculates a private key from a seed. It will panic if
    54  // len(seed) is not SeedSize. This function is provided for interoperability
    55  // with RFC 8032. RFC 8032's private keys correspond to seeds in this
    56  // package.
    57  func NewKeyFromSeed(seed []byte) PrivateKey {
    58  	return ed25519.NewKeyFromSeed(seed)
    59  }
    60  
    61  // Sign signs the message with privateKey and returns a signature. It will
    62  // panic if len(privateKey) is not PrivateKeySize.
    63  func Sign(privateKey PrivateKey, message []byte) []byte {
    64  	return ed25519.Sign(privateKey, message)
    65  }
    66  
    67  // Verify reports whether sig is a valid signature of message by publicKey. It
    68  // will panic if len(publicKey) is not PublicKeySize.
    69  func Verify(publicKey PublicKey, message, sig []byte) bool {
    70  	return ed25519.Verify(publicKey, message, sig)
    71  }
    72  

View as plain text