First of all, I read this Password Hashing using SHA256 and .NET / Node.js , and that didn't help me.
I need to check the password hashes created in ASP.NET in node.js. I was told that passwords are generated using this algorithm: What is the default hash algorithm that uses ASP.NET membership? .
I have an example of a password and salt hash (the first line is the password, and the second line is the salt):
"Password": "jj/rf7OxXM263rPgvLan4M6Is7o=", "PasswordSalt": "/Eju9rmaJp03e3+z1v5s+A==",
I know that the hash algorithm is SHA1 , and I know that this hash is generated for input test123 . However, I cannot reproduce the hash algorithm to get the same hash for this input. What I tried:
Password = "jj/rf7OxXM263rPgvLan4M6Is7o=" PasswordSalt = "/Eju9rmaJp03e3+z1v5s+A==" crypto = require("crypto") sha1 = crypto.createHash("sha1") PasswordSalt = new Buffer(PasswordSalt, 'base64').toString('utf8') sha1.update(PasswordSalt+"test123", "utf8") result = sha1.digest("base64") console.log(Password) console.log(result)
Result:
jj/rf7OxXM263rPgvLan4M6Is7o= xIjxRod4+HVYzlHZ9xomGGGY6d8=
I managed to get a working C # algorithm:
using System.IO; using System; using System.Text; using System.Security.Cryptography; class Program { static string EncodePassword(string pass, string salt) { byte[] bytes = Encoding.Unicode.GetBytes(pass); byte[] src = Convert.FromBase64String(salt); 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); } static void Main() { string pass = "test123"; string salt = "/Eju9rmaJp03e3+z1v5s+A=="; string hash = Program.EncodePassword(pass,salt); Console.WriteLine(hash);
So now it is a matter of porting this algorithm to node.js. The problem is that C # somehow magically works with bytes, and I don't know how to do this in node. Consider the following code (it does not use salt - it simply creates base64 sha1 from the password:
crypto = require("crypto") pass = 'test123' sha1 = crypto.createHash("sha1") buf = new Buffer( pass, 'utf8') sha1.update(buf) result = sha1.digest("base64") console.log(result)
And in C #
using System.Text; using System.Security.Cryptography; string pass = "test123"; byte[] bytes = Encoding.Unicode.GetBytes(pass); HashAlgorithm algorithm = HashAlgorithm.Create("SHA1"); byte[] inArray = algorithm.ComputeHash(bytes); string hash = Convert.ToBase64String(inArray); Console.WriteLine(hash);
I need code in node.js that will return the same value as the code in C #. Any ideas?