简体   繁体   中英

Calculate SHA256 hash WITHOUT System.Security.Cryptography

I'm moving one of my libraries to Xamarin PLC.

The official documentation states that System.Security.Cryptography namespace is supported, but it is not in actual code.

I'm looking for a way to compute SHA256 hash without this namespace (And without HashAlgorithmProvider in Windows namespace. This method breaks application for Android and WP8.0)

Test something like this using Bouncy Castle PCL lib ( https://www.nuget.org/packages/Portable.BouncyCastle/ )

var encData = Encoding.UTF8.GetBytes("TESTHASH");
Org.BouncyCastle.Crypto.Digests.Sha256Digest myHash = new Org.BouncyCastle.Crypto.Digests.Sha256Digest();
myHash.BlockUpdate (encData, 0, encData.Length);
byte[] compArr = new byte[myHash.GetDigestSize ()];
myHash.DoFinal (compArr, 0);

The Xamarin documentation is right that namespace is fully supported.

You are not able to use it, simply because you are writing a PCL library where Microsoft (Ok, you find it surprising) does not believe it should be.

You either use the workaround in the other answer, which has a significant performance lost as Bouncy Castle is fully managed and does not get any hardware boost, or give up the PCL approach completely.

Microsoft has .NET Core and .NET Platform Standard/.NET Standard Library coming, which allows your library to target a .NET Platform standard and then most of the PCL issues will go away. Keep an eye on it and you will like that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM