6 php encryption ruby-on-rails devise
我正在将Ruby on Rails的网站更新为PHP.我需要生成由Ruby on Rails中的Devise Gem生成的密码.我必须知道使用PHP创建相同方法的密码的哈希方法是什么.但是作为初学者,在Ruby on Rails中找到代码并不容易.如果有人知道我应该在哪里找到它,请帮助我.
这两个都是我发现的:
1) The configuration of encryptor is disabled in devise.rb like below:
# config.encryptor = :sha1
2) I read the comments very carefully then I found that they using sha512 and bcrypt as default encryptor.
# (default), :sha512 and :bcrypt. Devise also supports encryptors from others
Run Code Online (Sandbox Code Playgroud)
我尝试用PHP以不同的方式创建相同的加密密码:
1) sha1('--'.$password_salt.'--'.$encrypted_password);
2) sha1($password_salt.'-----'.$encrypted_password);
3) sha1('--'.$password_salt.'--'.$encrypted_password.'--');
4) sha1($password_salt.$encrypted_password);
5) sha1($encrypted_password.$password_salt);
6) substr(hash('sha512', $password_salt.$encrypted_password, false), 20);
7) substr(hash('sha512', $encrypted_password.$password_salt, false), 0, 40);
8) hash('sha512', $encrypted_password.$password_salt, false);
9) hash('sha512', $password_salt.$encrypted_password, false);
10) substr(hash('sha512', '--'.$password_salt.'--'.$encrypted_password.'--', false), 0, 40);
Run Code Online (Sandbox Code Playgroud)
我无法从上面的任何一个得到相同的结果.有没有人可以告诉我Devise Gem的加密方法?
帮我!!!
PS.我英语说的不好.即使我的英语不正确,也请不要生气.
我在回答自己:
加密器是Sha1
我一直在寻找的文件夹"\ CONFIG \初始化"的加密是commanted作为唯一的"devise.rb""#config.encryptor =:SHA1"不过还有一个"devise.rb"红宝石lib文件夹里面,"\ Ruby191\lib\ruby\gems\1.9.1\gems\devise-1.0.8\lib\devise.rb"还有一个配置为"@@ encryptor =:sha1"
使用SHA1当你去下面的文件,你会看到算法代码加密方法:\ Ruby191\LIB \红宝石\宝石\ 1.9.1 \宝石\设计-1.0.8\LIB \色器件\加密机\ sha1.rb
需要"digest/sha1"
模块设计模块加密器#= Sha1#使用Sha1哈希算法加密密码.class Sha1 <Base
# Gererates a default password digest based on stretches, salt, pepper and the
# incoming password.
def self.digest(password, stretches, salt, pepper)
digest = pepper
stretches.times { digest = self.secure_digest(salt, digest, password, pepper) }
digest
end
private
# Generate a SHA1 digest joining args. Generated token is something like
# --arg1--arg2--arg3--argN--
def self.secure_digest(*tokens)
::Digest::SHA1.hexdigest('--' << tokens.flatten.join('--') << '--')
end
end
end
Run Code Online (Sandbox Code Playgroud)
结束
所以我翻译成了PHP
function encrypt_password($salt, $password) {
$pepper = '';
$digest = $pepper;
$stretches = 10;
for ($i=0; $i<$stretches; $i++) {
$join = '--'.$salt.'--'.$digest.'--'.$password.'--'.$pepper.'--';
$digest = Sha1($join);
}
$result = substr($digest, 0, 40);
return $result;
}
Run Code Online (Sandbox Code Playgroud)
它工作得很好:-)
根据 Devise Gem 的消息来源,该方法有点复杂。SHA512 部分看起来像这样:
function sha512_digest($password, $stretches, $salt, $pepper)
{
$digest = $pepper;
for ($i = 0; $i < $stretches; $i++)
{
$digest = hash('sha512', '--' . $salt . '--' . $digest . '--' . $password . '--' . $pepper . '--');
}
}
Run Code Online (Sandbox Code Playgroud)
BCrypt 部分对我来说有点不清楚,到目前为止我唯一知道的是它是 Blowfish 加密。