Search code examples
hashowinauth0usermanager

OWIN password hash manual comparison from JavaScript


I need to use an existing ASP.NET OWIN database with Auth0. A problem that I've encountered is that I need to write a function in JavaScript (for the Auth0 Action Scripts) that takes a plaintext password, hashes it and compares that hash to an existing hash from the OWIN database. The hashes are created using ASP.NET Identity UserManager class.

How can I replicate the algorithm that UserManager uses in JavaScript?

Thanks in advance, Arthur.


Solution

  • Found the answer. You can use this C# code snippet that from node using edge to has the password like UserManager does it in ASP.NET Identity.

    var edge = require('edge');
    
      var verifyHash = edge.func(function() {/*
        using System;
        using System.Runtime.CompilerServices;
        using System.Security.Cryptography;
        using System.Threading.Tasks;
        using System.Runtime.InteropServices;
        using System.Security.Principal;
    
        internal static class Crypto
        {
            private const int PBKDF2IterCount = 1000; // default for Rfc2898DeriveBytes
            private const int PBKDF2SubkeyLength = 256/8; // 256 bits
            private const int SaltSize = 128/8; // 128 bits
    
            public static string HashPassword(string password)
            {
                if (password == null)
                {
                    throw new ArgumentNullException("password");
                }
    
                // Produce a version 0 (see comment above) text hash.
                byte[] salt;
                byte[] subkey;
                using (var deriveBytes = new Rfc2898DeriveBytes(password, SaltSize, PBKDF2IterCount))
                {
                    salt = deriveBytes.Salt;
                    subkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);
                }
    
                var outputBytes = new byte[1 + SaltSize + PBKDF2SubkeyLength];
                Buffer.BlockCopy(salt, 0, outputBytes, 1, SaltSize);
                Buffer.BlockCopy(subkey, 0, outputBytes, 1 + SaltSize, PBKDF2SubkeyLength);
                return Convert.ToBase64String(outputBytes);
            }
    
            // hashedPassword must be of the format of HashWithPassword (salt + Hash(salt+input)
            public static bool VerifyHashedPassword(string hashedPassword, string password)
            {
                if (hashedPassword == null)
                {
                    return false;
                }
                if (password == null)
                {
                    throw new ArgumentNullException("password");
                }
    
                var hashedPasswordBytes = Convert.FromBase64String(hashedPassword);
    
                // Verify a version 0 (see comment above) text hash.
    
                if (hashedPasswordBytes.Length != (1 + SaltSize + PBKDF2SubkeyLength) || hashedPasswordBytes[0] != 0x00)
                {
                    // Wrong length or version header.
                    return false;
                }
    
                var salt = new byte[SaltSize];
                Buffer.BlockCopy(hashedPasswordBytes, 1, salt, 0, SaltSize);
                var storedSubkey = new byte[PBKDF2SubkeyLength];
                Buffer.BlockCopy(hashedPasswordBytes, 1 + SaltSize, storedSubkey, 0, PBKDF2SubkeyLength);
    
                byte[] generatedSubkey;
                using (var deriveBytes = new Rfc2898DeriveBytes(password, salt, PBKDF2IterCount))
                {
                    generatedSubkey = deriveBytes.GetBytes(PBKDF2SubkeyLength);
                }
                return ByteArraysEqual(storedSubkey, generatedSubkey);
            }
    
            // Compares two byte arrays for equality. The method is specifically written so that the loop is not optimized.
            [MethodImpl(MethodImplOptions.NoOptimization)]
            private static bool ByteArraysEqual(byte[] a, byte[] b)
            {
                if (ReferenceEquals(a, b))
                {
                    return true;
                }
    
                if (a == null || b == null || a.Length != b.Length)
                {
                    return false;
                }
    
                var areSame = true;
                for (var i = 0; i < a.Length; i++)
                {
                    areSame &= (a[i] == b[i]);
                }
                return areSame;
            }
        }
    
        class Startup
        {
            public async Task<object> Invoke(dynamic input)
            {
                return await Task<object>.Run(() => { 
                    return Task.FromResult<object>(Crypto.VerifyHashedPassword (input.hashedPassword, input.providedPassword));
                });
            }
        }
    */});
    
      verifyHash({ hashedPassword: 'AONmKUXiPKhN1J+t8DwCp2uTq3TBcJNabcc4HuQkx+uwO+5+yRJZreJ9jZyxyGzhzg==', providedPassword: 'abc' }, function (error, result) {
        if (error) {
          console.log(error);
        }
        else {
          if (result) {
            // Password matches!
          }
        }
      });