new_10009_YanCheng_Metrology/Assets/Scripts/SecretUtils/Crypto/SM3Util.cs

49 lines
1.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using Org.BouncyCastle.Crypto.Digests;
namespace SecretUtils.Crypto
{
/**
* 计算sm3 hash值
* <p>若无特殊说明接口接收的都是原始的二进制数据被hex或者base64编码的数据务必解码之后再传进来
* @author liangruxing
*
*/
class SM3Util
{
/**
* 计算hash值在数据量不大时使用数据量大应使用原生接口分段计算sm3值
* @param srcData 待计算hash值的数据
* @return
*/
public static byte[] Hash(byte[] srcData)
{
SM3Digest digest = new SM3Digest();
digest.BlockUpdate(srcData, 0, srcData.Length);
byte[] hash = new byte[digest.GetDigestSize()];
digest.DoFinal(hash, 0);
return hash;
}
/**
* 校验sm3值在数据量不大时使用数据量大应使用原生接口分段计算sm3值然后校验
* @param srcData 待验证的数据
* @param sm3Hash 待验证的hash值
* @return
*/
public static Boolean VerifyHash(byte[] srcData, byte[] sm3Hash)
{
byte[] newHash = Hash(srcData);
if (newHash.Length != sm3Hash.Length) {
return false;
}
for (int i = 0;i< newHash.Length;i++) {
if (newHash[i] != sm3Hash[i]) {
return false;
}
}
return true;
}
}
}