You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
70 lines
2.2 KiB
C#
70 lines
2.2 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Cryptography;
|
|
using System.Text;
|
|
|
|
namespace POSV.Common.Util
|
|
{
|
|
public class Hashing
|
|
{
|
|
/// <summary>
|
|
/// 计算SHA1
|
|
/// </summary>
|
|
/// <param name="data">字节数据</param>
|
|
/// <returns>SHA1</returns>
|
|
public static byte[] CalcSHA1(byte[] data)
|
|
{
|
|
#if WINDOWS_UWP
|
|
var sha = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Sha1);
|
|
var buf = CryptographicBuffer.CreateFromByteArray(data);
|
|
var digest = sha.HashData(buf);
|
|
var hashBytes = new byte[digest.Length];
|
|
CryptographicBuffer.CopyToByteArray(digest, out hashBytes);
|
|
return hashBytes;
|
|
#else
|
|
SHA1 sha1 = SHA1.Create();
|
|
return sha1.ComputeHash(data);
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算MD5哈希(可能需要关闭FIPS)
|
|
/// </summary>
|
|
/// <param name="str">待计算的字符串</param>
|
|
/// <returns>MD5结果</returns>
|
|
public static string CalcMD5(string str)
|
|
{
|
|
#if WINDOWS_UWP
|
|
var md5 = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
|
|
var buf = CryptographicBuffer.ConvertStringToBinary(str, BinaryStringEncoding.Utf8);
|
|
var digest = md5.HashData(buf);
|
|
return CryptographicBuffer.EncodeToHexString(digest);
|
|
#else
|
|
MD5 md5 = MD5.Create();
|
|
byte[] data = Encoding.UTF8.GetBytes(str);
|
|
byte[] hashData = md5.ComputeHash(data);
|
|
StringBuilder sb = new StringBuilder(hashData.Length * 2);
|
|
foreach (byte b in hashData)
|
|
{
|
|
sb.AppendFormat("{0:x2}" , b);
|
|
}
|
|
return sb.ToString();
|
|
#endif
|
|
}
|
|
|
|
/// <summary>
|
|
/// 计算MD5哈希(第三方实现)
|
|
/// </summary>
|
|
/// <param name="str">待计算的字符串,避免FIPS-Exception</param>
|
|
/// <returns>MD5结果</returns>
|
|
public static string CalcMD5X(string str)
|
|
{
|
|
byte[] data = Encoding.UTF8.GetBytes(str);
|
|
LabMD5 md5 = new LabMD5();
|
|
return md5.ComputeHash(data);
|
|
}
|
|
|
|
}
|
|
}
|