.net 哈希
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 哈希的更多相关文章
- [PHP内核探索]PHP中的哈希表
在PHP内核中,其中一个很重要的数据结构就是HashTable.我们常用的数组,在内核中就是用HashTable来实现.那么,PHP的HashTable是怎么实现的呢?最近在看HashTable的数据 ...
- java单向加密算法小结(2)--MD5哈希算法
上一篇文章整理了Base64算法的相关知识,严格来说,Base64只能算是一种编码方式而非加密算法,这一篇要说的MD5,其实也不算是加密算法,而是一种哈希算法,即将目标文本转化为固定长度,不可逆的字符 ...
- Java 哈希表运用-LeetCode 1 Two Sum
Given an array of integers, find two numbers such that they add up to a specific target number. The ...
- 网络安全——Base64编码、MD5、SHA1-SHA512、HMAC(SHA1-SHA512)哈希
据说今天520是个好日子,为什么我想起的是502.500.404这些?还好服务器没事! 一.Base64编码 Base64编码要求把3个8位字节(3*8=24)转化为4个6位的字节(4*6=24),之 ...
- Oracle 哈希连接原理
<基于Oracle的sql优化>里关于哈希连接的原理介绍如下: 哈希连接(HASH JOIN)是一种两个表在做表连接时主要依靠哈希运算来得到连接结果集的表连接方法. 在Oracle 7.3 ...
- SQL连接操作符介绍(循环嵌套, 哈希匹配和合并连接)
今天我将介绍在SQLServer 中的三种连接操作符类型,分别是:循环嵌套.哈希匹配和合并连接.主要对这三种连接的不同.复杂度用范例的形式一一介绍. 本文中使用了示例数据库AdventureWorks ...
- BZOJ 3555: [Ctsc2014]企鹅QQ [字符串哈希]【学习笔记】
3555: [Ctsc2014]企鹅QQ Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 2046 Solved: 749[Submit][Statu ...
- [bzoj3207][花神的嘲讽计划Ⅰ] (字符串哈希+主席树)
Description 背景 花神是神,一大癖好就是嘲讽大J,举例如下: “哎你傻不傻的![hqz:大笨J]” “这道题又被J屎过了!!” “J这程序怎么跑这么快!J要逆袭了!” …… 描述 这一天D ...
- minHash最小哈希原理
minHash最小哈希原理 收藏 初雪之音 发表于 9个月前 阅读 208 收藏 9 点赞 1 评论 0 摘要: 在数据挖掘中,一个最基本的问题就是比较两个集合的相似度.通常通过遍历这两个集合中的所有 ...
- .net的一致性哈希实现
最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...
随机推荐
- win7 命令提示符怎么以管理员方式打开
点击屏幕最左下角的"开始"按钮,选择"运行"命令: 在弹出的"运行"对话框中输入"CMD"命令,再单击"确定& ...
- while True 死循环
while True 死循环示例: count = 0 #给count设置变量为0 while True: count += 1 #每循环一次,count+1 : count += 1 等同于coun ...
- Mysql显示所有数据库
show databases; mysql> show databases; +--------------------+ | Database | +--------------------+ ...
- WPF使用异步+绑定的方式处理大数据量
WPF的优势在于界面处理,即使是这样,在面对大数据量的时候也免不了界面假死,同一个线程里处理界面跟大数据量,这是不可避免的.解决办法还是有的,可以使用分页加载,虚拟加载,动态加载,增加条件限制... ...
- SPOJ375 Query on a tree(树链剖分)
传送门 题意 给出一棵树,每条边都有权值,有两种操作: 把第p条边的权值改为x 询问x,y路径上的权值最大的边 code #include<cstdio> #include<algo ...
- luogu2590 [ZJOI2008]树的统计
树剖裸题 #include <iostream> #include <cstdio> using namespace std; int n, uu, vv, hea[30005 ...
- JAVA-STRUTS-2x的项目配置
首先是web.xml的配置,这个是项目加载的开始. <display-name></display-name> <!--struts2配置开始--> <fil ...
- javascript学习笔记 - 引用类型 Date
三 Date new Date() 在不传递参数的情况下,新创建的对象自动获得当前日期和时间.参数接收毫秒的timestamp Date.parse() 接收表示日期的字符串,返回相应的日期毫秒数ti ...
- URAL 1106 Two Teams二分图
S - Two Teams Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submi ...
- Hibernate的简单封装Session(方便调用)
因为每次用增删改查时都需要用到hibernate的配置来生成session工厂进而生成session,比较麻烦,所以我们直接封装一个可以调用的类,需要的时候只需要调用即可. 新建一个Hibernate ...