Need an HMAC-SHA1 implementation for C# Micro Framework
Like the title says, I'm looking for an HMAC-SHA1 implementation开发者_运维技巧 for C#. This will run under the micro framework, so I can't use the built in System.Security.Cryptography classes.
I have no problem porting a library, if I can understand the original code. I've found the source for one in Javascript and verified that it matched expected values, but I'm having trouble porting because I can't always tell what types the values are.
So anyone know of any C# (or Java (or other strongly typed)) implementations out there?
The Bouncycastle C# crypto library contains liberally licensed C# source code. It should be trivial to extract the Org.BouncyCastle.Crypto.Digests.Sha1Digest
and Org.BouncyCastle.Crypto.Macs.HMac
classes and incorporate them into your code. It doesn't appear to have any dependence on those .NET classes, but I don't know what classes are included in the micro framework.
You can have a C# version with the same API as Microsoft by using the code provided by the Mono project. It's MIT X11 licensed so it's easy to reuse inside any project. See:
https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Security.Cryptography/HMACSHA1.cs
https://github.com/mono/mono/blob/master/mcs/class/corlib/System.Security.Cryptography/HMAC.cs
and related files. There are also unit tests that you can use to ensure it works properly on your platform.
sphlib is an opensource library which includes optimized code for many hash functions, including SHA-1, in both C and Java. The Java version also includes a generic HMAC implementation (usable with any of the implemented hash functions). Since the code is basic Java with only elementary integer operations (it was meant to be able to run on J2ME platforms too), translating it to C# should be easy (you will just need to change Java's '>>>
' unsigned right shift operator into a cast to the corresponding C# unsigned type and then do a regular right shift).
精彩评论