Skip to content

Instantly share code, notes, and snippets.

@Azadehkhojandi
Last active December 24, 2023 14:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Azadehkhojandi/50eaae4cf20b21faef186f2c8ee97873 to your computer and use it in GitHub Desktop.
Save Azadehkhojandi/50eaae4cf20b21faef186f2c8ee97873 to your computer and use it in GitHub Desktop.
Create SHA256 Signature for payment gateway (bendigo bank)
public static string CreateSHA256Signature(string key, string message)
{
// Hex Decode the Secure Secret for use in using the HMACSHA256 hasher
// hex decoding eliminates this source of error as it is independent of the character encoding
// hex decoding is precise in converting to a byte array and is the preferred form for representing binary values as hex strings.
var convertedHash = new byte[key.Length / 2];
for (var i = 0; i < key.Length / 2; i++)
{
convertedHash[i] = (byte)int.Parse(key.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber);
}
// Create secureHash on string
var hexHash = "";
using (var hasher = new HMACSHA256(convertedHash))
{
var hashValue = hasher.ComputeHash(Encoding.UTF8.GetBytes(message));
hexHash = hashValue.Aggregate(hexHash, (current, b) => current + b.ToString("X2"));
}
return hexHash;
}
@AMHZR
Copy link

AMHZR commented Nov 9, 2017

do you have the any another language code for the same like python....

@gauravvjn
Copy link

gauravvjn commented Nov 9, 2017

@AMHZR
Copy link

AMHZR commented Nov 9, 2017

@gjain0
you are awesome.. thanks..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment