PasswordHasher
namespace Microsoft.AspNet.Identity
{
public class PasswordHasher : IPasswordHasher
{
public virtual string HashPassword(string password)
{
return Crypto.HashPassword(password);
} public virtual PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword)
{
if (Crypto.VerifyHashedPassword(hashedPassword, providedPassword))
{
return PasswordVerificationResult.Success;
}
return PasswordVerificationResult.Failed;
}
}
}
using System;
using System.Runtime.CompilerServices;
using System.Security.Cryptography; namespace Microsoft.AspNet.Identity
{
internal static class Crypto
{
private const int PBKDF2IterCount = 1000; private const int PBKDF2SubkeyLength = 32; private const int SaltSize = 16; 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 Crypto.ByteArraysEqual(array3, bytes);
} [MethodImpl(MethodImplOptions.NoOptimization)]
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;
}
}
}
PasswordHasher的更多相关文章
- PasswordHasher 算法
public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string provid ...
- ASP.NET Core 之 Identity 入门(三)
前言 在上一篇文章中,我们学习了 CookieAuthentication 中间件,本篇的话主要看一下 Identity 本身. 最早2005年 ASP.NET 2.0 的时候开始, Web 应用程序 ...
- [转]ASP.NET Core 之 Identity 入门(三)
本文转自:http://www.cnblogs.com/savorboard/p/aspnetcore-identity3.html 前言 在上一篇文章中,我们学习了 CookieAuthentica ...
- Microsoft.AspNet.Identity 自定义使用现有的表—登录实现
Microsoft.AspNet.Identity是微软新引入的一种membership框架,也是微软Owin标准的一个实现.Microsoft.AspNet.Identity.EntityFrame ...
- Migrating an Existing Website from SQL Membership to ASP.NET Identity
Migrating an Existing Website from SQL Membership to ASP.NET Identity public class User : IdentityUs ...
- ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇
在之前的文章中,我为大家介绍了OWIN和Katana,有了对它们的基本了解后,才能更好的去学习ASP.NET Identity,因为它已经对OWIN 有了良好的集成. 在这篇文章中,我主要关注ASP. ...
- 【ASP.NET Identity系列教程(一)】ASP.NET Identity入门
注:本文是[ASP.NET Identity系列教程]的第一篇.本系列教程详细.完整.深入地介绍了微软的ASP.NET Identity技术,描述了如何运用ASP.NET Identity实现应用程序 ...
- 自定义表并实现Identity登录(一)
注意,Microsoft.AspNet.Identity.Core.1.0.0和Microsoft.AspNet.Identity.Core.2.2.1差别太大,需考虑实际项目中用的是哪种,本文是基于 ...
- ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇(转)
ASP.NET MVC 随想录——开始使用ASP.NET Identity,初级篇 阅读目录 ASP.NET Identity 前世今生 建立 ASP.NET Identity 使用ASP.NET ...
随机推荐
- PCA算法是怎么跟协方差矩阵/特征值/特征向量勾搭起来的?
PCA, Principle Component Analysis, 主成份分析, 是使用最广泛的降维算法. ...... (关于PCA的算法步骤和应用场景随便一搜就能找到了, 所以这里就不说了. ) ...
- C#-WinForm-Timer控件
比如在窗体中显示时间: 错误思路一:我在窗体结构函数中写入一个死循环,每隔一秒显示一次当前时间 public Form6() { InitializeComponent(); while (true) ...
- Leetcode 198 House Robber
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- BZOJ1040 [ZJOI2008]骑士
Description Z国的骑士团是一个很有势力的组织,帮会中汇聚了来自各地的精英.他们劫富济贫,惩恶扬善,受到社会各 界的赞扬.最近发生了一件可怕的事情,邪恶的Y国发动了一场针对Z国的侵略战争.战 ...
- SQLServer日期格式化
0 或 100 (*) 默认值 mon dd yyyy hh:miAM(或 PM) 1 101 美国 mm/dd/yyyy ...
- MYSQLDUMP参数详解
mysqldump客户端可用来转储数据库或搜集数据库进行备份或将数据转移到另一个SQL服务器(不一定是一个MySQL服务器).转储包含创建表和/或装载表的SQL语句. 如果你在服务器上进行备份,并且表 ...
- A.Kaw矩阵代数初步学习笔记 6. Gaussian Elimination
“矩阵代数初步”(Introduction to MATRIX ALGEBRA)课程由Prof. A.K.Kaw(University of South Florida)设计并讲授. PDF格式学习笔 ...
- JS 复制对象
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs& ...
- JavaScript 写几个简单的知识点
首先,还是用比较官方的文字描述来解释下JavaScript: JavaScript一种直译式脚本语言,是一种动态类型.弱类型.基于原型的语言,内置支持类型.它的解释器被称为JavaScript引擎,为 ...
- UVa 714 Copying Books(二分)
题目链接: 传送门 Copying Books Time Limit: 3000MS Memory Limit: 32768 KB Description Before the inventi ...