.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的一致性哈希实现
最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...
随机推荐
- 算法学习记录-排序——插入排序(Insertion Sort)
插入排序: 在<算法导论>中是这样描述的 这是一个对少量元素进行排序的有效算法.插入排序的工作机理与打牌时候,整理手中的牌做法差不多. 在开始摸牌时,我们的左手是空的,牌面朝下放在桌子上. ...
- loj2014 「SCOI2016」萌萌哒
神tm st表+并查集 #include <iostream> #include <cstdio> #include <cmath> using namespace ...
- RHEL6.X设置163yum源
目录 RHEL6.X设置163yum源 卸载系统的yum 检查是否已经卸载完成 下载yum以及相关包 安装yum相关rpm包 清除原有缓存,建立yum列表 本地yum源设置 挂载本地光盘 修改配置文件 ...
- 80x86保护模式下IDT和中断调用过程分析
80x86保护模式下IDT和中断调用过程分析 1.中断描述符表(IDT),将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT类似,IDT也是由8字节长度的描述符组成.IDT空描述符的存 ...
- 5中IO模型整理总结
1.5中IO模型: 阻塞I/O(blocking IO) 非阻塞I/O(noblocking IO) I/O复用 (IO multiplexing ) 信号驱动I/O (signal drive ...
- C++ POST方式访问网页
bool PostContent(CString strUrl, const CString &strPara, CString &strContent, CString &s ...
- 服务器迁移至Linux操作系统
我在这里试了ubuntu.Debian,centos.最终还是选择了centos 使用工具putty,远程桌面的话使用vnc viewer(看起来service文件更改只需要替换user,但是路径不对 ...
- Spring 4.3.11.RELEASE文档阅读(二):Core Technologies_IOC
在看这部分内容的时候,想了一些问题: 容器: 1,什么是容器 用来包装或装载物品的贮存器 2,容器能做什么 包装或装载物品 3,为什么需要容器 为什么要使用集装箱?如果没有容器会是什么样? 4,常见的 ...
- ubuntu服务器与本地文件传输
ubuntu SSH 连接.远程上传下载文件 博客分类: Ubuntu 安装 SSH(Secure Shell) 服务以提供远程管理服务 sudo apt-get install ssh SSH ...
- ubuntu 安装tomcat<服务器>
一.下载tomcat 可以先下载到本地,然后ftp到服务器 官方 Apache Tomcat 的下载页面(下面的链接是apache自己的镜像服务器的地址,不同网络连接的话,apache会给出不同的镜像 ...