zoukankan      html  css  js  c++  java
  • ASP.NET.Identity 加密算法

            public static string HashPassword(string password)
            {
                if (password == null)
                {
                    throw new ArgumentNullException("password");
                }
                byte[] salt;
                byte[] bytes;
                using (Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, 16, 1000))
                {
                    salt = rfc2898DeriveBytes.Salt;
                    bytes = rfc2898DeriveBytes.GetBytes(32);
                }
                byte[] array = new byte[49];
                Buffer.BlockCopy(salt, 0, array, 1, 16);
                Buffer.BlockCopy(bytes, 0, array, 17, 32);
                return Convert.ToBase64String(array);
            }
    
            public static bool VerifyHashedPassword(string hashedPassword, string password)
            {
                if (hashedPassword == null)
                {
                    return false;
                }
                if (password == null)
                {
                    throw new ArgumentNullException("password");
                }
                byte[] array = Convert.FromBase64String(hashedPassword);
                if (array.Length != 49 || array[0] != 0)
                {
                    return false;
                }
                byte[] array2 = new byte[16];
                Buffer.BlockCopy(array, 1, array2, 0, 16);
                byte[] array3 = new byte[32];
                Buffer.BlockCopy(array, 17, array3, 0, 32);
                byte[] bytes;
                using (Rfc2898DeriveBytes rfc2898DeriveBytes = new Rfc2898DeriveBytes(password, array2, 1000))
                {
                    bytes = rfc2898DeriveBytes.GetBytes(32);
                }
                return ByteArraysEqual(array3, bytes);
            }
    
            private static bool ByteArraysEqual(byte[] a, byte[] b)
            {
                if (object.ReferenceEquals(a, b))
                {
                    return true;
                }
                if (a == null || b == null || a.Length != b.Length)
                {
                    return false;
                }
                bool flag = true;
                for (int i = 0; i < a.Length; i++)
                {
                    flag &= (a[i] == b[i]);
                }
                return flag;
            }
     
    

      

  • 相关阅读:
    线段树区间异或--差分时间复杂度优化
    数独
    sql语句-根据动态参数去拼sql
    Python-读取文件的大小
    Docker-教你如何通过 Docker 快速搭建各种测试环境
    Docker-本地镜像发布到阿里云
    Docker- Mysql数据库主从同步配置方法
    mysql-如何删除主从同步
    Docker数据卷的介绍和使用
    Docker镜像-拉取并且运行
  • 原文地址:https://www.cnblogs.com/anech/p/3818844.html
Copyright © 2011-2022 走看看