I have a large user database (~ 200,000) that I am moving from an ASP.NET application to a Ruby on Rails application. I really do not want to ask each user to reset their password, and therefore I am trying to re-implement the C # password hashing function in Ruby.
The old function is this:
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); }
An example of a hashed password and salt (and the password was "password"):
Hashed password: "weEWx4rhyPtd3kec7usysxf7kpk =" Salt: "1ptFxHq7ALe7yXIQDdzQ9Q ==" Password: "password"
Now with the following Ruby code:
require "base64" require "digest/sha1" password = "password" salt = "1ptFxHq7ALe7yXIQDdzQ9Q==" concat = salt+password sha1 = Digest::SHA1.digest(concat) encoded = Base64.encode64(sha1) puts encoded
I do not get the correct password hash (I get "+ BsdIOBN / Vh2U7qWG4e + O13h3iQ =" instead of "weEWx4rhyPtd3kec7usysxf7kpk ="). Can anyone understand what could be the problem?
Thank you very much
Arfon
arfon Feb 09 '09 at 22:52 2009-02-09 22:52
source share