Created
June 16, 2015 02:40
-
-
Save query-string/fe9f33e1abea73dd1b1d to your computer and use it in GitHub Desktop.
Sunny Portal signature generation method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://www.sunnyportal.com API signature generation
Ruby implementation: https://gist.github.com/query-string/888c791d1f34748a0cbf