using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks; namespace HashTest
{
class Program
{
static void Main(string[] args)
{
string plainText = ".Net5 框架";
// 因为所有哈希函数的输入类型都是 Byte[],所以必须先将源数据转换为字节数组后再计算哈希值。
byte[] plainByte = ASCIIEncoding.ASCII.GetBytes(plainText);
Console.WriteLine("明文字符串:" + plainText);
Console.WriteLine("======================================================"); Console.ForegroundColor = ConsoleColor.Red;
MD5Hash(plainByte);
Console.ForegroundColor = ConsoleColor.Green;
SHA1Hash(plainByte);
Console.ForegroundColor = ConsoleColor.DarkYellow;
SHA512Hash(plainByte);
Console.ReadKey();
} /// <summary>
/// MD5哈希计算
/// </summary>
static void MD5Hash(byte[] plainByte)
{
// MD5本身是一个抽象类
MD5 md5 = MD5.Create(); // 默认实现类:Create("System.Security.Cryptography.MD5");
byte[] hashByte = md5.ComputeHash(plainByte);
Console.WriteLine("1.0:MD5默认实现类对明文字符串进行哈希计算后的结果:");
Console.WriteLine(ByteArrayToString(hashByte));
Console.WriteLine("======================================================"); // MD5的两个派生类:System.Security.Cryptography.MD5Cng 和 System.Security.Cryptography.MD5CryptoServiceProvider
MD5 md5Cng = MD5.Create("System.Security.Cryptography.MD5Cng");
byte[] cngHashByte = md5Cng.ComputeHash(plainByte);
Console.WriteLine("1.1:设置MD5的静态方法Create的参数为System.Security.Cryptography.MD5Cng,哈希结果为:");
Console.WriteLine(ByteArrayToString(cngHashByte));
Console.WriteLine("======================================================"); MD5 md5CryptoServiceProvider = MD5.Create("System.Security.Cryptography.MD5CryptoServiceProvider");
byte[] providerHashByte = md5CryptoServiceProvider.ComputeHash(plainByte);
Console.WriteLine("1.2:设置MD5的静态方法Create的参数为System.Security.Cryptography.MD5CryptoServiceProvider,哈希结果为:");
Console.WriteLine(ByteArrayToString(providerHashByte));
Console.WriteLine("======================================================"); // 直接使用派生类进行哈希
MD5Cng md5Cng2 = new MD5Cng();
byte[] cngHashByte2 = md5Cng.ComputeHash(plainByte);
Console.WriteLine("2.0:直接使用MD5的派生类MD5Cng进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(cngHashByte2));
Console.WriteLine("======================================================"); MD5CryptoServiceProvider md5CryptoServiceProvider2 = new MD5CryptoServiceProvider();
byte[] providerHashByte2 = md5Cng.ComputeHash(plainByte);
Console.WriteLine("2.1:直接使用MD5的派生类MD5CryptoServiceProvider进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(providerHashByte2));
Console.WriteLine("======================================================");
} /// <summary>
/// SHA1哈希
/// </summary>
/// <param name="plainByte"></param>
static void SHA1Hash(byte[] plainByte)
{
// SHA1本身是一个抽象类
SHA1 sha1 = SHA1.Create(); // 默认实现类:Create("System.Security.Cryptography.SHA1");
byte[] hashByte = sha1.ComputeHash(plainByte);
Console.WriteLine("1.0:SHA1默认实现类对明文字符串进行哈希计算后的结果:");
Console.WriteLine(ByteArrayToString(hashByte));
Console.WriteLine("======================================================"); // SHA1的两个派生类:System.Security.Cryptography.SHA1Cng 和 System.Security.Cryptography.SHA1CryptoServiceProvider
SHA1 sha1Cng = SHA1.Create("System.Security.Cryptography.SHA1Cng");
byte[] cngHashByte = sha1Cng.ComputeHash(plainByte);
Console.WriteLine("1.1:设置SHA1的静态方法Create的参数为System.Security.Cryptography.SHA1Cng,哈希结果为:");
Console.WriteLine(ByteArrayToString(cngHashByte));
Console.WriteLine("======================================================"); SHA1 sha1CryptoServiceProvider = SHA1.Create("System.Security.Cryptography.SHA1CryptoServiceProvider");
byte[] providerHashByte = sha1CryptoServiceProvider.ComputeHash(plainByte);
Console.WriteLine("1.2:设置SHA1的静态方法Create的参数为System.Security.Cryptography.SHA1CryptoServiceProvider,哈希结果为:");
Console.WriteLine(ByteArrayToString(providerHashByte));
Console.WriteLine("======================================================"); // 直接使用派生类进行哈希
SHA1Cng sha1Cng2 = new SHA1Cng();
byte[] cngHashByte2 = sha1Cng2.ComputeHash(plainByte);
Console.WriteLine("2.0:直接使用SHA1的派生类SHA1Cng进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(cngHashByte2));
Console.WriteLine("======================================================"); SHA1CryptoServiceProvider sha1CryptoServiceProvider2 = new SHA1CryptoServiceProvider();
byte[] providerHashByte2 = sha1CryptoServiceProvider2.ComputeHash(plainByte);
Console.WriteLine("2.1:直接使用SHA1的派生类SHA1CryptoServiceProvider进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(providerHashByte2));
Console.WriteLine("======================================================");
} static void SHA256Hash(byte[] plainByte)
{ } static void SHA384Hash(byte[] plainByte)
{ } static void SHA512Hash(byte[] plainByte)
{
// 直接使用派生类进行哈希
SHA512Cng sha512Cng2 = new SHA512Cng();
byte[] cngHashByte2 = sha512Cng2.ComputeHash(plainByte);
Console.WriteLine("2.0:直接使用SHA512的派生类SHA512Cng进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(cngHashByte2));
Console.WriteLine("======================================================"); SHA512CryptoServiceProvider sha512CryptoServiceProvider2 = new SHA512CryptoServiceProvider();
byte[] providerHashByte2 = sha512CryptoServiceProvider2.ComputeHash(plainByte);
Console.WriteLine("2.1:直接使用SHA512的派生类SHA512CryptoServiceProvider进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(providerHashByte2));
Console.WriteLine("======================================================"); SHA512Managed sha512Managed = new SHA512Managed();
byte[] sha512ManagedHashByte = sha512Managed.ComputeHash(plainByte);
Console.WriteLine("2.2:直接使用SHA512的派生类SHA512Managed进行哈希,哈希结果为:");
Console.WriteLine(ByteArrayToString(sha512ManagedHashByte));
Console.WriteLine("======================================================");
} /// <summary>
/// 字节数组转化成16进制字符串
/// </summary>
/// <param name="arrInput"></param>
/// <returns></returns>
static string ByteArrayToString(byte[] arrInput)
{
int i;
StringBuilder sOutput = new StringBuilder(arrInput.Length);
for (i = ; i < arrInput.Length - ; i++)
{
sOutput.Append(arrInput[i].ToString("X2"));
}
return sOutput.ToString();
}
}
}

.net 哈希的更多相关文章

  1. [PHP内核探索]PHP中的哈希表

    在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...

  2. java单向加密算法小结(2)--MD5哈希算法

    上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...

  3. Java 哈希表运用-LeetCode 1 Two Sum

    Given an array of integers, find two numbers such that they add up to a specific target number. The ...

  4. 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希

    据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...

  5. Oracle 哈希连接原理

    <基于Oracle的sql优化>里关于哈希连接的原理介绍如下: 哈希连接(HASH JOIN)是一种两个表在做表连接时主要依靠哈希运算来得到连接结果集的表连接方法. 在Oracle 7.3 ...

  6. SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)

    今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...

  7. BZOJ 3555: [Ctsc2014]企鹅QQ [字符串哈希]【学习笔记】

    3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec  Memory Limit: 256 MBSubmit: 2046  Solved: 749[Submit][Statu ...

  8. [bzoj3207][花神的嘲讽计划Ⅰ] (字符串哈希+主席树)

    Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...

  9. minHash最小哈希原理

    minHash最小哈希原理 收藏 初雪之音 发表于 9个月前 阅读 208 收藏 9 点赞 1 评论 0 摘要: 在数据挖掘中,一个最基本的问题就是比较两个集合的相似度.通常通过遍历这两个集合中的所有 ...

  10. .net的一致性哈希实现

    最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...

随机推荐

  1. Python中re(正则表达式)模块使用方法

    Python中常用的正则表达式处理函数: re.match re.match 尝试从字符串的开始匹配一个模式,如:下面的例子匹配第一个单词. import re text = "JGood ...

  2. Linux编程中链接库的使用

    链接库本质上是一段可执行的二进制代码,可以被操作系统载入内存执行.按加载的时机不同,链接库可以分为静态链接库和动态链接库. 静态链接库:编译过程中加载进可执行文件的库(静态库省去了运行时加载的消耗,但 ...

  3. 04-python进阶-map&reduce

    Map --映射 Reduce -- 归纳 将大数据标准化的处理 Map 拆封任务,Reduce将结果合并 这样是不是能够将很多计算机组成一台超级计算机呢? 一些问题:如果任务本身就很复杂,那么拆解任 ...

  4. 【NOIP2017】 列队

    线段树博客先开个点随笔.... 这意味着啥呢? 今天绝对要把这道题写出来并且更掉这篇blog!!!! ~ upd:懂了哈哈哈哈哈哈哈 先贴代码 回家+讲解 ---------------------- ...

  5. javascript是脚本语言?javascript万物皆对象?

    呵呵哒!带你见识下js面对对象的魅力 是的是的,退后,朕要开始装逼了- 这是什么鸟东西?是的是的,装逼开始,2016年度最佳JS编译器,ES6标准出来后,小伙伴们对新特性摩拳擦掌,奈何浏览器支持把我们 ...

  6. Uiautomator ---(1) 封装代码

    http://www.cnblogs.com/by-dream/p/4996000.html  上面是别人的写法 我自己的写法: package qq.test; import android.con ...

  7. [整理]tar压缩下来为什么格式是.tar.gz

    前段时间打包,直接用tar命令压缩,压缩好的文件取名rar.同事用winrar打开发现一直报错. 经过查询发现,tar -cvzf压缩下来的格式其实应该是.tar.gz 但是格式怎么会这么奇怪呢?是压 ...

  8. [POI2006] KRA-The Disks (贪心)

    题目描述 For his birthday present little Johnny has received from his parents a new plaything which cons ...

  9. ideaaaaaaaaa

    数据库proxy:可以用作自动化数据逆向SQL test4j/jtester:

  10. js中的clientHeight和offsetHeight的区别如

    如图所示: