Skip to content

Instantly share code, notes, and snippets.

@stuartbates
Created March 20, 2014 13:16
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stuartbates/9663498 to your computer and use it in GitHub Desktop.
Save stuartbates/9663498 to your computer and use it in GitHub Desktop.
Ruby implementation of PHPass to allow legacy passwords from Wordpress to Devise
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
@Billy4195
Copy link

Hi,
Could you explain how to use this code, please?
Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment