C# | 上位机开发新手指南(六)摘要算法
文章目录
- C# | 上位机开发新手指南(六)摘要算法
-
- 前言
- 常见摘要算法源码
-
- MD5算法
- SHA-1算法
- SHA-256算法
- SHA-512算法
- BLAKE2算法
- RIPEMD算法
- Whirlpool算法
前言
你知道摘要算法么?它在保障数据安全方面非常有用!
它能够将任意长度的数据转换成固定长度的消息摘要,从而确保数据的完整性和可靠性。比如说,我们下载软件的时候,就可以用摘要算法来检验软件是否被篡改,保障我们的电脑安全。
那这个算法的工作原理是怎样的呢?大致就是通过一系列复杂的计算,将原始数据转换为一个固定长度的摘要信息。而且无论输入的数据大小,输出的摘要信息长度都是一样的。
那么摘要算法有什么用处呢?比如数字签名,确保数据的来源和内容没有被篡改。还有密码学等领域的应用,可以说是非常厉害了!
那常见的摘要算法有哪些呢?比较常见的有MD5、SHA-1、SHA-2、SHA-3、BLAKE2、RIPEMD、Whirlpool和Keccak等。它们各有不同的优缺点和适用场景,我们需要根据实际情况来选择使用哦。
常见摘要算法源码
MD5算法
MD5是一种广泛使用的哈希算法,将任意长度的数据计算为128位的哈希值。尽管它被广泛使用,但是因为它已经被证明存在安全漏洞,现在已经不再被推荐使用。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 MD5 算法生成摘要string md5Hash = GetMd5Hashinput);Console.WriteLine$"MD5 hash of {input}: {md5Hash}");}// 使用 MD5 算法生成摘要static string GetMd5Hashstring input){using MD5 md5 = MD5.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = md5.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 MD5 类。在方法中,先将字符串转换为字节数组,然后调用 MD5 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
SHA-1算法
SHA-1是一种用于计算数据哈希值的加密算法,将任意长度的数据计算为160位的哈希值。目前,SHA-1算法也已经被证明存在安全漏洞,现在不再被推荐使用。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 SHA-1 算法生成摘要string sha1Hash = GetSha1Hashinput);Console.WriteLine$"SHA-1 hash of {input}: {sha1Hash}");}// 使用 SHA-1 算法生成摘要static string GetSha1Hashstring input){using SHA1 sha1 = SHA1.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = sha1.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 SHA1 类。在方法中,先将字符串转换为字节数组,然后调用 SHA1 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
SHA-256算法
SHA-256是一种用于计算数据哈希值的加密算法,将任意长度的数据计算为256位的哈希值。它是SHA-2算法族中的一员,比SHA-1更安全。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 SHA-256 算法生成摘要string sha256Hash = GetSha256Hashinput);Console.WriteLine$"SHA-256 hash of {input}: {sha256Hash}");}// 使用 SHA-256 算法生成摘要static string GetSha256Hashstring input){using SHA256 sha256 = SHA256.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = sha256.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 SHA256 类。在方法中,先将字符串转换为字节数组,然后调用 SHA256 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
SHA-512算法
SHA-512是一种用于计算数据哈希值的加密算法,将任意长度的数据计算为512位的哈希值。它是SHA-2算法族中的一员,比SHA-256更安全。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 SHA-512 算法生成摘要string sha512Hash = GetSha512Hashinput);Console.WriteLine$"SHA-512 hash of {input}: {sha512Hash}");}// 使用 SHA-512 算法生成摘要static string GetSha512Hashstring input){using SHA512 sha512 = SHA512.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = sha512.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 SHA512 类。在方法中,先将字符串转换为字节数组,然后调用 SHA512 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
BLAKE2算法
BLAKE2是BLAKE算法的改进版,是一种快速、安全的哈希函数,支持多种哈希长度,其中BLAKE2b和BLAKE2s分别支持512位和256位哈希值。它在安全性和性能方面都表现优异,被广泛应用于密码学和安全领域。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 BLAKE2 算法生成摘要string blake2bHash = GetBlake2bHashinput);Console.WriteLine$"BLAKE2b hash of {input}: {blake2bHash}");string blake2sHash = GetBlake2sHashinput);Console.WriteLine$"BLAKE2s hash of {input}: {blake2sHash}");}// 使用 BLAKE2b 算法生成摘要static string GetBlake2bHashstring input){using BLAKE2b blake2b = BLAKE2b.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = blake2b.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}// 使用 BLAKE2s 算法生成摘要static string GetBlake2sHashstring input){using BLAKE2s blake2s = BLAKE2s.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = blake2s.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 BLAKE2b 和 BLAKE2s 类。在方法中,先将字符串转换为字节数组,然后调用 BLAKE2 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
RIPEMD算法
RIPEMD是一种比较老的哈希算法,支持多种哈希长度,其中RIPEMD-160和RIPEMD-256分别支持160位和256位哈希值。它在安全性方面比MD5强,但性能较慢。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 RIPEMD-160 算法生成摘要string ripemd160Hash = GetRipemd160Hashinput);Console.WriteLine$"RIPEMD-160 hash of {input}: {ripemd160Hash}");// 使用 RIPEMD-256 算法生成摘要string ripemd256Hash = GetRipemd256Hashinput);Console.WriteLine$"RIPEMD-256 hash of {input}: {ripemd256Hash}");}// 使用 RIPEMD-160 算法生成摘要static string GetRipemd160Hashstring input){using RIPEMD160 ripemd160 = RIPEMD160.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = ripemd160.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}// 使用 RIPEMD-256 算法生成摘要static string GetRipemd256Hashstring input){using RIPEMD256 ripemd256 = RIPEMD256.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = ripemd256.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 RIPEMD160 和 RIPEMD256 类。在方法中,先将字符串转换为字节数组,然后调用 RIPEMD 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
Whirlpool算法
Whirlpool是一种强度很高的哈希算法,支持512位哈希值。它具有高度的安全性和抗碰撞性,并且被广泛应用于数字签名、身份验证和数据完整性验证等领域。
示例代码:
using System;
using System.Security.Cryptography;
using System.Text;class Program
{static void Mainstring[] args){string input = "Hello, world!";// 使用 Whirlpool 算法生成摘要string whirlpoolHash = GetWhirlpoolHashinput);Console.WriteLine$"Whirlpool hash of {input}: {whirlpoolHash}");}// 使用 Whirlpool 算法生成摘要static string GetWhirlpoolHashstring input){using Whirlpool whirlpool = Whirlpool.Create)){// 将输入字符串转换为字节数组byte[] inputBytes = Encoding.UTF8.GetBytesinput);// 计算哈希值byte[] hashBytes = whirlpool.ComputeHashinputBytes);// 将哈希值转换为字符串StringBuilder builder = new StringBuilder);for int i = 0; i < hashBytes.Length; i++){builder.AppendhashBytes[i].ToString"x2"));}return builder.ToString);}}
}
代码中使用了 System.Security.Cryptography 命名空间中的 Whirlpool 类。在方法中,先将字符串转换为字节数组,然后调用 Whirlpool 类的 ComputeHash 方法生成哈希值,最后将哈希值转换为字符串返回。
查看全文
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.dgrt.cn/a/2198619.html
如若内容造成侵权/违法违规/事实不符,请联系一条长河网进行投诉反馈,一经查实,立即删除!
相关文章:
C# | 上位机开发新手指南(六)摘要算法
C# | 上位机开发新手指南(六)摘要算法 文章目录C# | 上位机开发新手指南(六)摘要算法前言常见摘要算法源码MD5算法SHA-1算法SHA-256算法SHA-512算法BLAKE2算法RIPEMD算法Whirlpool算法前言
你知道摘要算法么?它在保障……
计算机毕业设计Android图书馆教室座位预约系统app
三、网站开放人员 1.学生(登录帐号为学号,密码最低限制6位),教室管理员
四、主页内容 1)、学生 1.预约自习室 2.进行签到界面(签到时间,签到机器) 3.修改个人信息(专业 、入学时间……
Python 算法交易实验60 ADBS: MyQuantBaseStep1Signals
说明
我发现从MyQuantBase 并不能直接到 MyQuantBaseStep2Signals,主要是因为后者需要依靠一阶的斜率计算。这部分本来也可以放在MyQuantBase做,但是ADBS又做了一次升级,所以干脆独立一个ADBS,与MyQuantBase同级,都会……
商业级手术麻醉系统源码,术前分析、用药、评级,术后访视、麻醉科室管理、数据统计分析
手术麻醉管理系统源码,商业级源码,有演示,系统稳定,完美运行。
C#语言前端框架:Winform 后端框架:WCF 数 据库:sqlserver 开发工具:VS2019
C# .net 桌面软件 C/S版 文末获取联系ÿ……
解决jar包依赖的经验教训
1、对比pom中文件引入的jar包版本与web目录下lib里边jar包版本的不同,定位到冲突的jar包;
2、利用pom的依赖树工具,找到引入该版本jar包的依赖,排除依赖即可。…
随便写点什么
自从开始工作之后,时间飞逝,一周一周的,在不知不觉中就过去了,随之而来的便是体重的增长,体质的下降,毕业时候想过要做的许多事情,也都没有再去想过。总觉得人生不能就这样过,每一周……
0116 实用指令
1.运行级别
0:关机
1:单用户(找回丢失密码)
2:多用户状态没有网络服务
3:多用户状态有网络服务
4:系统未使用保留给用户
5:图形界面
6:系统重启
常用运行级别是3……
redis删除key命令
redis-cli -h 10.110.21.81 -p 21412 -a password -n 4 scan 0 count 20000 | xargs redis-cli -h 10.110.21.81 -p 21412 -a password -n 4 del…
读书笔记—浪潮之巅
1、在google的战略里,以后复杂的功能将通过云计算来完成。
2、…
mongo findandmodify
mongodb 提供了一种原子性的操作,findandmodify,但它只满足原子性,不具备事务的特性,事务具有四种特性:原子性,隔离性,一致性,持久性。findAndModify在高并发下有可能会出现以下情况……
一、lua基础知识1
一、lua 的数据类型
–类型 a1; –number printtypea)) –number b"HelloWorld"; printtypeb)) –string 两种数据类型 ctrue; printtypec)) –boolean true 或者 false d print; d"HelloWorld"); printtyped)); –function类型 ……
二、lua语言基础2
1.lua的类型有哪些?答:lua的数据类型有:number,string,nil function,table,thread,userdata用户自定义的类型),boolean(布尔类型) 2.什么是尾调用,尾调用有什么优点尾调用:在一个函数的最后一步开始调用另……
quick-cocos2dx-luaUI控件讲解
–MyApp部分 require"config") require"cocos.init") require"framework.init") local MyApp class"MyApp", cc.mvc.AppBase) function MyApp:ctor) MyApp.super.ctorself) end function MyApp:run) cc.FileUti……
quick-cocos2dx lua语言讲解 动作,定时器,触摸事件,工程的类的讲解)
–MainScene部分
— display.newScene 创建一个场景 — 在quick里面我们的控件、精灵 一般是加载到场景上的 local MainScene class"MainScene", function) return display.newScene"MainScene") end) function MainScene:ctor) –创……
使用quick-cocos2dx-lua 实现的小游戏(包含碰撞检测,触屏发子弹)
–主界面local MainScene class"MainScene", function)return display.newScene"MainScene")end)ON true;function MainScene:ctor)local bg cc.Sprite:create"main_background.png");bg:setScale2);bg:setPositiondisplay.cx,display……
cocos2d-js 中scrollview详解
/****
开头的一些废话:
1、多思考,善于思考
2、懂得变通
3、多多查询API首先复制一段 API中的源码:(UIScrollView.js)这段代码可以看出 scrollview
中的容器是一个node,并且他的位置是:代码最后……
cocos2d-js中的回调函数中世界坐标系和节点坐标系的相互转换
世界坐标系和节点坐标系都是OPENGL 坐标系 1、世界坐标系原点就是屏幕的左下角; 2、节点坐标系的原点就是一个节点的左下角; 3、两个坐标系可以通过已经写好的cocosAPI进行想换转换; 4、所有的节点需要转为一个节点上或者是统一的世界坐标系……
通过JavaScript实现漂浮
<html>
<head><meta http-equiv"Content-Type" content"text/html"; charset"gb2312" /><title>漂浮广告</title><style type"text/css">div{position:absolute;}</style>
</head>
&……
序列动画和图片内存问题
一、帧动画问题 /*** 帧动画总结:* 1、如果精灵进行新建时,加载了纹理,那么setRestoreOriginalFrame可以设置为false或者true* 2、如果精灵新建时,没有加载纹理的话,那么setRestoreOriginalFrame需要设置为false&#……
冒泡排序的两种写法
//第一种写法: #include <iostream> using namespace std; void bubbleSortint arr[], int n) { for int i 0; i < n-1; i) { int temp 0; for int j i1; j <n; j) { if arr[i] ……
编程日记2023/4/16 15:01:21