diff --git a/lib/net/ldap/password.rb b/lib/net/ldap/password.rb index 76079338..9a6658ed 100644 --- a/lib/net/ldap/password.rb +++ b/lib/net/ldap/password.rb @@ -1,5 +1,6 @@ # -*- ruby encoding: utf-8 -*- require 'digest/sha1' +require 'digest/sha2' require 'digest/md5' require 'base64' require 'securerandom' @@ -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 diff --git a/test/test_password.rb b/test/test_password.rb index 87b47d91..3ecd8d1b 100644 --- a/test/test_password.rb +++ b/test/test_password.rb @@ -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