Skip to content

Commit

Permalink
Add support for UTF-8 encoded passwords when using the hash types :ss…
Browse files Browse the repository at this point in the history
…ha and ssha256
  • Loading branch information
Frank Walentowski committed Oct 8, 2024
1 parent 7f060e1 commit ed85aff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
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

0 comments on commit ed85aff

Please sign in to comment.