Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

salt.go 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. // (C) Copyright 2012, Jeramey Crawford <jeramey@antihe.ro>. All
  2. // rights reserved. Use of this source code is governed by a BSD-style
  3. // license that can be found in the LICENSE file.
  4. package common
  5. import (
  6. "bytes"
  7. "crypto/rand"
  8. "errors"
  9. "strconv"
  10. )
  11. var (
  12. ErrSaltPrefix = errors.New("invalid magic prefix")
  13. ErrSaltFormat = errors.New("invalid salt format")
  14. ErrSaltRounds = errors.New("invalid rounds")
  15. )
  16. const (
  17. roundsPrefix = "rounds="
  18. )
  19. // Salt represents a salt.
  20. type Salt struct {
  21. MagicPrefix []byte
  22. SaltLenMin int
  23. SaltLenMax int
  24. RoundsMin int
  25. RoundsMax int
  26. RoundsDefault int
  27. }
  28. // Generate generates a random salt of a given length.
  29. //
  30. // The length is set thus:
  31. //
  32. // length > SaltLenMax: length = SaltLenMax
  33. // length < SaltLenMin: length = SaltLenMin
  34. func (s *Salt) Generate(length int) []byte {
  35. if length > s.SaltLenMax {
  36. length = s.SaltLenMax
  37. } else if length < s.SaltLenMin {
  38. length = s.SaltLenMin
  39. }
  40. saltLen := (length * 6 / 8)
  41. if (length*6)%8 != 0 {
  42. saltLen += 1
  43. }
  44. salt := make([]byte, saltLen)
  45. rand.Read(salt)
  46. out := make([]byte, len(s.MagicPrefix)+length)
  47. copy(out, s.MagicPrefix)
  48. copy(out[len(s.MagicPrefix):], Base64_24Bit(salt))
  49. return out
  50. }
  51. // GenerateWRounds creates a random salt with the random bytes being of the
  52. // length provided, and the rounds parameter set as specified.
  53. //
  54. // The parameters are set thus:
  55. //
  56. // length > SaltLenMax: length = SaltLenMax
  57. // length < SaltLenMin: length = SaltLenMin
  58. //
  59. // rounds < 0: rounds = RoundsDefault
  60. // rounds < RoundsMin: rounds = RoundsMin
  61. // rounds > RoundsMax: rounds = RoundsMax
  62. //
  63. // If rounds is equal to RoundsDefault, then the "rounds=" part of the salt is
  64. // removed.
  65. func (s *Salt) GenerateWRounds(length, rounds int) []byte {
  66. if length > s.SaltLenMax {
  67. length = s.SaltLenMax
  68. } else if length < s.SaltLenMin {
  69. length = s.SaltLenMin
  70. }
  71. if rounds < 0 {
  72. rounds = s.RoundsDefault
  73. } else if rounds < s.RoundsMin {
  74. rounds = s.RoundsMin
  75. } else if rounds > s.RoundsMax {
  76. rounds = s.RoundsMax
  77. }
  78. saltLen := (length * 6 / 8)
  79. if (length*6)%8 != 0 {
  80. saltLen += 1
  81. }
  82. salt := make([]byte, saltLen)
  83. rand.Read(salt)
  84. roundsText := ""
  85. if rounds != s.RoundsDefault {
  86. roundsText = roundsPrefix + strconv.Itoa(rounds) + "$"
  87. }
  88. out := make([]byte, len(s.MagicPrefix)+len(roundsText)+length)
  89. copy(out, s.MagicPrefix)
  90. copy(out[len(s.MagicPrefix):], []byte(roundsText))
  91. copy(out[len(s.MagicPrefix)+len(roundsText):], Base64_24Bit(salt))
  92. return out
  93. }
  94. func (s *Salt) Decode(raw []byte) (salt []byte, rounds int, isRoundsDef bool, rest []byte, err error) {
  95. tokens := bytes.SplitN(raw, []byte{'$'}, 4)
  96. if len(tokens) < 3 {
  97. err = ErrSaltFormat
  98. return
  99. }
  100. if !bytes.HasPrefix(raw, s.MagicPrefix) {
  101. err = ErrSaltPrefix
  102. return
  103. }
  104. if bytes.HasPrefix(tokens[2], []byte(roundsPrefix)) {
  105. if len(tokens) < 4 {
  106. err = ErrSaltFormat
  107. return
  108. }
  109. salt = tokens[3]
  110. rounds, err = strconv.Atoi(string(tokens[2][len(roundsPrefix):]))
  111. if err != nil {
  112. err = ErrSaltRounds
  113. return
  114. }
  115. if rounds < s.RoundsMin {
  116. rounds = s.RoundsMin
  117. }
  118. if rounds > s.RoundsMax {
  119. rounds = s.RoundsMax
  120. }
  121. isRoundsDef = true
  122. } else {
  123. salt = tokens[2]
  124. rounds = s.RoundsDefault
  125. }
  126. if len(salt) > s.SaltLenMax {
  127. salt = salt[0:s.SaltLenMax]
  128. }
  129. return
  130. }