-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSecurity.cs
More file actions
45 lines (37 loc) · 1.5 KB
/
Security.cs
File metadata and controls
45 lines (37 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Collections;
using System.Text;
using System.Security.Cryptography;
using System.Collections.Specialized;
namespace secureacceptance
{
public static class Security
{
private const String SECRET_KEY = "<REPLACE WITH SECURITY KEY>";
public static String sign(IDictionary<string, string> paramsArray) {
return sign(buildDataToSign(paramsArray), SECRET_KEY);
}
private static String sign(String data, String secretKey) {
UTF8Encoding encoding = new System.Text.UTF8Encoding();
byte[] keyByte = encoding.GetBytes(secretKey);
HMACSHA256 hmacsha256 = new HMACSHA256(keyByte);
byte[] messageBytes = encoding.GetBytes(data);
return Convert.ToBase64String(hmacsha256.ComputeHash(messageBytes));
}
private static String buildDataToSign(IDictionary<string,string> paramsArray) {
String[] signedFieldNames = paramsArray["signed_field_names"].Split(',');
IList<string> dataToSign = new List<string>();
foreach (String signedFieldName in signedFieldNames)
{
dataToSign.Add(signedFieldName + "=" + paramsArray[signedFieldName]);
}
return commaSeparate(dataToSign);
}
private static String commaSeparate(IList<string> dataToSign) {
return String.Join(",", dataToSign);
}
}
}