Skip to content

Commit 6f2d527

Browse files
author
blackhedd
committed
Added a password-hash generator.
1 parent bf9e230 commit 6f2d527

File tree

4 files changed

+92
-0
lines changed

4 files changed

+92
-0
lines changed

lib/net/ldap.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
require 'net/ldap/pdu'
2323
require 'net/ldap/filter'
2424
require 'net/ldap/dataset'
25+
require 'net/ldap/psw'
2526

2627

2728
module Net

lib/net/ldap/psw.rb

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
# $Id$
2+
#
3+
#
4+
#----------------------------------------------------------------------------
5+
#
6+
# Copyright (C) 2006 by Francis Cianfrocca. All Rights Reserved.
7+
#
8+
# Gmail: garbagecat10
9+
#
10+
# This program is free software; you can redistribute it and/or modify
11+
# it under the terms of the GNU General Public License as published by
12+
# the Free Software Foundation; either version 2 of the License, or
13+
# (at your option) any later version.
14+
#
15+
# This program is distributed in the hope that it will be useful,
16+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
17+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18+
# GNU General Public License for more details.
19+
#
20+
# You should have received a copy of the GNU General Public License
21+
# along with this program; if not, write to the Free Software
22+
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23+
#
24+
#---------------------------------------------------------------------------
25+
#
26+
#
27+
28+
29+
module Net
30+
class LDAP
31+
32+
33+
class Password
34+
class << self
35+
36+
# Generate a password-hash suitable for inclusion in an LDAP attribute.
37+
# STUB: This is here to fulfill the requirements of an RFC, which one?
38+
# TODO, gotta do salted-sha and (maybe) salted-md5.
39+
# Should we provide sha1 as a synonym for sha1? I vote no because then
40+
# should you also provide ssha1 for symmetry?
41+
def generate( type, str )
42+
case type
43+
when :md5
44+
require 'md5'
45+
"{MD5}#{ [MD5.new( str.to_s ).digest].pack("m").chomp }"
46+
when :sha
47+
require 'sha1'
48+
"{SHA}#{ [SHA1.new( str.to_s ).digest].pack("m").chomp }"
49+
# when ssha
50+
else
51+
raise Net::LDAP::LdapError.new( "unsupported password-hash type (#{type})" )
52+
end
53+
end
54+
55+
end
56+
end
57+
58+
59+
end # class LDAP
60+
end # module Net
61+
62+

tests/testem.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@
66
require 'tests/testber'
77
require 'tests/testldif'
88
require 'tests/testldap'
9+
require 'tests/testpsw'
910

1011

tests/testpsw.rb

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# $Id$
2+
#
3+
#
4+
5+
6+
$:.unshift "lib"
7+
8+
require 'net/ldap'
9+
require 'stringio'
10+
11+
12+
class TestPassword < Test::Unit::TestCase
13+
14+
def setup
15+
end
16+
17+
18+
def test_psw
19+
assert_equal( "{MD5}xq8jwrcfibi0sZdZYNkSng==", Net::LDAP::Password.generate( :md5, "cashflow" ))
20+
assert_equal( "{SHA}YE4eGkN4BvwNN1f5R7CZz0kFn14=", Net::LDAP::Password.generate( :sha, "cashflow" ))
21+
end
22+
23+
24+
25+
26+
end
27+
28+

0 commit comments

Comments
 (0)