[英]c# SHA256 ToBase64String of string not the same as test case
I'm working on a body payload digest for a post request.我正在处理发布请求的正文有效负载摘要。
I'm using the test case provided to try and get the same output base64 as CyberSource does using their test values and test method.我正在使用提供的测试用例来尝试获得与 CyberSource 使用其测试值和测试方法相同的输出 base64。
public static string GenerateDigest() {
var digest = "";
var bodyText = "{ your JSON payload }";
using (var sha256hash = SHA256.Create()) {
byte[] payloadBytes = sha256hash
.ComputeHash(Encoding.UTF8.GetBytes(bodyText));
digest = Convert.ToBase64String(payloadBytes);
digest = "SHA-256=" + digest;
}
return digest;
}
The test case has the following JSON payload:
测试用例具有以下 JSON 负载:
{
"clientReferenceInformation": {
"code": "TC50171_3"
},
"processingInformation": {
"commerceIndicator": "internet"
},
"paymentInformation": {
"card": {
"number": "4111111111111111",
"expirationMonth": "12",
"expirationYear": "2031",
"securityCode": "123"
}
},
"orderInformation": {
"amountDetails": {
"totalAmount": "102.21",
"currency": "USD"
},
"billTo": {
"firstName": "John",
"lastName": "Doe",
"company": "Visa",
"address1": "1 Market St",
"address2": "Address 2",
"locality": "san francisco",
"administrativeArea": "CA",
"postalCode": "94105",
"country": "US",
"email": "test@cybs.com",
"phoneNumber": "4158880000"
}
}
}
The test payload I've attempted to generate the base64 string with are the following:我尝试生成 base64 字符串的测试负载如下:
var bodyText = @"
{
""clientReferenceInformation"": {
""code"": ""TC50171_3""
},
""processingInformation"": {
""commerceIndicator"": ""internet""
},
""paymentInformation"": {
""card"": {
""number"": ""4111111111111111"",
""expirationMonth"": ""12"",
""expirationYear"": ""2031"",
""securityCode"": ""123""
}
},
""orderInformation"": {
""amountDetails"": {
""totalAmount"": ""102.21"",
""currency"": ""USD""
},
""billTo"": {
""firstName"": ""John"",
""lastName"": ""Doe"",
""company"": ""Visa"",
""address1"": ""1 Market St"",
""address2"": ""Address 2"",
""locality"": ""san francisco"",
""administrativeArea"": ""CA"",
""postalCode"": ""94105"",
""country"": ""US"",
""email"": ""test@cybs.com"",
""phoneNumber"": ""4158880000""
}
}
}
";
and..和..
var bodyjson = new
{
clientReferenceInformation =
new { code = "TC50171_3" },
processingInformation =
new {
commerceIndicator = "internet"},
paymentInformation =
new { card =
new {
number = "4111111111111111",
expirationMonth = "12",
expirationYear = "2031",
securityCode = "123"
}
},
orderInformation = new
{
amountDetails = new
{
totalAmount = "102.21",
currency = "USD"
},
billTo = new
{
firstName = "John",
lastName = "Doe",
company = "Visa",
address1 = "1 Market St",
address2 = "Address 2",
locality = "san francisco",
administrativeArea = "CA",
postalCode = "94105",
country = "US",
email = "test@cybs.com",
phoneNumber = "4158880000"
}
}
};
var bodystring = JsonConvert.SerializeObject(bodyjson, Formatting.Indented);
The Sha256 byte64 returned should be:返回的 Sha256 byte64 应该是:
SHA-256=a/goIo1XUCr80rnKFCWp7yRpwVL50E9RaunuEHh11XM=
The the test case code above is all from the Cybersource getting started guide.以上测试用例代码均来自 Cybersource 入门指南。 But my body string just does not produce the same result.
但是我的身体字符串不会产生相同的结果。
Any assistance would be appreciated.任何援助将不胜感激。
string body = "{\n" +
" \"clientReferenceInformation\": {\n" +
" \"code\": \"TC50171_3\"\n" +
" },\n" +
" \"processingInformation\": {\n" +
" \"commerceIndicator\": \"internet\"\n" +
" },\n" +
" \"orderInformation\": {\n" +
" \"billTo\": {\n" +
" \"firstName\": \"john\",\n" +
" \"lastName\": \"doe\",\n" +
" \"address1\": \"201 S. Division St.\",\n" +
" \"postalCode\": \"48104-2201\",\n" +
" \"locality\": \"Ann Arbor\",\n" +
" \"administrativeArea\": \"MI\",\n" +
" \"country\": \"US\",\n" +
" \"phoneNumber\": \"999999999\",\n" +
" \"email\": \"test@cybs.com\"\n" +
" },\n" +
" \"amountDetails\": {\n" +
" \"totalAmount\": \"10\",\n" +
" \"currency\": \"USD\"\n" +
" }\n" +
" },\n" +
" \"paymentInformation\": {\n" +
" \"card\": {\n" +
" \"expirationYear\": \"2031\",\n" +
" \"number\": \"5555555555554444\",\n" +
" \"securityCode\": \"123\",\n" +
" \"expirationMonth\": \"12\",\n" +
" \"type\": \"002\"\n" +
" }\n" +
" }\n" +
"}";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.