You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

88 lines
2.7 KiB

using System;
using System.Security.Cryptography;
using System.Text;
using LitJson;
namespace Sog.Crypto
{
public enum HuaweiPaySignatureType
{
SHA256WithRSA = 1,
SHA256WithRSA_PSS = 2,
}
public static class HuaweiPaySecurity
{
public static bool VerifyRsaSign(String content, String sign, String publicKey,
HuaweiPaySignatureType sigType)
{
bool checkRet = false;
using (var rsaProv = RSA.Create())
{
byte[] contentBytes = Encoding.UTF8.GetBytes(content);
byte[] signBytes = Convert.FromBase64String(sign);
byte[] publicKeyBytes = Convert.FromBase64String(publicKey);
try
{
int readBytes = 0;
rsaProv.ImportSubjectPublicKeyInfo(publicKeyBytes, out readBytes);
if (sigType == HuaweiPaySignatureType.SHA256WithRSA)
{
checkRet = rsaProv.VerifyData(contentBytes, signBytes,
HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
}
else if (sigType == HuaweiPaySignatureType.SHA256WithRSA_PSS)
{
checkRet = rsaProv.VerifyData(contentBytes, signBytes,
HashAlgorithmName.SHA256, RSASignaturePadding.Pss);
}
else
{
TraceLog.Error("HuaweiPaySecurity.VerifyRsaSign invalid sign algorithm");
}
}
catch (CryptographicException e)
{
TraceLog.Exception(e);
}
finally
{
rsaProv.Clear();
}
}
TraceLog.Debug("HuaweiPaySecurity.VerifyRsaSign {0} ret {1}", sigType, checkRet);
return checkRet;
}
public static void GetOrderIdFromPurchaseData(string purchaseData,
out string orderId, out string orderId3rd)
{
orderId = null;
orderId3rd = null;
JsonData jsonData = JsonMapper.ToObject(purchaseData);
if (jsonData == null)
{
return;
}
JsonData payload = jsonData["developerPayload"];
if (payload != null)
{
orderId = payload.ToString();
}
JsonData order3rd = jsonData["orderId"];
if (order3rd != null)
{
orderId3rd = order3rd.ToString();
}
}
}
}