Created
March 20, 2014 13:16
-
-
Save stuartbates/9663498 to your computer and use it in GitHub Desktop.
Ruby implementation of PHPass to allow legacy passwords from Wordpress to Devise
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class PHPass | |
def self.itoa64 | |
'./0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz' | |
end | |
def self.crypt_private(password, setting) | |
output = '*0' | |
if setting[0,2] == output | |
output = '*1' | |
end | |
id = setting[0,3] | |
# We use "P", phpBB3 uses "H" for the same thing | |
if (id != '$P$' && id != '$H$') | |
return output | |
end | |
count_log2 = self.itoa64.index(setting[3]) | |
if (count_log2 < 7 || count_log2 > 30) | |
return output | |
end | |
count = 1 << count_log2 | |
salt = setting[4,8] | |
if salt.length != 8 | |
return output | |
end | |
hash = Digest::MD5.digest(salt + password) | |
while count > 0 | |
hash = Digest::MD5.digest(hash + password) | |
count -= 1 | |
end | |
setting[0,12] + self.encode64(hash, 16) | |
end | |
def self.encode64(input, count) | |
out = '' | |
cur = 0 | |
while cur < count | |
value = input[cur].ord | |
cur += 1 | |
out << self.itoa64[value & 0x3f] | |
if cur < count | |
value |= input[cur].ord << 8 | |
end | |
out << self.itoa64[(value >> 6) & 0x3f] | |
break if cur >= count | |
cur += 1 | |
if cur < count | |
value |= input[cur].ord << 16 | |
end | |
out << self.itoa64[(value >> 12) & 0x3f] | |
break if cur >= count | |
cur += 1 | |
out << self.itoa64[(value >> 18) & 0x3f] | |
end | |
out | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi,
Could you explain how to use this code, please?
Thanks