Generating a SHA1 hash

Hi everyoe,

I'm trying to connect with a webService, and I must encode a part of the header with SHA1 encoding. I have an example in .NET, and I'm trying to convert it into C/AL, but I'm receiving an error from the webService. The code in .NET is this:

byte[] key = Encoding.UTF8.GetBytes(secret);
byte[] hmac_encode = Sha1Hash_Raw(concat, key);
string hmac_encode_base64 = System.Convert.ToBase64String(hmac_encode);

private static byte[] Sha1Hash_Raw(string input, byte[] key)
{
    HMACSHA1 sha1 = new HMACSHA1(key);
    byte[] byteArray = Encoding.UTF8.GetBytes(input);

    MemoryStream stream = new MemoryStream(byteArray);
    return sha1.ComputeHash(stream);
}
        

The value I must include into the header of the HTTP request is "hmac_encode_base64"

This is how I'm trying to generate the hash in NAV:

key := Encoding.UTF8.GetBytes(secret);
hmacSHA1 := hmacSHA1.Create;
hmacSHA1.Key  := key;
hmac_encode := hmacSHA1.ComputeHash(Encoding.UTF8.GetBytes(concat));
hmac_encode_base64 := Convert.ToBase64String(hmac_encode);

This are the variables:

secret    Text        
key    DotNet    System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'    
hmac_encode_base64    Text        
hmacSHA1    DotNet    System.Security.Cryptography.HMACSHA1.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'  
concat    Text        
hmac_encode    DotNet    System.Array.'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'   

Does anyone see any mistake or something I'm loosing?

Thnak you very much

Parents Reply
  • Thank you both for your answer.

    , the issue of the DONTNET variable is solved,l it was my mistake.

    , I've tried to instantiate the hmacSHA1 has you've told me, but the result is the same. So I have 3 different (4) ways to translate this code to C A/L:

    byte[] key = Encoding.UTF8.GetBytes(secret);
    byte[] hmac_encode = Sha1Hash_Raw(concat, key);
    string hmac_encode_base64 = System.Convert.ToBase64String(hmac_encode);

    Option 1, using DOTNET variable:

     key := Encoding.UTF8.GetBytes(secret);
     //hmacSHA1 := hmacSHA1.Create;
     //hmacSHA1.Key  := key;
     hmacSHA1 := hmacSHA1.HMACSHA1(key);
     hmac_encode := hmacSHA1.ComputeHash(Encoding.UTF8.GetBytes(concat));
     hmac_encode_base64 := Convert.ToBase64String(hmac_encode);

    Option 2&3, using function from CU 1266 in NAV BC:

    key := Encoding.UTF8.GetBytes(secret);
    TryGenerateKeyedHash(hmac_encode,concat,key,'HMACSHA1');
    hmac_encode_base64 := Convert.ToBase64String(hmac_encode);

    hmac_encode_base64 := GenerateKeyedHashAsBase64String(concat,secret,1);

    All options give the same result. But I think, that probably has the clue. You have talk about the binary representation.

    From support, they don't know ab out NAV, but they've told me to consider carefully that the function in C# is returning a RAW value... Can it have anything to do with?

    Thank you for your tips, really appreciated

Children
Related
Recommended