一种密码加密存储方案:加盐哈希

栏目: IT技术 · 发布时间: 6年前

内容简介:今天来聊一聊一种被广泛使用的密码加密存储方案:加盐哈希。本文包含以下内容:其实说白了,盐就是在密码学中用于加密的随机数据。

今天来聊一聊一种被广泛使用的密码加密存储方案:加盐哈希。

本文包含以下内容:

  • 什么是盐?
  • 什么是哈希算法?
  • 加密与校验过程。
  • C#实现。

什么是盐?

 In cryptography, a salt is random data that is used as an additional input to a one-way function that hashes data, a password or passphrase. -- From Wiki.

其实说白了,盐就是在密码学中用于加密的随机数据。

什么是哈希算法?

A cryptographic hash function (CHF) is a hash function that is suitable for use in cryptography. It is a mathematical algorithm that maps data of arbitrary size (often called the "message") to a bit string of a fixed size (the "hash value", "hash", or "message digest") and is a one-way function, that is, a function which is practically infeasible to invert. -- From Wiki.

密码哈希函数(CHF)是适用于密码学的哈希函数。 这是一种数学算法,可将任意大小的数据(通常称为“消息”)映射到固定大小的字符串上(“哈希值”,“哈希”或“消息摘要”),并且是单向的函数,不可逆转。这里包含了密码哈希函数重要的两个特点:1. 可将任意大小数据映射到固定大小数据上。2. 不可逆转。参考下面的图片(源于Wiki),不管input长短,经过哈希算法之后,都会生成一个固定长短的哈希值。并且在input上一个微小的改变,对哈希结果的影响都是巨大的。

一种密码加密存储方案:加盐哈希

加密与校验过程

一种密码加密存储方案:加盐哈希

C#实现

生成盐

 1         /// <summary>
 2         /// 生成盐
 3         /// </summary>
 4         /// <returns></returns>
 5         public static byte[] GetSalt()
 6         {
 7             //Buffer storage.
 8             var salt = new byte[8];
 9             using (var provider = new RNGCryptoServiceProvider())
10             {
11                 provider.GetBytes(salt);
12             }
13             return salt;
14         }

合并盐与明文

 1         /// <summary>
 2         /// 合并盐与明文
 3         /// </summary>
 4         /// <param name="byte1"></param>
 5         /// <param name="byte2"></param>
 6         /// <returns></returns>
 7         private static byte[] CombineByteArray(byte[] byte1, byte[] byte2)
 8         {
 9             var ret = new byte[byte1.Length + byte2.Length];
10             Array.Copy(byte1, 0, ret, 0, byte1.Length);
11             Array.Copy(byte2, 0, ret, byte1.Length, byte2.Length);
12             return ret;
13         }

哈希合并后数据

 1         /// <summary>
 2         /// Hash Salted input and salt
 3         /// </summary>
 4         /// <param name="input">input</param>
 5         /// <param name="salt">salt</param>
 6         /// <returns>return 32 bytes hashed values</returns>
 7         public static byte[] HashSalted256(string input, byte[] salt)
 8         {
 9             var bytes = Encoding.Unicode.GetBytes(input);
10             byte[] hashSaltedValue = null;
11             using (var sha256 = new SHA256CryptoServiceProvider())
12             {
13                 var saltedInput = CombineByteArray(bytes, salt);
14                 hashSaltedValue = sha256.ComputeHash(saltedInput);
15             }
16             return hashSaltedValue;
17         }

参考链接:

https://en.wikipedia.org/wiki/Salt_(cryptography)

https://en.wikipedia.org/wiki/Cryptographic_hash_function


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

移动风暴

移动风暴

[美]弗雷德·沃格尔斯坦 / 朱邦芊 / 中信出版社 / 2014-1-1 / 39

也许,除了伟大的乔布斯,每一位奋力改变世界的硅谷英雄,都值得我们肃然起敬。苹果与谷歌十年博弈,关于这场移动平台战争的报道早已铺天盖地,而这是第一次,我们能听到幕后工程师的真实声音。两大科技巨人用智能手机和平板电脑颠覆了电脑产业。它们位处变革的中心,凭借各自的经营哲学、魅力领袖和商业敏感度,把竞争变成了残酷对决。商业记者沃格尔斯坦报道这场对抗已逾十载,在《移动风暴》中,他带领我们来到一间间办公室和会......一起来看看 《移动风暴》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

HEX CMYK 转换工具
HEX CMYK 转换工具

HEX CMYK 互转工具