2009-11-14 12 views
8

Sto provando a testare l'API da ankoder.com e ho problemi nel calcolo del digest per lo authentication token. Il campione è rubino mentre sto provando a chiamare da C#. Quando confronto il risultato del digest tra HMAC-SHA1, ho riscontrato problemi con il risultato della passkey.hmac-sha1 in ruby ​​differisce da C# HMACSHA1

Per rendere più facile testare ecco il codice:

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 

che mi dà il risultato: "X/0EngsTYf7L8e7LvoihTMLetlM = \ n" Se provo questo in C# con il seguente:

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(); 

il risultato sale è la stessa, ma il risultato chiave di accesso è diversi- C# mi dà:

QLC68XjQlEBurwbVwr7euUfHW/k =

0.123.

Entrambi genera il sale: f5cab5092f9271d43d2e

Qualunque buona idea di cosa è successo?

risposta

10

Hai inserito PrivateKey e salt nelle posizioni sbagliate nel tuo codice C#; per il tuo codice Ruby, PrivateKey è pensato per essere la chiave segreta dell'HMAC.

Ricorda inoltre che hai incluso una nuova riga alla fine dell'hash prodotta dal tuo programma Ruby (in base all'output di esempio, comunque). È necessario non includere la nuova riga o gli hash non corrispondono.

Questo C# programma risolve il primo numero:

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(); 
    } 
    } 
} 
+0

Basta chiedersi, quale risultato si ottiene con questo? – quantumpotato

+0

X/0EngsTYf7L8e7LvoihTMLetlM = –

3

vedo 2 problemi,

  1. Hai avuto chiave/dati invertiti. In Ruby, private_key è la chiave e il sale è i dati. In C#, hai fatto il contrario.
  2. Se non è consentito ASCII in nessuna delle stringhe, è necessario assicurarsi di utilizzare la stessa codifica. Ruby tratta tutto come byte non elaborati, quindi il C# deve corrispondere alla sua codifica. Se viene utilizzato jcode, la codifica in C# deve corrispondere a $ KCODE.
Problemi correlati