我有一个随机生成的byte
数组:
Random random = new Random();
var bytes = new byte[32];
random.NextBytes(myBytes);
我希望使用SHA1
对给定的字节数组进行哈希处理,例如:
public static readonly sbyte[] salt = new sbyte [] { 82, 122, 43, 30,
-47, 97, 4,-124,
-31, -63, -108, 69,
-83, -86, -125, 88,
-98, -77, 111, 79,
-71, -73, 100, 106,
8, -20, -95, -27,
38, -32, -61, 88};
我对此并不是很有经验,我想在java
平台C#
上编写这个UWP
代码:
public static byte []computeKey(byte[] bytes) throws NoSuchAlgorithmException
{
byte[] salt = { 82,122, 43, 30,-47, 97, 4,-124,-31,-63,-108, 69,-83,-86,-125, 88,-98,-77,111, 79,-71,-73,100,106, 8,-20,-95,-27, 38,-32,-61, 88};
MessageDigest digester = MessageDigest.getInstance("SHA1");
digester.update(bytes, 0, bytes.length);
digester.update(salt, 0, salt.length);
byte[] digest = digester.digest();
return digest;
}
我想在C#UWP中实现这一点,我发现.Net
使用System.Security.Cryptography
使用TransformBlock
方法可以实现这一点(不确定究竟如何),问题在UWP上它是完全不同的,使用Windows.Security.Cryptography
,没有提供太多选择,尝试了一些路径,但没有什么是明确的,任何提示如何实现这一点?
答案 0 :(得分:0)
试试这个:
byte[] salt = { 82, 122, 43, 30, 47, 97, 4, 124, 31, 63, 108, 69, 83, 86, 125, 88, 98, 77, 111, 79, 71, 73, 100, 106, 8, 20, 95, 27, 38, 32, 61, 88 };
var message = "message";
var computedBytes = HmacSha1Sign(salt, message);
使用此功能:
public static byte[] HmacSha1Sign(byte[] keyBytes, string message)
{
var messageBytes = Encoding.UTF8.GetBytes(message);
MacAlgorithmProvider objMacProv = MacAlgorithmProvider.OpenAlgorithm("HMAC_SHA1");
CryptographicKey hmacKey = objMacProv.CreateKey(keyBytes.AsBuffer());
IBuffer buffHMAC = CryptographicEngine.Sign(hmacKey, messageBytes.AsBuffer());
return buffHMAC.ToArray();
}