password_hash.rb 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
  2. module PasswordHash
  3. include ApplicationLib
  4. extend self
  5. def crypt(password)
  6. argon2.create(password)
  7. end
  8. def verified?(pw_hash, password)
  9. Argon2::Password.verify_password(password, pw_hash, secret)
  10. rescue
  11. false
  12. end
  13. def crypted?(pw_hash)
  14. return false if !pw_hash
  15. return true if hashed_argon2?(pw_hash)
  16. return true if hashed_sha2?(pw_hash)
  17. false
  18. end
  19. def legacy?(pw_hash, password)
  20. return false if pw_hash.blank?
  21. return false if !password
  22. sha2?(pw_hash, password)
  23. end
  24. def hashed_sha2?(pw_hash)
  25. pw_hash.start_with?('{sha2}')
  26. end
  27. def hashed_argon2?(pw_hash)
  28. # taken from: https://github.com/technion/ruby-argon2/blob/7e1f4a2634316e370ab84150e4f5fd91d9263713/lib/argon2.rb#L33
  29. pw_hash =~ /^\$argon2i\$.{,112}/
  30. end
  31. def sha2(password)
  32. crypted = Digest::SHA2.hexdigest(password)
  33. "{sha2}#{crypted}"
  34. end
  35. private
  36. def sha2?(pw_hash, password)
  37. return false if !hashed_sha2?(pw_hash)
  38. pw_hash == sha2(password)
  39. end
  40. def argon2
  41. @argon2 ||= Argon2::Password.new(secret: secret)
  42. end
  43. def secret
  44. @secret ||= Setting.get('application_secret')
  45. end
  46. end