Advertisement
JoshuaB

Payment Hash

Sep 3rd, 2012
7,975
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.82 KB | None | 0 0
  1. // This is in response to the question from Stackoverflow at:
  2. // http://stackoverflow.com/questions/12185122/calculating-hmacsha256-using-c-sharp-to-match-payment-provider-example
  3. //
  4. // By: Jaxrtech
  5.  
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Linq;
  9. using System.Text;
  10. using System.Globalization;
  11. using System.Security.Cryptography;
  12.  
  13. namespace PaymentHash
  14. {
  15.     class Program
  16.     {
  17.         static void Main(string[] args)
  18.         {
  19.             string message = "amount=100&currency=EUR";
  20.             string expectedHex = "b436e3e86cb3800b3864aeecc8d06c126f005e7645803461717a8e4b2de3a905";
  21.             Console.WriteLine("Ref : " + expectedHex);
  22.  
  23.             // Test out the HMAC hash method
  24.             string key = "57617b5d2349434b34734345635073433835777e2d244c31715535255a366773755a4d70532a5879793238235f707c4f7865753f3f446e633a21575643303f66";
  25.             string hashHMACHex = HashHMACHex(key, message);
  26.             Console.WriteLine("HMAC: " + hashHMACHex);
  27.  
  28.             // Test out the SHA hash method
  29.             string innerKey = "61574d6b157f757d02457573556645750e0341481b127a07476303136c005145436c7b46651c6e4f4f040e1569464a794e534309097258550c17616075060950";
  30.             string outerKey = "0b3d27017f151f17682f1f193f0c2f1f64692b227178106d2d096979066a3b2f2906112c0f760425256e647f032c2013243929636318323f667d0b0a1f6c633a";
  31.             string hashSHAHex = HashSHAHex(innerKey, outerKey, message);
  32.             Console.WriteLine("SHA : " + hashSHAHex);
  33.  
  34.             Console.ReadLine();
  35.         }
  36.  
  37.         #region Hash Hex Functions
  38.         private static string HashHMACHex(string keyHex, string message)
  39.         {
  40.             byte[] hash = HashHMAC(HexDecode(keyHex), StringEncode(message));
  41.             return HashEncode(hash);
  42.         }
  43.  
  44.         private static string HashSHAHex(string innerKeyHex, string outerKeyHex, string message)
  45.         {
  46.             byte[] hash = HashSHA(HexDecode(innerKeyHex), HexDecode(outerKeyHex), StringEncode(message));
  47.             return HashEncode(hash);
  48.         }
  49.         #endregion
  50.  
  51.         #region Hash Functions
  52.         private static byte[] HashHMAC(byte[] key, byte[] message)
  53.         {
  54.             var hash = new HMACSHA256(key);
  55.             return hash.ComputeHash(message);
  56.         }
  57.  
  58.         private static byte[] HashSHA(byte[] innerKey, byte[] outerKey, byte[] message)
  59.         {
  60.             var hash = new SHA256Managed();
  61.  
  62.             // Compute the hash for the inner data first
  63.             byte[] innerData = new byte[innerKey.Length + message.Length];
  64.             Buffer.BlockCopy(innerKey, 0, innerData, 0, innerKey.Length);
  65.             Buffer.BlockCopy(message, 0, innerData, innerKey.Length, message.Length);
  66.             byte[] innerHash = hash.ComputeHash(innerData);
  67.  
  68.             // Compute the entire hash
  69.             byte[] data = new byte[outerKey.Length + innerHash.Length];
  70.             Buffer.BlockCopy(outerKey, 0, data, 0, outerKey.Length);
  71.             Buffer.BlockCopy(innerHash, 0, data, outerKey.Length, innerHash.Length);
  72.             byte[] result = hash.ComputeHash(data);
  73.  
  74.             return result;
  75.         }
  76.         #endregion
  77.  
  78.         #region Encoding Helpers
  79.         private static byte[] StringEncode(string text)
  80.         {
  81.             var encoding = new ASCIIEncoding();
  82.             return encoding.GetBytes(text);
  83.         }
  84.  
  85.         private static string HashEncode(byte[] hash)
  86.         {
  87.             return BitConverter.ToString(hash).Replace("-", "").ToLower();
  88.         }
  89.  
  90.         private static byte[] HexDecode(string hex)
  91.         {
  92.             var bytes = new byte[hex.Length / 2];
  93.             for (int i = 0; i < bytes.Length; i++)
  94.             {
  95.                 bytes[i] = byte.Parse(hex.Substring(i * 2, 2), NumberStyles.HexNumber);
  96.             }
  97.             return bytes;
  98.         }
  99.         #endregion
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement