admin管理员组文章数量:1530516
2023年12月12日发(作者:)
计算文件SHA1哈希值
Computes the hash for the input data using the managed library.
使用托管库为输入数据计算SHA1哈希。
1. 需求为:为指定文件计算 SHA1哈希值
using System;
using ;
using graphy;
public class HashDirectory
{
public static void Main(String[] args)
{
//文件路径
string directory = "E:/";
//计算输入文件的SHA1哈希值
string sha1Hash = GetSha1Hash(directory);
//输出哈希值或异常信息
ine(sha1Hash);
}
// Display the byte array in a readable format.
public static string PrintByteArray(byte[] array)
{
string sHA1string = "";
for (int i = 0; i < ; i++)
{
sHA1string += $"{array[i]:x2}";
}
return sHA1string;
}
// Computes the SHA1 hash for the input data using the managed library.
public static string GetSha1Hash(string dir)
{
FileInfo fileInfo = new FileInfo(dir);
// Initialize a SHA1 hash object.
using (SHA1Managed sHA1 = new SHA1Managed())
{
try
{
// Create a fileStream for the file.
FileStream fileStream = ();
// Be sure it's positioned to the beginning of the stream.
on = 0;
// Compute the hash of the fileStream.
byte[] hashValue = eHash(fileStream);
// Write the name and hash value of the file to the console.
($"{}: ");
string sHA1string = PrintByteArray(hashValue);
ine(sHA1string);
// Close the file.
();
return sHA1string;
}
catch (IOException e)
{
return ($"I/O Exception: {e}");
}
catch (UnauthorizedAccessException e)
{
{
return ($"Access Exception: {e}");
}
}
}
}
2.
1. 以上为 SHA1算法,巨硬官网很多例子都是 SHA256算法。安全性方面,显然SHA256(又称SHA2)的安全性最高,但是耗
时要比其他两种多很多。MD5相对较容易碰撞,因此,SHA1应该是这三种中性能最好的一款加密算法。
2. The hash size for the SHA1 algorithm is 160 bits.
3. Due to collision problems with SHA1, Microsoft recommends a security model based on SHA256 or better.
3.
版权声明:本文标题:计算文件SHA1哈希值 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://m.elefans.com/dianzi/1702346646a2263.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论