Skip to content

Instantly share code, notes, and snippets.

@query-string
Created June 16, 2015 02:40
Show Gist options
  • Save query-string/fe9f33e1abea73dd1b1d to your computer and use it in GitHub Desktop.
Save query-string/fe9f33e1abea73dd1b1d to your computer and use it in GitHub Desktop.
Sunny Portal signature generation method
using System;
using System.Text;
using System.Security.Cryptography;
using System.Web;
namespace SPortal
{
static class MainClass
{
public static string BuildSingature(String httpMethod, String apiService,
String identifier, DateTime timestamp, String signatureSecretKey)
{
String sigString = httpMethod.ToLower () + apiService.ToLower () +
timestamp.ToString ("s") + identifier.ToLower ();
// NEW: UTF-8 inspite of ASCII
UTF8Encoding encoder = new System.Text.UTF8Encoding();
// string to byte[] for secret key
byte[] encodedKey = encoder.GetBytes(signatureSecretKey);
// string to byte[] for signature
byte[] encodedSignature = encoder.GetBytes(sigString);
// create hmacsha1
HMACSHA1 hmacsha1 = new HMACSHA1(encodedKey);
//create hash
byte[] hmacHash = hmacsha1.ComputeHash(encodedSignature);
// byte[] to base64
String base64EncodedSignature = Convert.ToBase64String(hmacHash);
// ... and URL encode
String urlEncodeSignatureHashString = HttpUtility.UrlEncode(base64EncodedSignature);
return urlEncodeSignatureHashString;
}
static void Main (string[] args)
{
DateTime datetimeNow = System.DateTime.Now;
System.Console.WriteLine(datetimeNow);
System.Console.WriteLine("SPortal signature key:");
System.Console.WriteLine(BuildSingature("GET", "plantlist", "IDENTIFIER", datetimeNow, "KEY"));
}
}
}
@query-string
Copy link
Author

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