【发布时间】:2022-08-19 04:07:55
【问题描述】:
我正在尝试使用 RSA 私钥创建 sha256 signature,但我得到了 401 \"Could not authenticate in-request, auth signature : Signature verification failed: affil-product, version: 2.0.0, env: prod
我认为问题在于如何获取我的 .pem 文件。我已阅读 Microsoft 文档和提供的 Walmart 示例。我正在关注this 指南。我创建了一个非密码保护的密钥对并将公钥上传到沃尔玛。然后我将我的消费者 ID 和密钥版本添加到 appsettings.json {\"Settings\": {\"consumerID\": \"e2ca6a2f-56f2-4465-88b3-273573b1e0c9\",\"keyVer\": \"4\"}}。
然后我通过以下代码在 program.cs 中获取这些数据。
IConfiguration config = new ConfigurationBuilder()
.AddJsonFile(\"appsettings.json\")
.AddEnvironmentVariables()
.Build();
// Get values from the config given their key and their target type.
Settings settings = config.GetRequiredSection(\"Settings\").Get<Settings>();
然后我实例化 Walmart 附属对象,允许我们使用访问和读取 Walmart api 所需的方法
WalMartAfilAPI wallMartAfilAPI = new WalMartAfilAPI();
从那里我创建一个 RSACryptoServiceProvider 对象并导入 .pem 并导出参数。
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
var rsaPem = File.ReadAllText(\"D:\\\\Users\\\\Adam\\\\source\\\\repos\\\\DealsBot\\\\DealsBot\\\\DealsBot\\\\wallmartAfill\\\\WM_IO_private_key.pem\");
//now we instantiate the RSA object
var rsa = RSA.Create();
//replace the private key with our .pem
rsa.ImportFromPem(rsaPem);
//Export the key information to an RSAParameters object.
// You must pass true to export the private key for signing.
// However, you do not need to export the private key
// for verification.
RSAParameters Key = rsa.ExportParameters(true);
从这里我得到了来自 Walmart Affiliate 对象的时间戳和调用方法。
//Get current im in unix epoch milliseconds
TimeSpan t = DateTime.UtcNow - new DateTime(1970, 1, 1);
var time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds().ToString();
Console.WriteLine(time);
byte[] conicData = wallMartAfilAPI.Canonicalize(settings.KeyVer, settings.ConsumerID, time);
byte[] signedData = wallMartAfilAPI.HashAndSignBytes(conicData, Key);
if (wallMartAfilAPI.VerifySignedHash(conicData, signedData, Key))
{
Console.WriteLine(\"The data was verified\");
;
Console.WriteLine(Convert.ToBase64String(signedData));
}
else
{
这是 WalMartAfilAPI 类
namespace DealsBot.wallmartAfill
{
public class WalMartAfilAPI
{
public byte[] Canonicalize(string version, string consumerId, string timestamp)
{
ASCIIEncoding ByteConverter = new ASCIIEncoding();
// Follow after the java code, which just orders the keys/values.
StringBuilder keyBuilder = new StringBuilder();
StringBuilder valueBuilder = new StringBuilder();
SortedDictionary<string, string> dictionary = new SortedDictionary<string, string>() { { \"WM_CONSUMER.ID\", consumerId }, { \"WM_CONSUMER.INTIMESTAMP\", timestamp }, { \"WM_SEC.KEY_VERSION\", version } };
foreach (string key in dictionary.Keys)
{
keyBuilder.Append($\"{key.Trim()};\");
valueBuilder.AppendLine($\"{dictionary[key].Trim()}\");
}
string[] conHeader = { keyBuilder.ToString(), valueBuilder.ToString() };
byte[] originalData = ByteConverter.GetBytes(conHeader[1]);
return originalData;
}
public byte[] HashAndSignBytes(byte[] DataToSign, RSAParameters Key)
{
try
{
// Create a new instance of RSACryptoServiceProvider using the
// key from RSAParameters.
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.ImportParameters(Key);
// Hash and sign the data. Pass a new instance of SHA256
// to specify the hashing algorithm.
return RSAalg.SignData(DataToSign, SHA256.Create());
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return null;
}
}
public bool VerifySignedHash(byte[] DataToVerify, byte[] SignedData, RSAParameters Key)
{
try
{
// Create a new instance of RSACryptoServiceProvider using the
// key from RSAParameters.
RSACryptoServiceProvider RSAalg = new RSACryptoServiceProvider();
RSAalg.ImportParameters(Key);
// Verify the data using the signature. Pass a new instance of SHA256
// to specify the hashing algorithm.
return RSAalg.VerifyData(DataToVerify, SHA256.Create(), SignedData);
}
catch (CryptographicException e)
{
Console.WriteLine(e.Message);
return false;
}
}
}
}
-
我不熟悉 Walmart API - 但是阅读您的代码,您似乎正在使用以毫秒为单位的时间戳。默认实现通常以秒为单位。可能这会导致问题?
标签: c# .net sha256 rsacryptoserviceprovider walmart-api