Skip to content

Use Base64.strict_encode64 and SSHA256 #303

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 29, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
adds a SSHA256 type and uses strict_encode64
Base64.encode64 adds \n every 60 encoded chars. This was originally an encoding mechanism for sending binary
content in e-mail, where the line length is limited. For passwords we dont want this.
cf https://stackoverflow.com/questions/2620975/strange-n-in-base64-encoded-string-in-ruby
  • Loading branch information
bamthomas committed Apr 16, 2018
commit a07710c6446e2b1d00cdc3c6ae881bc60aa3a2f7
10 changes: 7 additions & 3 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 All @@ -23,12 +24,15 @@ class << self
def generate(type, str)
case type
when :md5
attribute_value = '{MD5}' + Base64.encode64(Digest::MD5.digest(str)).chomp!
attribute_value = '{MD5}' + Base64.strict_encode64(Digest::MD5.digest(str))
when :sha
attribute_value = '{SHA}' + Base64.encode64(Digest::SHA1.digest(str)).chomp!
attribute_value = '{SHA}' + Base64.strict_encode64(Digest::SHA1.digest(str))
when :ssha
salt = SecureRandom.random_bytes(16)
attribute_value = '{SSHA}' + Base64.encode64(Digest::SHA1.digest(str + salt) + salt).chomp!
attribute_value = '{SSHA}' + Base64.strict_encode64(Digest::SHA1.digest(str + salt) + salt)
when :ssha256
salt = SecureRandom.random_bytes(16)
attribute_value = '{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