Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'digest/md5'
- def crypt_private(password, setting)
- output = '*0'
- itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
- output = '*1' if setting[0..1] == output
- return output if setting[0..2] != '$P$'
- salt = setting[4..11]
- return output if salt.length != 8
- count_log2 = itoa64.index(setting[3])
- return output if count_log2 < 7 || count_log2 > 30
- count = 1 << count_log2
- hash = Digest::MD5.digest("#{salt}#{password}");
- count.times do |i|
- hash = Digest::MD5.digest("#{hash}#{password}");
- end
- output = setting[0..11]
- output += b64encode(hash, 16, itoa64)
- return output == setting
- end
- def b64encode(input, count, itoa64)
- output = '';
- i = 0;
- ary = itoa64.split(//)
- while (i < count)
- value = input[i];
- i += 1
- output += ary[value & 0x3f];
- value |= input[i] << 8 if i < count
- output += ary[(value >> 6) & 0x3f]
- i += 1
- return output if i >= count
- value |= input[i] << 16 if i < count
- output += ary[(value >> 12) & 0x3f]
- i += 1
- return output if i >= count
- output += ary[(value >> 18) & 0x3f]
- end
- return output
- end
- puts crypt_private("test-password", "$P$Bd68Jgt9eSsoeqqq9uOSviaJxBUBy./")
Add Comment
Please, Sign In to add comment