Skip to content

Commit

Permalink
Merge pull request #303 from ICIJ/master
Browse files Browse the repository at this point in the history
Use Base64.strict_encode64 and SSHA256
  • Loading branch information
HarlemSquirrel committed Aug 29, 2020
2 parents 58b4a85 + 2eeab2b commit 1792608
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/net/ldap/password.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- ruby encoding: utf-8 -*-
require 'digest/sha1'
require 'digest/sha2'
require 'digest/md5'
require 'base64'
require 'securerandom'
Expand Down Expand Up @@ -28,6 +29,9 @@ def generate(type, str)
when :ssha
salt = SecureRandom.random_bytes(16)
'{SSHA}' + Base64.strict_encode64(Digest::SHA1.digest(str + salt) + salt)
when :ssha256
salt = SecureRandom.random_bytes(16)
'{SSHA256}' + Base64.strict_encode64(Digest::SHA256.digest(str + salt) + salt)
else
raise Net::LDAP::HashTypeUnsupportedError, "Unsupported password-hash type (#{type})"
end
Expand Down
5 changes: 5 additions & 0 deletions test/test_password.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,9 @@ def test_psw
assert_equal("{MD5}xq8jwrcfibi0sZdZYNkSng==", Net::LDAP::Password.generate(:md5, "cashflow"))
assert_equal("{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=", Net::LDAP::Password.generate(:sha, "cashflow"))
end

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
end

0 comments on commit 1792608

Please sign in to comment.