Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // This is in response to the question from Stackoverflow at:
- // http://stackoverflow.com/questions/12185122/calculating-hmacsha256-using-c-sharp-to-match-payment-provider-example
- //
- // By: Jaxrtech
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Globalization;
- using System.Security.Cryptography;
- namespace PaymentHash
- {
- class Program
- {
- static void Main(string[] args)
- {
- string message = "amount=100¤cy=EUR";
- string expectedHex = "b436e3e86cb3800b3864aeecc8d06c126f005e7645803461717a8e4b2de3a905";
- Console.WriteLine("Ref : " + expectedHex);
- // Test out the HMAC hash method
- string key = "57617b5d2349434b34734345635073433835777e2d244c31715535255a366773755a4d70532a5879793238235f707c4f7865753f3f446e633a21575643303f66";
- string hashHMACHex = HashHMACHex(key, message);
- Console.WriteLine("HMAC: " + hashHMACHex);
- // Test out the SHA hash method
- string innerKey = "61574d6b157f757d02457573556645750e0341481b127a07476303136c005145436c7b46651c6e4f4f040e1569464a794e534309097258550c17616075060950";
- string outerKey = "0b3d27017f151f17682f1f193f0c2f1f64692b227178106d2d096979066a3b2f2906112c0f760425256e647f032c2013243929636318323f667d0b0a1f6c633a";
- string hashSHAHex = HashSHAHex(innerKey, outerKey, message);
- Console.WriteLine("SHA : " + hashSHAHex);
- Console.ReadLine();
- }
- #region Hash Hex Functions
- private static string HashHMACHex(string keyHex, string message)
- {
- byte[] hash = HashHMAC(HexDecode(keyHex), StringEncode(message));
- return HashEncode(hash);
- }
- private static string HashSHAHex(string innerKeyHex, string outerKeyHex, string message)
- {
- byte[] hash = HashSHA(HexDecode(innerKeyHex), HexDecode(outerKeyHex), StringEncode(message));
- return HashEncode(hash);
- }
- #endregion
- #region Hash Functions
- private static byte[] HashHMAC(byte[] key, byte[] message)
- {
- var hash = new HMACSHA256(key);
- return hash.ComputeHash(message);
- }
- private static byte[] HashSHA(byte[] innerKey, byte[] outerKey, byte[] message)
- {
- var hash = new SHA256Managed();
- // Compute the hash for the inner data first
- byte[] innerData = new byte[innerKey.Length + message.Length];
- Buffer.BlockCopy(innerKey, 0, innerData, 0, innerKey.Length);
- Buffer.BlockCopy(message, 0, innerData, innerKey.Length, message.Length);
- byte[] innerHash = hash.ComputeHash(innerData);
- // Compute the entire hash
- byte[] data = new byte[outerKey.Length + innerHash.Length];
- Buffer.BlockCopy(outerKey, 0, data, 0, outerKey.Length);
- Buffer.BlockCopy(innerHash, 0, data, outerKey.Length, innerHash.Length);
- byte[] result = hash.ComputeHash(data);
- return result;
- }
- #endregion
- #region Encoding Helpers
- private static byte[] StringEncode(string text)
- {
- var encoding = new ASCIIEncoding();
- return encoding.GetBytes(text);
- }
- private static string HashEncode(byte[] hash)
- {
- return BitConverter.ToString(hash).Replace("-", "").ToLower();
- }
- private static byte[] HexDecode(string hex)
- {
- var bytes = new byte[hex.Length / 2];
- for (int i = 0; i < bytes.Length; i++)
- {
- bytes[i] = byte.Parse(hex.Substring(i * 2, 2), NumberStyles.HexNumber);
- }
- return bytes;
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement