Guest User

Untitled

a guest
Jul 15th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. require 'digest/md5'
  2. def crypt_private(password, setting)
  3. output = '*0'
  4. itoa64 = './0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
  5. output = '*1' if setting[0..1] == output
  6. return output if setting[0..2] != '$P$'
  7. salt = setting[4..11]
  8. return output if salt.length != 8
  9.  
  10. count_log2 = itoa64.index(setting[3])
  11. return output if count_log2 < 7 || count_log2 > 30
  12. count = 1 << count_log2
  13. hash = Digest::MD5.digest("#{salt}#{password}");
  14. count.times do |i|
  15. hash = Digest::MD5.digest("#{hash}#{password}");
  16. end
  17. output = setting[0..11]
  18. output += b64encode(hash, 16, itoa64)
  19. return output == setting
  20. end
  21.  
  22. def b64encode(input, count, itoa64)
  23. output = '';
  24. i = 0;
  25. ary = itoa64.split(//)
  26. while (i < count)
  27. value = input[i];
  28. i += 1
  29. output += ary[value & 0x3f];
  30. value |= input[i] << 8 if i < count
  31. output += ary[(value >> 6) & 0x3f]
  32. i += 1
  33. return output if i >= count
  34. value |= input[i] << 16 if i < count
  35. output += ary[(value >> 12) & 0x3f]
  36. i += 1
  37. return output if i >= count
  38. output += ary[(value >> 18) & 0x3f]
  39. end
  40. return output
  41. end
  42.  
  43. puts crypt_private("test-password", "$P$Bd68Jgt9eSsoeqqq9uOSviaJxBUBy./")
Add Comment
Please, Sign In to add comment