C # PCL HMACSHAX with BouncyCastle-PCL

I want to implement this logic in a C # portable 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); } } } }; } 

but HMACSHA256 , HMACSHA384 and HMACSHA512 do not exist in the portable library.

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

I checked the code then, and I saw that Crpyto for PCL is not implemented and always throws an exception

Then I found this library: https://github.com/onovotny/BouncyCastle-PCL

But there is no documentation on how to use it. Can someone give me an example how to implement

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

with BouncyCastle-PCL.

+2
source share
2 answers

Try to do it for 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 the other two ...

+4
source

This is just a sequel because it appears on Google. The PCLCrypto library implements all hash methods, but not in the PCL dll. The PCL DLL is just a stub, and the actual implementations are in the platform specific DLLs.

Just make sure you link to the PCLCrypto library from ALL of your projects, not just the PCL library.

This technology is called bait-and-switch and is used because it allows the final application to use apis system cryptography (to improve performance)

See https://github.com/AArnott/PCLCrypto#installation

0
source

Source: https://habr.com/ru/post/1247993/


All Articles