C# SHA512 到十六进制字符串
C# SHA512 to Hex string
有一个名为 Crypto-js 的 javascript 库,我正在尝试将我使用的一些方法转换为 c#。
例如 javascript:
var payload = JSON.stringify({ market: "BTC-ETH", order: { price: "0.02159338", side: "buy", size: "0.024" } });
var contentHash = cryptoJS.SHA512(payload).toString(cryptoJS.enc.Hex);
console.log(contentHash);
在 C# 中
public class OrdersVM
{
public string Market { get; set; }
public Order Order { get; set; }
}
public class Order
{
public string Price { get; set; }
public string Side { get; set; }
public string Size { get; set; }
}
public async Task<IActionResult> Orders([FromBody] OrdersVM vm) {
var payload = JsonConvert.SerializeObject(vm);
var contentHash = sha512Hex(payload).ToLower();
Console.WriteLine(contentHash);
}
public string sha512Hex(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using (var hash = SHA512.Create())
{
hash.ComputeHash(bytes);
return BitConverter.ToString(hash.Hash).Replace("-", "");
}
}
contentHash
对于 javascript 是
"99bb05af8aace509189e08625bb4e475a9daaafc92edf5c85fa1aefcc16c16e4533c23843c5806aef01c97e8cb4150b2dc129d04d3b6a50331833fe5cb8158fc"
对于 c#
"731b92cf482ff90ffe759e356959ec005334062bdc3c2cc78b48c3041d21a45ecaa6b33f6df2971fa868f94f04b7596e818104cb1017ed1c436365beac3a01d1"
我在 C# 转换中做错了什么?
问题是 JSON.Net,默认情况下,会将您的 属性 名称序列化为与显示完全相同的名称,这意味着它们都以大写字母开头。有两种方法可以解决此问题:
使用JsonProperty
明确控制属性 名称序列化。例如:
public class OrdersVM
{
[JsonProperty("market")]
public string Market { get; set; }
[JsonProperty("order")]
public Order Order { get; set; }
}
使用 contract resolver to tell JSON.Net how to process the names. Fortunately there is one provided 可以做到这一点:
var settings = new JsonSerializerSettings
{
ContractResolver = new CamelCasePropertyNamesContractResolver()
}
var payload = JsonConvert.SerializeObject(vm, settings);
有一个名为 Crypto-js 的 javascript 库,我正在尝试将我使用的一些方法转换为 c#。
例如 javascript:
var payload = JSON.stringify({ market: "BTC-ETH", order: { price: "0.02159338", side: "buy", size: "0.024" } });
var contentHash = cryptoJS.SHA512(payload).toString(cryptoJS.enc.Hex);
console.log(contentHash);
在 C# 中
public class OrdersVM
{
public string Market { get; set; }
public Order Order { get; set; }
}
public class Order
{
public string Price { get; set; }
public string Side { get; set; }
public string Size { get; set; }
}
public async Task<IActionResult> Orders([FromBody] OrdersVM vm) {
var payload = JsonConvert.SerializeObject(vm);
var contentHash = sha512Hex(payload).ToLower();
Console.WriteLine(contentHash);
}
public string sha512Hex(string input)
{
var bytes = Encoding.UTF8.GetBytes(input);
using (var hash = SHA512.Create())
{
hash.ComputeHash(bytes);
return BitConverter.ToString(hash.Hash).Replace("-", "");
}
}
contentHash
对于 javascript 是
"99bb05af8aace509189e08625bb4e475a9daaafc92edf5c85fa1aefcc16c16e4533c23843c5806aef01c97e8cb4150b2dc129d04d3b6a50331833fe5cb8158fc"
对于 c#
"731b92cf482ff90ffe759e356959ec005334062bdc3c2cc78b48c3041d21a45ecaa6b33f6df2971fa868f94f04b7596e818104cb1017ed1c436365beac3a01d1"
我在 C# 转换中做错了什么?
问题是 JSON.Net,默认情况下,会将您的 属性 名称序列化为与显示完全相同的名称,这意味着它们都以大写字母开头。有两种方法可以解决此问题:
使用
JsonProperty
明确控制属性 名称序列化。例如:public class OrdersVM { [JsonProperty("market")] public string Market { get; set; } [JsonProperty("order")] public Order Order { get; set; } }
使用 contract resolver to tell JSON.Net how to process the names. Fortunately there is one provided 可以做到这一点:
var settings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() } var payload = JsonConvert.SerializeObject(vm, settings);