ed25519_go113.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. // +build go1.13
  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. import (
  19. "crypto/ed25519"
  20. "io"
  21. )
  22. const (
  23. // PublicKeySize is the size, in bytes, of public keys as used in this package.
  24. PublicKeySize = 32
  25. // PrivateKeySize is the size, in bytes, of private keys as used in this package.
  26. PrivateKeySize = 64
  27. // SignatureSize is the size, in bytes, of signatures generated and verified by this package.
  28. SignatureSize = 64
  29. // SeedSize is the size, in bytes, of private key seeds. These are the private key representations used by RFC 8032.
  30. SeedSize = 32
  31. )
  32. // PublicKey is the type of Ed25519 public keys.
  33. //
  34. // This type is an alias for crypto/ed25519's PublicKey type.
  35. // See the crypto/ed25519 package for the methods on this type.
  36. type PublicKey = ed25519.PublicKey
  37. // PrivateKey is the type of Ed25519 private keys. It implements crypto.Signer.
  38. //
  39. // This type is an alias for crypto/ed25519's PrivateKey type.
  40. // See the crypto/ed25519 package for the methods on this type.
  41. type PrivateKey = ed25519.PrivateKey
  42. // GenerateKey generates a public/private key pair using entropy from rand.
  43. // If rand is nil, crypto/rand.Reader will be used.
  44. func GenerateKey(rand io.Reader) (PublicKey, PrivateKey, error) {
  45. return ed25519.GenerateKey(rand)
  46. }
  47. // NewKeyFromSeed calculates a private key from a seed. It will panic if
  48. // len(seed) is not SeedSize. This function is provided for interoperability
  49. // with RFC 8032. RFC 8032's private keys correspond to seeds in this
  50. // package.
  51. func NewKeyFromSeed(seed []byte) PrivateKey {
  52. return ed25519.NewKeyFromSeed(seed)
  53. }
  54. // Sign signs the message with privateKey and returns a signature. It will
  55. // panic if len(privateKey) is not PrivateKeySize.
  56. func Sign(privateKey PrivateKey, message []byte) []byte {
  57. return ed25519.Sign(privateKey, message)
  58. }
  59. // Verify reports whether sig is a valid signature of message by publicKey. It
  60. // will panic if len(publicKey) is not PublicKeySize.
  61. func Verify(publicKey PublicKey, message, sig []byte) bool {
  62. return ed25519.Verify(publicKey, message, sig)
  63. }