当前位置:网站首页>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;
}
}
}
边栏推荐
猜你喜欢
随机推荐
D. Tournament Countdown
MT4/MQL4 Getting Started to Mastering EA Tutorial Lesson 1 - MQL Language Common Functions (1) OrderSend() Function
How to play knowledge graph in recommender system
Go-8-Gin框架
Summary of Database Design
ROS 、SLAM 学习 error整理
2022杭电多校第五场1007(生成函数+启发式合并+ntt)
Duplicate class com.google.common.util.concurrent.ListenableFuture found in modules
2020.12.4日志
Simple example of .reduce()
数字孪生+燃气管理,开启智慧燃气管理新模式
程序员的日常生活 | 每日趣闻
2022眼康品牌加盟展,北京视力保健展,中国眼科医学技术峰会
What is the difference between a project manager and a product manager?
spdlog日志库的封装使用
Programmer's Daily Life | Daily Fun
史上最猛“员工”,疯狂吐槽亿万富翁老板小扎:那么有钱,还总穿着同样的衣服!
js实现数组去重的方式(7种)
年金险的安全性怎么样啊?可靠吗?
How SEMRush finds keywords for advertising