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.

scrypt.go 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package hash
  4. import (
  5. "encoding/hex"
  6. "strings"
  7. "code.gitea.io/gitea/modules/log"
  8. "golang.org/x/crypto/scrypt"
  9. )
  10. func init() {
  11. MustRegister("scrypt", NewScryptHasher)
  12. }
  13. // ScryptHasher implements PasswordHasher
  14. // and uses the scrypt key derivation function.
  15. type ScryptHasher struct {
  16. n, r, p, keyLen int
  17. }
  18. // HashWithSaltBytes a provided password and salt
  19. func (hasher *ScryptHasher) HashWithSaltBytes(password string, salt []byte) string {
  20. if hasher == nil {
  21. return ""
  22. }
  23. hashedPassword, _ := scrypt.Key([]byte(password), salt, hasher.n, hasher.r, hasher.p, hasher.keyLen)
  24. return hex.EncodeToString(hashedPassword)
  25. }
  26. // NewScryptHasher is a factory method to create an ScryptHasher
  27. // The provided config should be either empty or of the form:
  28. // "<n>$<r>$<p>$<keyLen>", where <x> is the string representation
  29. // of an integer
  30. func NewScryptHasher(config string) *ScryptHasher {
  31. // This matches the original configuration for `scrypt` prior to storing hash parameters
  32. // in the database.
  33. // THESE VALUES MUST NOT BE CHANGED OR BACKWARDS COMPATIBILITY WILL BREAK
  34. hasher := &ScryptHasher{
  35. n: 1 << 16,
  36. r: 16,
  37. p: 2, // 2 passes through memory - this default config will use 128MiB in total.
  38. keyLen: 50,
  39. }
  40. if config == "" {
  41. return hasher
  42. }
  43. vals := strings.SplitN(config, "$", 4)
  44. if len(vals) != 4 {
  45. log.Error("invalid scrypt hash spec %s", config)
  46. return nil
  47. }
  48. var err error
  49. hasher.n, err = parseIntParam(vals[0], "n", "scrypt", config, nil)
  50. hasher.r, err = parseIntParam(vals[1], "r", "scrypt", config, err)
  51. hasher.p, err = parseIntParam(vals[2], "p", "scrypt", config, err)
  52. hasher.keyLen, err = parseIntParam(vals[3], "keyLen", "scrypt", config, err)
  53. if err != nil {
  54. return nil
  55. }
  56. return hasher
  57. }