具有BouncyCastle-PCL的C#PCL HMACSHAX [英] C# PCL HMACSHAX with BouncyCastle-PCL
问题描述
我想在可移植C#类中实现此逻辑:
I want to implement this logic in portable C# class:
static JsonWebToken()
{
HashAlgorithms = new Dictionary<JwtHashAlgorithm, Func<byte[], byte[], byte[]>>
{
{ JwtHashAlgorithm.HS256, (key, value) => { using (var sha = new HMACSHA256(key)) { return sha.ComputeHash(value); } } },
{ JwtHashAlgorithm.HS384, (key, value) => { using (var sha = new HMACSHA384(key)) { return sha.ComputeHash(value); } } },
{ JwtHashAlgorithm.HS512, (key, value) => { using (var sha = new HMACSHA512(key)) { return sha.ComputeHash(value); } } }
};
}
但HMACSHA256
,HMACSHA384
和HMACSHA512
在便携式计算机中不存在
库.
but HMACSHA256
, HMACSHA384
and HMACSHA512
does not exist in portable
library.
首先,我尝试使用 https://github.com/AArnott/PCLCrypto
但我总是得到:An exception of type 'System.NotImplementedException' occurred in PCLCrypto.dll but was not handled in user code
First I try with https://github.com/AArnott/PCLCrypto
but I always get: An exception of type 'System.NotImplementedException' occurred in PCLCrypto.dll but was not handled in user code
我检查了一下代码,然后发现Crpyto for PCL没有实现,并且总是抛出异常
I checked then code and I saw Crpyto for PCL is not implemented and always throw an exception
然后我找到了这个图书馆: https://github.com/onovotny/BouncyCastle-PCL
Then i found this library: https://github.com/onovotny/BouncyCastle-PCL
但是没有文档说明如何使用它.有人可以给我一个例子如何实现
But there is no documentation how to use it. Can someone give me an exmaple how to implement
var sha = new HMACSHA256(key)
var sha = new HMACSHA384(key)
var sha = new HMACSHA512(key)
与BouncyCastle-PCL.
with BouncyCastle-PCL.
推荐答案
尝试为HmacSha256
public class HmacSha256
{
private readonly HMac _hmac;
public HmacSha256(byte[] key)
{
_hmac = new HMac(new Sha256Digest());
_hmac.Init(new KeyParameter(key));
}
public byte[] ComputeHash(byte[] value)
{
if (value == null) throw new ArgumentNullException("value");
byte[] resBuf = new byte[_hmac.GetMacSize()];
_hmac.BlockUpdate(value, 0, value.Length);
_hmac.DoFinal(resBuf, 0);
return resBuf;
}
}
其他两个应该也一样...
The same should be for other two...
这篇关于具有BouncyCastle-PCL的C#PCL HMACSHAX的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!