C# sample code for the signature calculation

 

 

using System;
using System.Collections.Generic;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace SignCommand {
	public struct SignedCommand {
		public static string Sign(string itemToCheck, string keyString) {
			ASCIIEncoding encoding = new ASCIIEncoding();
			// converting key to bytes will throw an exception, need to replace '-' and '_' characters first.
			string usablePrivateKey = keyString.Replace("-", "+").Replace("_", "/");
			byte[] privateKeyBytes = Convert.FromBase64String(usablePrivateKey);
			byte[] encodedCommandBytes = encoding.GetBytes(itemToCheck);
			// compute the hash
			HMACSHA1 algorithm = new HMACSHA1(privateKeyBytes);
			byte[] hash = algorithm.ComputeHash(encodedCommandBytes);
			// convert the bytes to string and make command-safe by replacing '+' and '/' characters
			string signature = Convert.ToBase64String(hash).Replace("+", "-").Replace("/", "_");
			// Return the signature.
			return signature;
		}
	}
	class Program {
		static void Main() {
			// Note: Generally, you should store your private key someplace safe
			// and read them into your code
			const string keyString = "YOUR_PRIVATE_KEY";
			const string commandString = "COMMAND_ITEM_TO_SIGN";
			Console.WriteLine(SignedCommand.Sign(commandString,keyString));
		}
	}
}
Terms Of Use | Privacy Statement | Company info
  • © Cedac Sistemi srl
  • Head office: Via Toscana, 3 40035 Castiglione dei Pepoli (Italy) Tel.: +39 0534-93811 Fax: +39 0534-93899
  • Branch office: Via del Lavoro, 47 40033 Casalecchio di Reno (Italy)
  • Branch office: Via Parini, 1 40033 Casalecchio di Reno (Italy)
  • Share capital Euro 26,000.00 - Company Register of Bologna / VAT no / fiscal code 01695261204