arf*_*fon 8 ruby asp.net base64 sha1 ruby-on-rails
我有一个庞大的用户数据库(~200,000),我正在从ASP.NET应用程序转移到Ruby on Rails应用程序.我真的不想要求每个用户重置密码,所以我试图在Ruby中重新实现C#密码散列函数.
旧功能是这样的:
public string EncodePassword(string pass, string saltBase64)
{
byte[] bytes = Encoding.Unicode.GetBytes(pass);
byte[] src = Convert.FromBase64String(saltBase64);
byte[] dst = new byte[src.Length + bytes.Length];
Buffer.BlockCopy(src, 0, dst, 0, src.Length);
Buffer.BlockCopy(bytes, 0, dst, src.Length, bytes.Length);
HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
byte[] inArray = algorithm.ComputeHash(dst);
return Convert.ToBase64String(inArray);
}
Run Code Online (Sandbox Code Playgroud)
示例哈希密码和盐是(并且使用的密码是"密码"):
哈希密码:"weEWx4rhyPtd3kec7usysxf7kpk ="盐:"1ptFxHq7ALe7yXIQDdzQ9Q =="密码:"密码"
现在使用以下Ruby代码:
require "base64"
require "digest/sha1"
password = "password"
salt = "1ptFxHq7ALe7yXIQDdzQ9Q=="
concat = salt+password
sha1 = Digest::SHA1.digest(concat)
encoded = Base64.encode64(sha1)
puts encoded
Run Code Online (Sandbox Code Playgroud)
我没有得到正确的密码哈希(我得到"+ BsdIOBN/Vh2U7qWG4e + O13h3iQ ="而不是"weEWx4rhyPtd3kec7usysxf7kpk =").任何人都可以看到问题可能是什么?
非常感谢
Arfon
只是一个快速更新,我的一位同事解决了这个问题:
require "base64"
require "digest"
require "jcode"
def encode_password(password, salt)
bytes = ""
password.each_char { |c| bytes += c + "\x00" }
salty = Base64.decode64(salt)
concat = salty+bytes
sha1 = Digest::SHA1.digest(concat)
encoded = Base64.encode64(sha1).strip()
puts encoded
end
Run Code Online (Sandbox Code Playgroud)