C# PCL 带 BouncyCastle 的 HMACSHAX-PCL
C# PCL HMACSHAX with BouncyCastle-PCL
我想在可移植的 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
不存在于便携式
图书馆。
首先我尝试 https://github.com/AArnott/PCLCrypto
但我总是得到:An exception of type 'System.NotImplementedException' occurred in PCLCrypto.dll but was not handled in user code
然后我检查了代码,发现 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;
}
}
其他两个也一样...
这只是跟进,因为它出现在 Google 上。 PCLCrypto Library 确实实现了所有哈希方法,但不在 PCL dll 中。 PCL dll 只是一个存根,实际的实现在平台特定的 DLL 中。
只需确保从所有项目中引用 PCLCrypto 库,而不仅仅是 PCL 库。
该技术被称为诱饵和开关,之所以被使用是因为它允许最终应用程序利用系统特定的加密 api(以获得更快的性能)
我想在可移植的 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
不存在于便携式
图书馆。
首先我尝试 https://github.com/AArnott/PCLCrypto
但我总是得到:An exception of type 'System.NotImplementedException' occurred in PCLCrypto.dll but was not handled in user code
然后我检查了代码,发现 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;
}
}
其他两个也一样...
这只是跟进,因为它出现在 Google 上。 PCLCrypto Library 确实实现了所有哈希方法,但不在 PCL dll 中。 PCL dll 只是一个存根,实际的实现在平台特定的 DLL 中。
只需确保从所有项目中引用 PCLCrypto 库,而不仅仅是 PCL 库。
该技术被称为诱饵和开关,之所以被使用是因为它允许最终应用程序利用系统特定的加密 api(以获得更快的性能)