You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

crypt.go 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // (C) Copyright 2013, Jonas mg. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license
  3. // that can be found in the LICENSE file.
  4. // Package crypt provides interface for password crypt functions and collects
  5. // common constants.
  6. package crypt
  7. import (
  8. "errors"
  9. "strings"
  10. "github.com/GehirnInc/crypt/common"
  11. )
  12. var ErrKeyMismatch = errors.New("hashed value is not the hash of the given password")
  13. // Crypter is the common interface implemented by all crypt functions.
  14. type Crypter interface {
  15. // Generate performs the hashing algorithm, returning a full hash suitable
  16. // for storage and later password verification.
  17. //
  18. // If the salt is empty, a randomly-generated salt will be generated with a
  19. // length of SaltLenMax and number RoundsDefault of rounds.
  20. //
  21. // Any error only can be got when the salt argument is not empty.
  22. Generate(key, salt []byte) (string, error)
  23. // Verify compares a hashed key with its possible key equivalent.
  24. // Returns nil on success, or an error on failure; if the hashed key is
  25. // diffrent, the error is "ErrKeyMismatch".
  26. Verify(hashedKey string, key []byte) error
  27. // Cost returns the hashing cost (in rounds) used to create the given hashed
  28. // key.
  29. //
  30. // When, in the future, the hashing cost of a key needs to be increased in
  31. // order to adjust for greater computational power, this function allows one
  32. // to establish which keys need to be updated.
  33. //
  34. // The algorithms based in MD5-crypt use a fixed value of rounds.
  35. Cost(hashedKey string) (int, error)
  36. // SetSalt sets a different salt. It is used to easily create derivated
  37. // algorithms, i.e. "apr1_crypt" from "md5_crypt".
  38. SetSalt(salt common.Salt)
  39. }
  40. // Crypt identifies a crypt function that is implemented in another package.
  41. type Crypt uint
  42. const (
  43. APR1 Crypt = 1 + iota // import github.com/GehirnInc/crypt/apr1_crypt
  44. MD5 // import github.com/GehirnInc/crypt/md5_crypt
  45. SHA256 // import github.com/GehirnInc/crypt/sha256_crypt
  46. SHA512 // import github.com/GehirnInc/crypt/sha512_crypt
  47. maxCrypt
  48. )
  49. var crypts = make([]func() Crypter, maxCrypt)
  50. // New returns new Crypter making the Crypt c.
  51. // New panics if the Crypt c is unavailable.
  52. func (c Crypt) New() Crypter {
  53. if c > 0 && c < maxCrypt {
  54. f := crypts[c]
  55. if f != nil {
  56. return f()
  57. }
  58. }
  59. panic("crypt: requested crypt function is unavailable")
  60. }
  61. // Available reports whether the Crypt c is available.
  62. func (c Crypt) Available() bool {
  63. return c > 0 && c < maxCrypt && crypts[c] != nil
  64. }
  65. var cryptPrefixes = make([]string, maxCrypt)
  66. // RegisterCrypt registers a function that returns a new instance of the given
  67. // crypt function. This is intended to be called from the init function in
  68. // packages that implement crypt functions.
  69. func RegisterCrypt(c Crypt, f func() Crypter, prefix string) {
  70. if c >= maxCrypt {
  71. panic("crypt: RegisterHash of unknown crypt function")
  72. }
  73. crypts[c] = f
  74. cryptPrefixes[c] = prefix
  75. }
  76. // New returns a new crypter.
  77. func New(c Crypt) Crypter {
  78. return c.New()
  79. }
  80. // IsHashSupported returns true if hashedKey has a supported prefix.
  81. // NewFromHash will not panic for this hashedKey
  82. func IsHashSupported(hashedKey string) bool {
  83. for i := range cryptPrefixes {
  84. prefix := cryptPrefixes[i]
  85. if crypts[i] != nil && strings.HasPrefix(hashedKey, prefix) {
  86. return true
  87. }
  88. }
  89. return false
  90. }
  91. // NewFromHash returns a new Crypter using the prefix in the given hashed key.
  92. func NewFromHash(hashedKey string) Crypter {
  93. for i := range cryptPrefixes {
  94. prefix := cryptPrefixes[i]
  95. if crypts[i] != nil && strings.HasPrefix(hashedKey, prefix) {
  96. crypt := Crypt(uint(i))
  97. return crypt.New()
  98. }
  99. }
  100. panic("crypt: unknown crypt function")
  101. }