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