这是我的代码,我应该使用什么格式的输入字符串或输入字符串的实际格式是什么?prn号码应该是多少?或者我们可以假设任何 10 位数字作为 prn 来获得输出?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
namespace HMCApp2
{
class Sha1HashProgram
{
static void Main(string[] args)
{
string key = "<my private key>";
// string data = "'mrj'.'json'.'539ff0f815ca697c681fe01d32ba52e3'";
string data = "json539ff0f815ca697c681fe01d32ba52e31234567890";
string secret = Sha1HashProgram.ShaHash(data, key);
Console.WriteLine(secret);
Console.ReadKey();
}
static string ShaHash(string value, string key)
{
using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(key)))
{
return ByteToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(value)));
}
}
static string ByteToString(IEnumerable<byte> data)
{
return string.Concat(data.Select(b => b.ToString("x2")));
}
}
}
我也以另一种方式尝试过,但仍然无法正常工作,这是我的另一个代码,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
namespace ConsoleApplication1
{
class Program
{
public static string Encode(string input, byte[] key)
{
HMACSHA1 myhmacsha1 = new HMACSHA1(key);
byte[] byteArray = Encoding.ASCII.GetBytes(input);
MemoryStream stream = new MemoryStream(byteArray);
return myhmacsha1.ComputeHash(stream).Aggregate("", (s, e) => s + String.Format("{0:x2}", e), s => s);
}
static void Main(string[] args)
{
byte[] key = Encoding.ASCII.GetBytes("my private key");
string input = "";
// foreach (string s in new string[] { "Marry", " had", " a", " little", " //lamb" })
// {
// input ="'mrj'.'json'.'539ff0f815ca697c681fe01d32ba52e3'";
input = "json539ff0f815ca697c681fe01d32ba52e31234567890";
System.Console.WriteLine(Encode(input, key));
// }
return;
}
}
}
我按照马丁所说的那样尝试过,现在我得到了 HMAC 签名,但是当我将它粘贴到 URL 时它没有向我显示 json,
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Cryptography;
using System.IO;
using System.Web;
using System.Globalization;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
namespace HMCApp2
{
class Sha1HashProgram
{
static void Main(string[] args)
{
string key = "<my private key>";
// string data = "json539ff0f815ca697c681fe01d32ba52e31234567890";
string data = "539ff0f815ca697c681fe01d32ba52e3";
string secret = Sha1HashProgram.ShaHash(data, key);
Console.WriteLine(secret);
// Console.ReadKey();
}
static string ShaHash(string value, string key)
{
// using (var hmac = new HMACSHA1(Encoding.ASCII.GetBytes(key)))
using (var hmac = new HMACSHA1(StringToBytes(key)))
{
// return ByteToString(hmac.ComputeHash(Encoding.ASCII.GetBytes(value)));
return ByteToString(hmac.ComputeHash(StringToBytes(value)));
}
}
static string ByteToString(IEnumerable<byte> data)
{
return string.Concat(data.Select(b => b.ToString("x2")));
}
static Byte[] StringToBytes(String hexString)
{
return Enumerable.Range(0, hexString.Length / 2).Select(i => (Byte)Int32.Parse(hexString.Substring(2 * i, 2), NumberStyles.HexNumber)).ToArray();
}
}
}