Skip to content

Instantly share code, notes, and snippets.

@cfj
Created August 18, 2016 13:45
Show Gist options
  • Save cfj/6049d640f1a6654b94ee20343a6681e2 to your computer and use it in GitHub Desktop.
Save cfj/6049d640f1a6654b94ee20343a6681e2 to your computer and use it in GitHub Desktop.
public static string GetOriginProof(int amount, string currency, string userToken)
{
var uuid = Guid.NewGuid().ToString();
JObject jsonObject = new JObject();
jsonObject["amount"] = amount;
jsonObject["currency"] = currency;
jsonObject["user_token"] = userToken;
jsonObject["id"] = uuid;
var data = jsonObject.ToString();
byte[] originalData = Encoding.UTF8.GetBytes(data);
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAParameters Key = RSAalg.ExportParameters(true);
var signatureBytes = HashAndSignBytes(originalData, Key);
string signature = Convert.ToBase64String(signatureBytes);
JObject originProof = new JObject();
originProof["data"] = data;
originProof["signature"] = signature;
var originProofString = originProof.ToString();
return Convert.ToBase64String(Encoding.UTF8.GetBytes(originProofString));
}
public static byte[] HashAndSignBytes(byte[] dataToSign, RSAParameters key)
{
try
{
RSACryptoServiceProvider rsAalg = new RSACryptoServiceProvider();
rsAalg.ImportParameters(key);
return rsAalg.SignData(dataToSign, new SHA1CryptoServiceProvider());
}
catch (CryptographicException e)
{
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment