Skip to content

Instantly share code, notes, and snippets.

@thuutien
Created September 19, 2023 20:32
Show Gist options
  • Save thuutien/b5c74824fa74fc12177d2b86ae5e5297 to your computer and use it in GitHub Desktop.
Save thuutien/b5c74824fa74fc12177d2b86ae5e5297 to your computer and use it in GitHub Desktop.
Authenticate NetSuite functions
private string ComputeNonce()
{
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] data = new byte[20];
rng.GetBytes(data);
int value = Math.Abs(BitConverter.ToInt32(data, 0));
return value.ToString();
}
private long ComputeTimestamp()
{
return ((long)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds);
}
private TokenPassportSignature ComputeSignature(string compId, string consumerKey, string consumerSecret,
string tokenId, string tokenSecret, string nonce, long timestamp)
{
string baseString = compId + "&" + consumerKey + "&" + tokenId + "&" + nonce + "&" + timestamp;
string key = consumerSecret + "&" + tokenSecret;
string signature = "";
var encoding = new System.Text.ASCIIEncoding();
byte[] keyBytes = encoding.GetBytes(key);
byte[] baseStringBytes = encoding.GetBytes(baseString);
using (var hmacSha256 = new HMACSHA256(keyBytes))
{
byte[] hashBaseString = hmacSha256.ComputeHash(baseStringBytes);
signature = Convert.ToBase64String(hashBaseString);
}
TokenPassportSignature sign = new TokenPassportSignature();
sign.algorithm = "HMAC_SHA256";
sign.Value = signature;
return sign;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment