main.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. // Copyright 2018-2019 The NATS Authors
  2. // Licensed under the Apache License, Version 2.0 (the "License");
  3. // you may not use this file except in compliance with the License.
  4. // You may obtain a copy of the License at
  5. //
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // Package nkeys is an Ed25519 based public-key signature system that simplifies keys and seeds
  14. // and performs signing and verification.
  15. package nkeys
  16. import (
  17. "errors"
  18. )
  19. // Version is our current version
  20. const Version = "0.3.0"
  21. // Errors
  22. var (
  23. ErrInvalidPrefixByte = errors.New("nkeys: invalid prefix byte")
  24. ErrInvalidKey = errors.New("nkeys: invalid key")
  25. ErrInvalidPublicKey = errors.New("nkeys: invalid public key")
  26. ErrInvalidSeedLen = errors.New("nkeys: invalid seed length")
  27. ErrInvalidSeed = errors.New("nkeys: invalid seed")
  28. ErrInvalidEncoding = errors.New("nkeys: invalid encoded key")
  29. ErrInvalidSignature = errors.New("nkeys: signature verification failed")
  30. ErrCannotSign = errors.New("nkeys: can not sign, no private key available")
  31. ErrPublicKeyOnly = errors.New("nkeys: no seed or private key available")
  32. ErrIncompatibleKey = errors.New("nkeys: incompatible key")
  33. )
  34. // KeyPair provides the central interface to nkeys.
  35. type KeyPair interface {
  36. Seed() ([]byte, error)
  37. PublicKey() (string, error)
  38. PrivateKey() ([]byte, error)
  39. Sign(input []byte) ([]byte, error)
  40. Verify(input []byte, sig []byte) error
  41. Wipe()
  42. }
  43. // CreateUser will create a User typed KeyPair.
  44. func CreateUser() (KeyPair, error) {
  45. return CreatePair(PrefixByteUser)
  46. }
  47. // CreateAccount will create an Account typed KeyPair.
  48. func CreateAccount() (KeyPair, error) {
  49. return CreatePair(PrefixByteAccount)
  50. }
  51. // CreateServer will create a Server typed KeyPair.
  52. func CreateServer() (KeyPair, error) {
  53. return CreatePair(PrefixByteServer)
  54. }
  55. // CreateCluster will create a Cluster typed KeyPair.
  56. func CreateCluster() (KeyPair, error) {
  57. return CreatePair(PrefixByteCluster)
  58. }
  59. // CreateOperator will create an Operator typed KeyPair.
  60. func CreateOperator() (KeyPair, error) {
  61. return CreatePair(PrefixByteOperator)
  62. }
  63. // FromPublicKey will create a KeyPair capable of verifying signatures.
  64. func FromPublicKey(public string) (KeyPair, error) {
  65. raw, err := decode([]byte(public))
  66. if err != nil {
  67. return nil, err
  68. }
  69. pre := PrefixByte(raw[0])
  70. if err := checkValidPublicPrefixByte(pre); err != nil {
  71. return nil, ErrInvalidPublicKey
  72. }
  73. return &pub{pre, raw[1:]}, nil
  74. }
  75. // FromSeed will create a KeyPair capable of signing and verifying signatures.
  76. func FromSeed(seed []byte) (KeyPair, error) {
  77. _, _, err := DecodeSeed(seed)
  78. if err != nil {
  79. return nil, err
  80. }
  81. copy := append([]byte{}, seed...)
  82. return &kp{copy}, nil
  83. }
  84. // FromRawSeed will create a KeyPair from the raw 32 byte seed for a given type.
  85. func FromRawSeed(prefix PrefixByte, rawSeed []byte) (KeyPair, error) {
  86. seed, err := EncodeSeed(prefix, rawSeed)
  87. if err != nil {
  88. return nil, err
  89. }
  90. return &kp{seed}, nil
  91. }