Skip to content

Instantly share code, notes, and snippets.

@goofmint
Created November 24, 2010 12:34
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save goofmint/713582 to your computer and use it in GitHub Desktop.
Save goofmint/713582 to your computer and use it in GitHub Desktop.
wp-includes/class-phpass.phpのパスワード生成部分をRubyで再実装したもの。WordPressの認証をRubyから直接利用した時に。
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./")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment