123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- using System;
- using System.Text;
- using System.Security.Cryptography;
- using log4net;
- using System.IO;
- namespace Agent.Services
- {
- class HashService
- {
- private static readonly ILog log = LogManager.GetLogger(typeof(HashService));
- private readonly SHA256 _sha256 = SHA256.Create();
- private readonly MD5 _md5 = MD5.Create();
- /// <summary>
- /// SHA256 HEX 문자열 (-제거)
- /// </summary>
- /// <param name="plain"></param>
- /// <returns></returns>
- public string ToSHA256(string plain)
- {
- try
- {
- return ToSHA256(Encoding.UTF8.GetBytes(plain));
- }
- catch (Exception e)
- {
- log.Error(e);
- return null;
- }
- }
- private string ToSHA256(byte[] plain)
- {
- try
- {
- return BitConverter.ToString(_sha256.ComputeHash(plain)).Replace("-", "");
- }
- catch (Exception)
- {
- throw;
- }
- }
- /// <summary>
- /// 파일 -> MD5 HEX 문자열 (-제거)
- /// </summary>
- /// <param name="path"></param>
- /// <returns></returns>
- public string FileToMD5(string path)
- {
- try
- {
- using (FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read))
- {
- byte[] buff = new byte[fs.Length];
- fs.Read(buff, 0, buff.Length);
- return ToMD5(buff);
- }
- }
- catch (Exception e)
- {
- log.Error(e);
- return null;
- }
- }
- private string ToMD5(byte[] plain)
- {
- try
- {
- return BitConverter.ToString(_md5.ComputeHash(plain)).Replace("-", "");
- }
- catch (Exception)
- {
- throw;
- }
- }
- }
- }
|