mbr*_*brc 2 c# bouncycastle portable-class-library
我想在可移植的 C# 类中实现这个逻辑:
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); } } }
};
}
Run Code Online (Sandbox Code Playgroud)
但是HMACSHA256,HMACSHA384并且HMACSHA512在便携式库中不存在。
首先我尝试使用https://github.com/AArnott/PCLCrypto
但我总是得到:An exception of type 'System.NotImplementedException' occurred in PCLCrypto.dll but was not handled in user code
然后我检查了代码,我看到 Crpyto for PCL 没有实现并且总是抛出异常
然后我找到了这个库:https : //github.com/onovotny/BouncyCastle-PCL
但是没有文档说明如何使用它。有人可以给我一个如何实施的例子吗
var sha = new HMACSHA256(key)
var sha = new HMACSHA384(key)
var sha = new HMACSHA512(key)
Run Code Online (Sandbox Code Playgroud)
使用 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;
}
}
Run Code Online (Sandbox Code Playgroud)
其他两个应该也一样...