123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 |
- namespace SAGA.DotNetUtils
- {
- using System;
- using System.IO;
- using System.Security.Cryptography;
- using System.Text;
- public sealed class HashHelper
- {
- public static string ComputeCRC32(string fileName)
- {
- string str = string.Empty;
- if (!File.Exists(fileName))
- {
- return str;
- }
- using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- Crc32 crc = new Crc32();
- byte[] buffer = crc.ComputeHash(stream);
- crc.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string ComputeHash(HashAlgorithm calculator, byte[] s)
- {
- using (MemoryStream stream = new MemoryStream(s))
- {
- byte[] buffer = calculator.ComputeHash(stream);
- calculator.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string ComputeHash(ComputeOptions option, byte[] s)
- {
- using (MemoryStream stream = new MemoryStream(s))
- {
- HashAlgorithm algorithm = null;
- switch (option)
- {
- case ComputeOptions.ComputeMD5:
- algorithm = MD5.Create();
- break;
- case ComputeOptions.ComputeSHA1:
- algorithm = SHA1.Create();
- break;
- default:
- algorithm = HashAlgorithm.Create();
- break;
- }
- byte[] buffer = algorithm.ComputeHash(stream);
- algorithm.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string ComputeMD5(Stream fs)
- {
- using (fs)
- {
- MD5 md = MD5.Create();
- byte[] buffer = md.ComputeHash(fs);
- md.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string ComputeMD5(string fileName)
- {
- string str = string.Empty;
- if (!File.Exists(fileName))
- {
- return str;
- }
- using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- MD5 md = MD5.Create();
- byte[] buffer = md.ComputeHash(stream);
- md.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- public static string ComputeSHA1(string fileName)
- {
- string str = string.Empty;
- if (!File.Exists(fileName))
- {
- return str;
- }
- using (FileStream stream = new FileStream(fileName, FileMode.Open, FileAccess.Read, FileShare.Read))
- {
- SHA1 sha = SHA1.Create();
- byte[] buffer = sha.ComputeHash(stream);
- sha.Clear();
- StringBuilder builder = new StringBuilder();
- for (int i = 0; i < buffer.Length; i++)
- {
- builder.Append(buffer[i].ToString("x2"));
- }
- return builder.ToString();
- }
- }
- }
- }
|