博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
PasswordHasher 算法
阅读量:4310 次
发布时间:2019-06-06

本文共 3125 字,大约阅读时间需要 10 分钟。

1         public override PasswordVerificationResult VerifyHashedPassword(string hashedPassword, string providedPassword) 2         { 3             string[] passwordProperties = hashedPassword.Split('|'); 4             if (passwordProperties.Length != 3) 5             { 6                 return base.VerifyHashedPassword(hashedPassword, providedPassword); 7             } 8             else 9             {10                 string passwordHash = passwordProperties[0];11                 int passwordformat = 1;12                 string salt = passwordProperties[2];13                 if (String.Equals(EncryptPassword(providedPassword, passwordformat, salt), passwordHash, StringComparison.CurrentCultureIgnoreCase))14                 {15                     return PasswordVerificationResult.SuccessRehashNeeded;16                 }17                 else18                 {19                     return PasswordVerificationResult.Failed;20                 }21             }22         }23 24         25         private string EncryptPassword(string pass, int passwordFormat, string salt)26         {27             if (passwordFormat == 0) 28                 return pass;29 30             byte[] bIn = Encoding.Unicode.GetBytes(pass);31             byte[] bSalt = Convert.FromBase64String(salt);32             byte[] bRet = null;33 34             if (passwordFormat == 1)35             { // MembershipPasswordFormat.Hashed 36                 HashAlgorithm hm = HashAlgorithm.Create("SHA1");37                 if (hm is KeyedHashAlgorithm)38                 {39                     KeyedHashAlgorithm kha = (KeyedHashAlgorithm)hm;40                     if (kha.Key.Length == bSalt.Length)41                     {42                         kha.Key = bSalt;43                     }44                     else if (kha.Key.Length < bSalt.Length)45                     {46                         byte[] bKey = new byte[kha.Key.Length];47                         Buffer.BlockCopy(bSalt, 0, bKey, 0, bKey.Length);48                         kha.Key = bKey;49                     }50                     else51                     {52                         byte[] bKey = new byte[kha.Key.Length];53                         for (int iter = 0; iter < bKey.Length; )54                         {55                             int len = Math.Min(bSalt.Length, bKey.Length - iter);56                             Buffer.BlockCopy(bSalt, 0, bKey, iter, len);57                             iter += len;58                         }59                         kha.Key = bKey;60                     }61                     bRet = kha.ComputeHash(bIn);62                 }63                 else64                 {65                     byte[] bAll = new byte[bSalt.Length + bIn.Length];66                     Buffer.BlockCopy(bSalt, 0, bAll, 0, bSalt.Length);67                     Buffer.BlockCopy(bIn, 0, bAll, bSalt.Length, bIn.Length);68                     bRet = hm.ComputeHash(bAll);69                 }70             }71 72             return Convert.ToBase64String(bRet);73         }

 

转载于:https://www.cnblogs.com/anech/p/3818703.html

你可能感兴趣的文章
HDU 4571 SPFA+DP
查看>>
centos 创建以日期为名的文件夹
查看>>
Java Timer触发定时器
查看>>
Page Object设计模式
查看>>
程序的基础知识
查看>>
在VIM中使用GDB调试 – 使用vimgdb
查看>>
python爬虫---从零开始(五)pyQuery库
查看>>
POJ2236(KB5-A)
查看>>
Centos MySQL数据库迁移详细步骤
查看>>
2初出茅庐--初级篇2.1
查看>>
新建 WinCE7.0 下的 Silverlight 工程
查看>>
腾讯的张小龙是一个怎样的人?
查看>>
jxl写入excel实现数据导出功能
查看>>
linux文件目录类命令|--cp指令
查看>>
.net MVC 404错误解决方法
查看>>
linux系统目录结构
查看>>
git
查看>>
btn按钮之间事件相互调用
查看>>
Entity Framework 4.3.1 级联删除
查看>>
codevs 1163:访问艺术馆
查看>>