Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions lib/net/ldap/password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ def generate(type, str)
'{SHA}' + Base64.strict_encode64(Digest::SHA1.digest(str))
when :ssha
salt = SecureRandom.random_bytes(16)
'{SSHA}' + Base64.strict_encode64(Digest::SHA1.digest(str + salt) + salt)
digest = Digest::SHA1.new
digest << str << salt
'{SSHA}' + Base64.strict_encode64(digest.digest + salt)
when :ssha256
salt = SecureRandom.random_bytes(16)
'{SSHA256}' + Base64.strict_encode64(Digest::SHA256.digest(str + salt) + salt)
digest = Digest::SHA256.new
digest << str << salt
'{SSHA256}' + Base64.strict_encode64(digest.digest + salt)
else
raise Net::LDAP::HashTypeUnsupportedError, "Unsupported password-hash type (#{type})"
end
Expand Down
7 changes: 7 additions & 0 deletions test/test_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,11 @@ def test_psw_with_ssha256_should_not_contain_linefeed
flexmock(SecureRandom).should_receive(:random_bytes).and_return('\xE5\x8A\x99\xF8\xCB\x15GW\xE8\xEA\xAD\x0F\xBF\x95\xB0\xDC')
assert_equal("{SSHA256}Cc7MXboTyUP5PnPAeJeCrgMy8+7Gus0sw7kBJuTrmf1ceEU1XHg4QVx4OTlceEY4XHhDQlx4MTVHV1x4RThceEVBXHhBRFx4MEZceEJGXHg5NVx4QjBceERD", Net::LDAP::Password.generate(:ssha256, "cashflow"))
end

def test_utf8_psw
flexmock(SecureRandom).should_receive(:random_bytes).and_return('\xE5\x8A\x99\xF8\xCB\x15GW\xE8\xEA\xAD\x0F\xBF\x95\xB0\xDC')
utf8_psw = "iHVh©NjrLR§h!cru"
assert_equal("{SSHA}shzNiWgSPr3DoDm+Re7QPCcu1g1ceEU1XHg4QVx4OTlceEY4XHhDQlx4MTVHV1x4RThceEVBXHhBRFx4MEZceEJGXHg5NVx4QjBceERD", Net::LDAP::Password.generate(:ssha, utf8_psw))
assert_equal("{SSHA256}/aS06GodUyRYx+z436t+WZsH2aQCSac9FY4ewaXzhSNceEU1XHg4QVx4OTlceEY4XHhDQlx4MTVHV1x4RThceEVBXHhBRFx4MEZceEJGXHg5NVx4QjBceERD", Net::LDAP::Password.generate(:ssha256, utf8_psw))
end
end