Skip to content

Instantly share code, notes, and snippets.

@DamianSuess
Last active March 28, 2020 13:52
Show Gist options
  • Save DamianSuess/efc6e2dd6538bd8ccf87a9a2d6f89b87 to your computer and use it in GitHub Desktop.
Save DamianSuess/efc6e2dd6538bd8ccf87a9a2d6f89b87 to your computer and use it in GitHub Desktop.
C# Example of how to create a secured hash signature from requested text and key
using System;
using System.Text;
using System.Linq;
using System.Security.Cryptography;
public class Program
{
public static void Main()
{
var request = "322424946172;69760;24051943;000001;69760;000182B0CC538B1C5F1EFB31CDA1FB01A15D62AC380F38A6EDB6";
var signature = CreateSignature(request,"2a0f5c8feea9a9d52ed2555a3540cea9dc92982b75b50ef0");
Console.Write(signature);
}
public static string CreateSignature(string request, string hexEncodedKey)
{
var key = FromHexString(hexEncodedKey);
var iso919 = new MACTripleDES(key);
var bytes = Encoding.Unicode.GetBytes(request);
var signBytes = iso919.ComputeHash(bytes);
var sign = Convert.ToBase64String(signBytes);
return sign;
}
public static string ToHexString(byte[] bytes)
{
StringBuilder hex = new StringBuilder(bytes.Length * 2);
foreach (byte b in bytes) hex.AppendFormat("{0:X2}", b);
return hex.ToString();
}
public static byte[] FromHexString(string hex)
{
return Enumerable.Range(0, hex.Length)
.Where(x => x % 2 == 0)
.Select(x => Convert.ToByte(hex.Substring(x, 2), 16))
.ToArray();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment