gitea源码

setting.go 2.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // Copyright 2023 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package hash
  4. // DefaultHashAlgorithmName represents the default value of PASSWORD_HASH_ALGO
  5. // configured in app.ini.
  6. //
  7. // It is NOT the same and does NOT map to the defaultEmptyHashAlgorithmSpecification.
  8. //
  9. // It will be dealiased as per aliasAlgorithmNames whereas
  10. // defaultEmptyHashAlgorithmSpecification does not undergo dealiasing.
  11. const DefaultHashAlgorithmName = "pbkdf2"
  12. var DefaultHashAlgorithm *PasswordHashAlgorithm
  13. // aliasAlgorithNames provides a mapping between the value of PASSWORD_HASH_ALGO
  14. // configured in the app.ini and the parameters used within the hashers internally.
  15. //
  16. // If it is necessary to change the default parameters for any hasher in future you
  17. // should change these values and not those in argon2.go etc.
  18. var aliasAlgorithmNames = map[string]string{
  19. "argon2": "argon2$2$65536$8$50",
  20. "bcrypt": "bcrypt$10",
  21. "scrypt": "scrypt$65536$16$2$50",
  22. "pbkdf2": "pbkdf2_v2", // pbkdf2 should default to pbkdf2_v2
  23. "pbkdf2_v1": "pbkdf2$10000$50",
  24. // The latest PBKDF2 password algorithm is used as the default since it doesn't
  25. // use a lot of memory and is safer to use on less powerful devices.
  26. "pbkdf2_v2": "pbkdf2$50000$50",
  27. // The pbkdf2_hi password algorithm is offered as a stronger alternative to the
  28. // slightly improved pbkdf2_v2 algorithm
  29. "pbkdf2_hi": "pbkdf2$320000$50",
  30. }
  31. var RecommendedHashAlgorithms = []string{
  32. "pbkdf2",
  33. "argon2",
  34. "bcrypt",
  35. "scrypt",
  36. "pbkdf2_hi",
  37. }
  38. // hashAlgorithmToSpec converts an algorithm name or a specification to a full algorithm specification
  39. func hashAlgorithmToSpec(algorithmName string) string {
  40. if algorithmName == "" {
  41. algorithmName = DefaultHashAlgorithmName
  42. }
  43. alias, has := aliasAlgorithmNames[algorithmName]
  44. for has {
  45. algorithmName = alias
  46. alias, has = aliasAlgorithmNames[algorithmName]
  47. }
  48. return algorithmName
  49. }
  50. // SetDefaultPasswordHashAlgorithm will take a provided algorithmName and de-alias it to
  51. // a complete algorithm specification.
  52. func SetDefaultPasswordHashAlgorithm(algorithmName string) (string, *PasswordHashAlgorithm) {
  53. algoSpec := hashAlgorithmToSpec(algorithmName)
  54. // now we get a full specification, e.g. pbkdf2$50000$50 rather than pbdkf2
  55. DefaultHashAlgorithm = Parse(algoSpec)
  56. return algoSpec, DefaultHashAlgorithm
  57. }
  58. // ConfigHashAlgorithm will try to find a "recommended algorithm name" defined by RecommendedHashAlgorithms for config
  59. // This function is not fast and is only used for the installation page
  60. func ConfigHashAlgorithm(algorithm string) string {
  61. algorithm = hashAlgorithmToSpec(algorithm)
  62. for _, recommAlgo := range RecommendedHashAlgorithms {
  63. if algorithm == hashAlgorithmToSpec(recommAlgo) {
  64. return recommAlgo
  65. }
  66. }
  67. return algorithm
  68. }