2017-01-27 08:17:03 +00:00
|
|
|
# Copyright (C) 2012-2016 Zammad Foundation, http://zammad-foundation.org/
|
|
|
|
|
|
|
|
module PasswordHash
|
|
|
|
include ApplicationLib
|
|
|
|
|
2019-01-01 16:54:13 +00:00
|
|
|
extend self
|
2017-01-27 08:17:03 +00:00
|
|
|
|
|
|
|
def crypt(password)
|
|
|
|
argon2.create(password)
|
|
|
|
end
|
|
|
|
|
|
|
|
def verified?(pw_hash, password)
|
|
|
|
Argon2::Password.verify_password(password, pw_hash, secret)
|
|
|
|
rescue
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
def crypted?(pw_hash)
|
2017-05-08 10:04:54 +00:00
|
|
|
return false if !pw_hash
|
|
|
|
return true if hashed_argon2?(pw_hash)
|
|
|
|
return true if hashed_sha2?(pw_hash)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-05-08 10:04:54 +00:00
|
|
|
false
|
2017-01-27 08:17:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def legacy?(pw_hash, password)
|
2017-05-08 10:04:54 +00:00
|
|
|
return false if pw_hash.blank?
|
|
|
|
return false if !password
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-05-08 10:04:54 +00:00
|
|
|
sha2?(pw_hash, password)
|
2017-01-27 08:17:03 +00:00
|
|
|
end
|
|
|
|
|
2017-05-08 10:04:54 +00:00
|
|
|
def hashed_sha2?(pw_hash)
|
|
|
|
pw_hash.start_with?('{sha2}')
|
|
|
|
end
|
2017-01-27 08:17:03 +00:00
|
|
|
|
2017-05-08 10:04:54 +00:00
|
|
|
def hashed_argon2?(pw_hash)
|
|
|
|
# taken from: https://github.com/technion/ruby-argon2/blob/7e1f4a2634316e370ab84150e4f5fd91d9263713/lib/argon2.rb#L33
|
|
|
|
pw_hash =~ /^\$argon2i\$.{,112}/
|
|
|
|
end
|
|
|
|
|
|
|
|
def sha2(password)
|
2017-01-27 08:17:03 +00:00
|
|
|
crypted = Digest::SHA2.hexdigest(password)
|
2017-05-08 10:04:54 +00:00
|
|
|
"{sha2}#{crypted}"
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def sha2?(pw_hash, password)
|
|
|
|
return false if !hashed_sha2?(pw_hash)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-05-08 10:04:54 +00:00
|
|
|
pw_hash == sha2(password)
|
2017-01-27 08:17:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def argon2
|
2018-09-28 12:14:46 +00:00
|
|
|
@argon2 ||= Argon2::Password.new(secret: secret)
|
2017-01-27 08:17:03 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def secret
|
2018-09-28 12:14:46 +00:00
|
|
|
@secret ||= Setting.get('application_secret')
|
2017-01-27 08:17:03 +00:00
|
|
|
end
|
|
|
|
end
|