gpt4 book ai didi

C# PCL HMACSHAX 与 BouncyCaSTLe-PCL

转载 作者:行者123 更新时间:2023-11-30 20:43:00 27 4
gpt4 key购买 nike

我想在可移植的 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); } } }
};
}

但是 HMACSHA256, HMACSHA384HMACSHA512 不存在于可移植图书馆。

首先我尝试使用 https://github.com/AArnott/PCLCrypto但我总是得到:PCLCrypto.dll 中发生类型为“System.NotImplementedException”的异常,但未在用户代码中处理

然后我检查了代码,发现没有实现用于 PCL 的 Crpyto,并且总是抛出异常

然后我找到了这个库: https://github.com/onovotny/BouncyCastle-PCL

但是没有文档说明如何使用它。谁能给我一个如何实现的例子

var sha = new HMACSHA256(key)
var sha = new HMACSHA384(key)
var sha = new HMACSHA512(key)

使用 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;
}
}

其他两个应该也是一样的...

关于C# PCL HMACSHAX 与 BouncyCaSTLe-PCL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30974846/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com