LoukMouk
LoukMouk

Reputation: 512

How to get Resulting Disgest with WS-UsernameToken?

I have the following documentation from the ONVIF's Programmer Guide

onvif0

I'm currently trying to reproduce the Resulting Digest using the same entries given in the guide...

Here's my code:

private string GenerateHashedPassword(string nonce, string created, string password)
    {
        byte[] nonceBytes = Encoding.UTF8.GetBytes(nonce);
        byte[] createdBytes = Encoding.UTF8.GetBytes(created);
        byte[] passwordBytes = Encoding.UTF8.GetBytes(password);
        byte[] combined = new byte[createdBytes.Length + nonce.Length + passwordBytes.Length];
        //N-C-P
        Buffer.BlockCopy(nonceBytes, 0, combined, 0, nonceBytes.Length);
        Buffer.BlockCopy(createdBytes, 0, combined, nonceBytes.Length, createdBytes.Length);
        Buffer.BlockCopy(passwordBytes, 0, combined, nonceBytes.Length + createdBytes.Length, passwordBytes.Length);

        return Convert.ToBase64String(SHA1.Create().ComputeHash(combined));
    }

When I use my function:

string digestPassword = GenerateHashedPassword("LKqI6G/AikKCQrN0zqZFlg==","2010-09-16T07:50:45Z","userpassword");//Values from guide

My function doesn't return the same result as in the guide...

What's wrong with my function?? Why can't I get the same output??

Upvotes: 1

Views: 1748

Answers (2)

LoukMouk
LoukMouk

Reputation: 512

Thanks to @Ottavio 's help, I've figured that I didn't decode the nonce before hashing it with the rest of my entries... The code I used to get to the good result is:

private string GenerateHashedPassword(string nonce, string created, string password)
    {
        byte[] nonceBytes = Convert.FromBase64String(nonce);
        byte[] createdAndPasswordBytes = Encoding.UTF8.GetBytes(created + password);
        byte[] combined = new byte[nonceBytes.Length + createdAndPasswordBytes.Length];

        Buffer.BlockCopy(nonceBytes, 0, combined, 0, nonceBytes.Length);
        Buffer.BlockCopy(createdAndPasswordBytes, 0, combined, nonceBytes.Length, createdAndPasswordBytes.Length);

        return Convert.ToBase64String(SHA1.Create().ComputeHash(combined));
    }

Upvotes: 0

Ottavio Campana
Ottavio Campana

Reputation: 4188

The correct steps, in pseudocode, are

1. n = base64decode ("LKqI6G/AikKCQrN0zqZFlg==")
2. s = sha1 (n + "2010-09-16T07:50:45Zuserpassword")
3. resulting_digest = base64encoder (s)

As a reference, the intermediate values are:

1. n = '\x2c\xaa\x88\xe8\x6f\xc0\x8a\x42\x82\x42\xb3\x74\xce\xa6\x45\x96'
2. s = '\xb6\xe3\x92\xa4\x69\x45\x94\x85\xec\xa3\x3a\xb8\x1c\x53\x5e\x78x\67\x85\x2c\x42'

You don't post the result you get, but I think you are using the base64-encoded version of the nonce in the binary string that you are hashing with sha1.

Upvotes: 1

Related Questions