当前位置:网站首页>C#计算SHA1加密和base64编码
C#计算SHA1加密和base64编码
2022-08-09 02:21:00 【yutiedun】
调用方法:
Md5SHA1Helper md5helper = new Md5SHA1Helper();
string filepath = @"E:\Release\test.txt";
string sha1value = md5helper.SHA1File(filepath);
string base64value = md5helper.EncodeBase64("utf-8", sha1value);
textBox1.Text = "文件:" + filepath + "\r\nSHA1值:" + sha1value + "\r\nbase64值:" + base64value;
辅助类定义:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace FileMd5SHA1
{
public class Md5SHA1Helper
{
/// <summary>
/// 计算文件的 MD5 值
/// </summary>
/// <param name="fileName">要计算 MD5 值的文件名和路径</param>
/// <returns>MD5 值16进制字符串</returns>
public string MD5File(string fileName)
{
return HashFile(fileName, "md5");
}
/// <summary>
/// 计算文件的 sha1 值
/// </summary>
/// <param name="fileName">要计算 sha1 值的文件名和路径</param>
/// <returns>sha1 值16进制字符串</returns>
public string SHA1File(string fileName)
{
return HashFile(fileName, "sha1");
}
/// <summary>
/// 计算文件的哈希值
/// </summary>
/// <param name="fileName">要计算哈希值的文件名和路径</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值16进制字符串</returns>
private string HashFile(string fileName, string algName)
{
if (!System.IO.File.Exists(fileName))
return string.Empty;
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);
byte[] hashBytes = HashData(fs, algName);
fs.Close();
return ByteArrayToHexString(hashBytes);
}
/// <summary>
/// 计算哈希值
/// </summary>
/// <param name="stream">要计算哈希值的 Stream</param>
/// <param name="algName">算法:sha1,md5</param>
/// <returns>哈希值字节数组</returns>
private byte[] HashData(System.IO.Stream stream, string algName)
{
System.Security.Cryptography.HashAlgorithm algorithm;
if (algName == null)
{
throw new ArgumentNullException("algName 不能为 null");
}
if (string.Compare(algName, "sha1", true) == 0)
{
algorithm = System.Security.Cryptography.SHA1.Create();
}
else
{
if (string.Compare(algName, "md5", true) != 0)
{
throw new Exception("algName 只能使用 sha1 或 md5");
}
algorithm = System.Security.Cryptography.MD5.Create();
}
return algorithm.ComputeHash(stream);
}
/// <summary>
/// 字节数组转换为16进制表示的字符串
/// </summary>
private string ByteArrayToHexString(byte[] buf)
{
return BitConverter.ToString(buf).Replace("-", "");
}
///base64编码
public string EncodeBase64(string code_type, string code)
{
string encode = "";
byte[] bytes = Encoding.GetEncoding(code_type).GetBytes(code);
try
{
encode = Convert.ToBase64String(bytes);
}
catch
{
encode = code;
}
return encode;
}
///base64解码
public string DecodeBase64(string code_type, string code)
{
string decode = "";
byte[] bytes = Convert.FromBase64String(code);
try
{
decode = Encoding.GetEncoding(code_type).GetString(bytes);
}
catch
{
decode = code;
}
return decode;
}
}
}
边栏推荐
- torchversion.transforms的使用
- ZCMU--5115: Buying Keys(C语言)
- mysql连接超过八小时报错
- 嵌入式设备驱动开发
- 2022护眼产品展,北京眼健康展,眼科医学展,近视矫正设备展
- 2022年自然语言处理校招社招实习必备知识点盘点分享
- Open3D 均匀采样
- Force buckled brush problem record 7.1 -- -- -- -- -- 707. The design list
- 虹科技术|如何阻止供应链攻击?
- MT4/MQL4 entry to proficient foreign exchange EA tutorial Lesson 1 Getting to know MetaEditor
猜你喜欢

力扣刷题记录9.1-----24. 两两交换链表中的节点

SEMRush如何寻找关键词用于投放广告

JDBC技术(一)——一个简单的JDBC测试

ROS2 ERROR: OpenGL 1.5 is not supported in GLRenderSystem::initialiseContext at C:\ci\ws\build...

PMP有什么答题技巧?

NPDP改版前最后一次考试!请注意

eladmin容器部署超详细过程

The last exam before the NPDP revision!caution

在树莓派上使用cpolar(番外篇2)

MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
随机推荐
Go - 9 - data type - function
力扣刷题记录2.1-----27. 移除元素
力扣刷题记录8.1-----206. 反转链表
数据恢复软件EasyRecovery支持恢复所有类型的文件
HCIP-R&S By Wakin自用笔记(3)OSPF之各类LSA及LSA更新规则
9.1-----24. Swap the nodes in the linked list in pairs
Go-10-模块与包
Go-7-RESTful API的设计
mysql 5.7 入坑
考研人总结的时间管理7大忌,你中了几条?
MT4/MQL4入门到精通外汇EA教程第一课 认识MetaEditor
18.flink Table/Sql API之 catlog
数仓第二篇: 数据模型(维度建模)
yii2的安装之路
力扣刷题记录10.1-----19. 删除链表的倒数第 N 个结点
Go-8-Gin框架
Use of torchversion.transforms
增额终身寿险哪家最好呢?真的安全吗?
Simple example of .reduce()
电磁辐射安全标准及检测方法