在Ruby中重新实现ASP.NET成员资格和用户密码哈希

前端之家收集整理的这篇文章主要介绍了在Ruby中重新实现ASP.NET成员资格和用户密码哈希前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个庞大的用户数据库(~200,000),我正在从ASP.NET应用程序转移到 Ruby on Rails应用程序.我真的不想让每个用户重置他们的密码,所以我试图在Ruby中重新实现C#密码散列函数.

功能是这样的:

  1. public string EncodePassword(string pass,string saltBase64)
  2. {
  3. byte[] bytes = Encoding.Unicode.GetBytes(pass);
  4. byte[] src = Convert.FromBase64String(saltBase64);
  5. byte[] dst = new byte[src.Length + bytes.Length];
  6. Buffer.BlockCopy(src,dst,src.Length);
  7. Buffer.BlockCopy(bytes,src.Length,bytes.Length);
  8. HashAlgorithm algorithm = HashAlgorithm.Create("SHA1");
  9. byte[] inArray = algorithm.ComputeHash(dst);
  10. return Convert.ToBase64String(inArray);
  11. }

示例哈希密码和salt是(并且使用的密码是“password”):

哈希密码:“weEWx4rhyPtd3kec7usysxf7kpk =”
盐:“1ptFxHq7ALe7yXIQDdzQ9Q ==”
密码:“密码”

现在使用以下Ruby代码

  1. require "base64"
  2. require "digest/sha1"
  3.  
  4.  
  5. password = "password"
  6. salt = "1ptFxHq7ALe7yXIQDdzQ9Q=="
  7.  
  8. concat = salt+password
  9.  
  10. sha1 = Digest::SHA1.digest(concat)
  11.  
  12. encoded = Base64.encode64(sha1)
  13.  
  14. puts encoded

我没有得到正确的密码哈希(我得到“BsdioBN / Vh2U7qWG4e O13h3iQ =”而不是“weEWx4rhyPtd3kec7usysxf7kpk =”).任何人都可以看到问题可能是什么?

非常感谢

Arfon

解决方法

只是一个快速更新,我的一位同事解决了这个问题:
  1. require "base64"
  2. require "digest"
  3. require "jcode"
  4.  
  5.  
  6. def encode_password(password,salt)
  7. bytes = ""
  8. password.each_char { |c| bytes += c + "\x00" }
  9. salty = Base64.decode64(salt)
  10. concat = salty+bytes
  11. sha1 = Digest::SHA1.digest(concat)
  12. encoded = Base64.encode64(sha1).strip()
  13. puts encoded
  14. end

猜你在找的Ruby相关文章