English 中文(简体)
hmac-sha1 in ruby differs from C# HMACSHA1
原标题:

I am trying to test the API from ankoder.com and have problem on the digest calculation for the authentication token . The sample is ruby while I am trying to call from C#. When I compare the digest result between in HMAC-SHA1, I got issues with the passkey result.

To make it easy to test here is the code:

require  hmac-sha1 
require  digest/sha1 
require  base64 
token="-Sat, 14 Nov 2009 09:47:53 GMT-GET-/video.xml-"
private_key="whatever"
salt=Digest::SHA1.hexdigest(token)[0..19]
passkey=Base64.encode64(HMAC::SHA1.digest(private_key, salt)).strip

Which gives me the result: "X/0EngsTYf7L8e7LvoihTMLetlM= " If I try this in C# with the following:

const string PrivateKey = "whatever";

var date = "Sat, 14 Nov 2009 09:47:53 GMT";//DateTime.Now.ToUniversalTime().ToString("ddd, dd MMM yyyy HH:mm:ss") + " GMT";
string token=string.Format("-{0}-GET-/video.xml-", date);

var salt_binary=SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(token));
var salt_hex=BitConverter.ToString(salt_binary).Replace("-", "").ToLower();
var salt =salt_hex.Substring(0,20);

var hmac_sha1 =
            new HMACSHA1(Encoding.ASCII.GetBytes(salt));
hmac_sha1.Initialize();

var private_key_binary = Encoding.ASCII.GetBytes(PrivateKey);
var passkey_binary = hmac_sha1.ComputeHash(private_key_binary,0,private_key_binary.Length);

var passkey = Convert.ToBase64String(passkey_binary).Trim();

The salt result is the same, but the passkey result is different- C# gives me :

QLC68XjQlEBurwbVwr7euUfHW/k=

Both generates the salt: f5cab5092f9271d43d2e

Any good idea what has happened?

最佳回答

You ve put PrivateKey and salt in the wrong positions in your C# code; per your Ruby code, PrivateKey is meant to be the HMAC s secret key.

Also be aware that you ve included a newline at the end of the hash produced by your Ruby program (according to your sample output, anyway). You must not include the newline or the hashes won t match.

This C# program corrects the first issue:

using System;
using System.Security.Cryptography;
using System.Text;

namespace Hasher
{
  class Program
  {
    static void Main(string[] args)
    {
      const string PrivateKey = "whatever";

      string date = "Sat, 14 Nov 2009 09:47:53 GMT";
      string token = string.Format("-{0}-GET-/video.xml-", date);

      byte[] salt_binary = SHA1.Create().ComputeHash(Encoding.ASCII.GetBytes(token));
      string salt_hex = BitConverter.ToString(salt_binary).Replace("-", "").ToLower();
      string salt = salt_hex.Substring(0, 20);

      HMACSHA1 hmac_sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(PrivateKey));
      hmac_sha1.Initialize();

      byte[] private_key_binary = Encoding.ASCII.GetBytes(salt);
      byte[] passkey_binary = hmac_sha1.ComputeHash(private_key_binary, 0, private_key_binary.Length);

      string passkey = Convert.ToBase64String(passkey_binary).Trim();
    }
  }
}
问题回答

I see 2 issues,

  1. You got key/data reversed. In Ruby, private_key is the key and the salt is the data. In C#, you did the opposite.
  2. If non-ASCII is allowed in any of your strings, you have to make sure you use same encoding. Ruby treats everything as raw bytes so the C# has to match its encoding. If jcode is used, the encoding in C# should match $KCODE.




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签