.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的一致性哈希实现
最近在项目的微服务架构推进过程中,一个新的服务需要动态伸缩的弹性部署,所有容器化示例组成一个大的工作集群,以分布式处理的方式来完成一项工作,在集群中所有节点的任务分配过程中,由于集群工作节点需要动态增 ...
随机推荐
- ReportViewer部分使用总结
最近winform上使用ReportViewer做报表,因为之前没弄过,所以遇到了很多问题,现在总结一下. 一.运行环境 .net环境:4.0 开发工具:vs2010 二.开发步骤 第一步,在winf ...
- CodeForces 519E 树形DP A and B and Lecture Rooms
给出一棵树,有若干次询问,每次询问距两个点u, v距离相等的点的个数. 情况还挺多的,少侠不妨去看官方题解.^_^ #include <iostream> #include <cst ...
- Centos 7下利用crontab定时执行任务详解
一 cron服务 cron服务是Linux的内置服务,但它不会开机自动启动.可以用以下命令启动和停止服务: /sbin/service crond start /sbin/service crond ...
- python - log日志
# -*- coding:utf-8 -*- ''' @project: jiaxy @author: Jimmy @file: study_logging.py @ide: PyCharm Comm ...
- ptyhon - 接口自动化测试实战case1
work_20181203_httprequest.py: import requestsclass http_request: def http_get(url,params): res = req ...
- Leetcode 450.删除二叉搜索树的节点
删除二叉搜索树的节点 给定一个二叉搜索树的根节点 root 和一个值 key,删除二叉搜索树中的 key 对应的节点,并保证二叉搜索树的性质不变.返回二叉搜索树(有可能被更新)的根节点的引用. 一般来 ...
- 台州学院we are without brain 训练 计算几何
A - View Angle Flatland has recently introduced a new type of an eye check for the driver's licence. ...
- Swagger Edit自动生成代码工具
一.swagger简介 swagger是一套开源的API设计工具,包括Swagger UI和Swagger Editor等.其中swagger edit是用来编辑接口文档的小程序,非常简单易用.在官网 ...
- HDU-2853 Assignment
求二分最大匹配,但还要尽量接近原匹配... 解决方法:对于N个顶点的二分图,每条边同时乘上一个比N稍微大的数N',然后对于在原匹配的边就都+1. 经过这样处理,求得的答案Ans乘除N'即是原图的最大匹 ...
- [luoguP1640] [SCOI2010]连续攻击游戏(二分图最大匹配)
传送门 我们将每一个属性和物品连边,然后枚举从小到大属性跑匈牙利,直到找不到连边 #include <cstdio> #include <cstring> #include & ...