public string Sign(SignatureContext context) { var hmac = new HMACSHA1(); var secret = context.Secret.UrlEncode(); if (secret == null) secret = Guid.NewGuid().ToString(); hmac.Key = context.Encoding.GetBytes(secret); var source = new StringBuilder(); if (context.SortedQuery != null) { // append path foreach (var key in context.SortedQuery.AllKeys) { source.Append(key); source.Append(context.SortedQuery[key]); } } var buffer = context.Encoding.GetBytes(source.ToString()); var signBytes = hmac.ComputeHash(buffer); var sb = new StringBuilder(); foreach (byte b in signBytes) { sb.Append(b.ToString("x2")); } return sb.ToString(); }
public void should_equal_with_online_internet() { var mockRequestRepository = new Mock<IRequestRepository>(); mockRequestRepository.SetupGet<string>(s => s.RawUrl).Returns(DEFAULT_URL); var context = new SignatureContext(mockRequestRepository.Object, Encoding.UTF8); var md5 = new MD5Signature(); var ret = md5.Sign(context); Assert.Equal("e78ea96fcecd4a3df8ddf9d776246e6a", ret); }
public void query_string_is_empty() { var mockRequestRepository = new Mock<IRequestRepository>(); mockRequestRepository.SetupGet<string>(s => s.RawUrl).Returns("http://localhost:11944/Misc/EncUtil"); var context = new SignatureContext(mockRequestRepository.Object, Encoding.UTF8); var md5 = new MD5Signature(); var ret = md5.Sign(context); Assert.Equal("d41d8cd98f00b204e9800998ecf8427e", ret); }
public void should_equal_with_snda_result() { var mockRequestRepository = new Mock<IRequestRepository>(); mockRequestRepository.SetupGet<string>(s => s.RawUrl).Returns(DEFAULT_URL); var context = new SignatureContext(mockRequestRepository.Object, Encoding.UTF8); var sha1 = new SHA1Signature(); var ret = sha1.Sign(context); Assert.Equal("53ed4585d50783c4fa1a0f5605a299622494169f", ret); }
public void query_string_is_empty() { var mockRequestRepository = new Mock<IRequestRepository>(); mockRequestRepository.SetupGet<string>(s => s.RawUrl).Returns("http://localhost:11944/Misc/EncUtil"); var context = new SignatureContext(mockRequestRepository.Object, Encoding.UTF8); var sha1 = new SHA1Signature(); var ret = sha1.Sign(context); Assert.NotEqual("b6a59234783b8ed4afd1cd5d4230bbe0c6de13dd", ret); }
static string GetMd5Hash(SignatureContext context, MD5 md5Hash, string input) { // Convert the input string to a byte array and compute the hash. var data = md5Hash.ComputeHash(context.Encoding.GetBytes(input)); // Create a new Stringbuilder to collect the bytes // and create a string. var sb = new StringBuilder(); // Loop through each byte of the hashed data // and format each one as a hexadecimal string. for (int i = 0; i < data.Length; i++) { sb.Append(data[i].ToString("x2")); } // Return the hexadecimal string. return sb.ToString(); }
public string Sign(SignatureContext context) { var ret = string.Empty; var source = new StringBuilder(); source.Append(context.Secret); if (context.SortedQuery != null) { foreach (var key in context.SortedQuery.AllKeys) { source.Append(key); source.Append(context.SortedQuery[key]); } } using (MD5 md5Hash = MD5.Create()) { ret = GetMd5Hash(context, md5Hash, source.ToString()); } return ret; }
public string Sign(SignatureContext context) { return this.Signature.Sign(context); }