main.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright 2018 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
  20. const Version = "0.0.2"
  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. )
  33. // KeyPair provides the central interface to nkeys.
  34. type KeyPair interface {
  35. Seed() ([]byte, error)
  36. PublicKey() (string, error)
  37. PrivateKey() ([]byte, error)
  38. Sign(input []byte) ([]byte, error)
  39. Verify(input []byte, sig []byte) error
  40. Wipe()
  41. }
  42. // CreateUser will create a User typed KeyPair.
  43. func CreateUser() (KeyPair, error) {
  44. return CreatePair(PrefixByteUser)
  45. }
  46. // CreateAccount will create an Account typed KeyPair.
  47. func CreateAccount() (KeyPair, error) {
  48. return CreatePair(PrefixByteAccount)
  49. }
  50. // CreateServer will create a Server typed KeyPair.
  51. func CreateServer() (KeyPair, error) {
  52. return CreatePair(PrefixByteServer)
  53. }
  54. // CreateCluster will create a Cluster typed KeyPair.
  55. func CreateCluster() (KeyPair, error) {
  56. return CreatePair(PrefixByteCluster)
  57. }
  58. // CreateOperator will create an Operator typed KeyPair.
  59. func CreateOperator() (KeyPair, error) {
  60. return CreatePair(PrefixByteOperator)
  61. }
  62. // FromPublicKey will create a KeyPair capable of verifying signatures.
  63. func FromPublicKey(public string) (KeyPair, error) {
  64. raw, err := decode([]byte(public))
  65. if err != nil {
  66. return nil, err
  67. }
  68. pre := PrefixByte(raw[0])
  69. if err := checkValidPublicPrefixByte(pre); err != nil {
  70. return nil, ErrInvalidPublicKey
  71. }
  72. return &pub{pre, raw[1:]}, nil
  73. }
  74. // FromSeed will create a KeyPair capable of signing and verifying signatures.
  75. func FromSeed(seed []byte) (KeyPair, error) {
  76. _, _, err := DecodeSeed(seed)
  77. if err != nil {
  78. return nil, err
  79. }
  80. copy := append([]byte{}, seed...)
  81. return &kp{copy}, nil
  82. }
  83. // Create a KeyPair from the raw 32 byte seed for a given type.
  84. func FromRawSeed(prefix PrefixByte, rawSeed []byte) (KeyPair, error) {
  85. seed, err := EncodeSeed(prefix, rawSeed)
  86. if err != nil {
  87. return nil, err
  88. }
  89. return &kp{seed}, nil
  90. }