首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >c# SHA256 ToBase64String of string与测试用例不同

c# SHA256 ToBase64String of string与测试用例不同
EN

Stack Overflow用户
提问于 2020-11-20 03:35:44
回答 1查看 182关注 0票数 1

我正在为post请求编写body有效负载摘要。

我正在使用提供的测试用例,尝试使用它们的测试值和测试方法获得与CyberSource相同的输出base64。

代码语言:javascript
运行
复制
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;
}

测试用例具有以下JSON有效负载:

代码语言:javascript
运行
复制
{
  "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"
    }
  }
}

我尝试用来生成base64字符串的测试负载如下:

代码语言:javascript
运行
复制
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""
    }
  }
}
";

然后..。

代码语言:javascript
运行
复制
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);

返回的Sha256 byte64应为:

SHA-256=a/goIo1XUCr80rnKFCWp7yRpwVL50E9RaunuEHh11XM=

上面的测试用例代码都来自Cybersource入门指南。但是我的身体线不会产生同样的结果。

任何帮助都将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2020-11-20 17:07:46

代码语言:javascript
运行
复制
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" +
                "}";
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64918793

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档