代码之家  ›  专栏  ›  技术社区  ›  LoukMouk

如何使用WS-UsernameToken获得结果Disgest?

  •  1
  • LoukMouk  · 技术社区  · 7 年前

    我从 ONVIF's Programmer Guide

    onvif0

    我目前正在尝试复制 结果摘要 使用指南中给出的相同条目。。。

    这是我的代码:

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

    当我使用我的功能时:

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

    我的函数返回的结果与指南中的结果不同。。。

    我的函数有什么问题?? 为什么我不能得到相同的输出??

    2 回复  |  直到 7 年前
        1
  •  1
  •   Ottavio Campana    7 年前

    在伪代码中,正确的步骤是

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

    作为参考,中间值为:

    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'
    

    您不会发布得到的结果,但我认为您正在使用sha1散列的二进制字符串中的nonce的base64编码版本。

        2
  •  0
  •   LoukMouk    7 年前

    多亏了@Ottavio的帮助,我发现在将nonce与其余条目进行哈希运算之前,我没有解码它。。。我用来获得好结果的代码是:

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