// Returns either NameValueCollection of the signed data or NULL when request is not valid
private NameValueCollection getSignedRequestData()
{
// Shared secret key between parties for signing requests
String hmac_secret = "V3ryS3cureP@ssw0rd";
// Get request parameters
NameValueCollection requestData = Request.RequestType == "POST" ? Request.Form : Request.QueryString;
// Remove signature_hash from query string for generating a signature
String signedData = requestData.ToString();
signedData = Regex.Replace(signedData, "&signature_hash=(.*?)$", "");
// Calculate signature from requesr data and convert it to hex representation
HMACSHA256 hmac = new HMACSHA256(Encoding.ASCII.GetBytes(hmac_secret));
String calculatedSignature = BitConverter.ToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(signedData))).Replace("-", "");
if (requestData["signature_hash"] == calculatedSignature)
{
return requestData;
}
return null;
}