本文介绍了使用Crypto ++生成ONVIF身份验证摘要吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ONVIF 身份验证规范第6.1.1.3节对如何生成摘要具有直截了当的描述.但是,当对Base64和SHA1操作使用Crypto ++时,我无法生成与规范相同的哈希.我一定做错了,但是看不到.

The ONVIF authentication spec section 6.1.1.3 has what looks like a straight forward description of how to generate a digest. However, when using Crypto++ for Base64 and SHA1 operations, I cannot generate the same hash as the specification. I must be doing something wrong but cannot see what.

std::string nonce = "LKqI6G/AikKCQrN0zqZFlg==";
std::string dt = "2010-09-16T07:50:45Z";
std::string pwd = "userpassword";

{
    // result should be tuOSpGlFlIXsozq4HFNeeGeFLEI=
    // as per spec. This approach is also used here:
    // https://github.com/agsh/onvif/blob/master/lib/cam.js

    CryptoPP::Base64Decoder decoder;
    decoder.Put((byte*)nonce.data(), nonce.size());
    std::vector<uint8_t> bytes(decoder.MaxRetrievable(),0);
    decoder.Get(&bytes[0],bytes.size());

    //
    CryptoPP::SHA1 hash;
    byte digest[CryptoPP::SHA1::DIGESTSIZE];
    hash.Update(bytes.data(), bytes.size());
    hash.Update((const byte*)dt.c_str(), dt.size());
    hash.Update((const byte*)pwd.c_str(), pwd.size());
    hash.Final(digest);

    CryptoPP::Base64Encoder encoder;
    encoder.Put(digest, CryptoPP::SHA1::DIGESTSIZE);
    std::string hash64(encoder.MaxRetrievable(), 0);
    encoder.Get((byte*)hash64.data(), hash64.size());
    // generates woEIuU+ryXxcwkTZ9ktbKGeQ
    std::cout << hash64 << std::endl;
}

对此有任何想法.

推荐答案

CryptoPP::Base64Decoder decoder;
decoder.Put((byte*)nonce.data(), nonce.size());
std::vector<uint8_t> bytes(decoder.MaxRetrievable(),0);
decoder.Get(&bytes[0],bytes.size());

致电MessageEnd:

Base64Decoder decoder;
decoder.Put((byte*)nonce.data(), nonce.size());
decoder.MessageEnd();

vector<uint8_t> bytes(decoder.MaxRetrievable(),0);
decoder.Get(&bytes[0],bytes.size());

同上:

Base64Encoder encoder;
encoder.Put(digest, 20);
encoder.MessageEnd();

string hash64(encoder.MaxRetrievable(), 0);
encoder.Get((byte*)hash64.data(), hash64.size());

另请参见 Base64Encoder |丢失数据 Base64Decoder | Crypto ++ Wiki上的数据丢失.

我不像Crypto ++一样了解C#,因此我无法提供一个可以在ONVIF身份验证中正常工作的C#示例.

I don't know C# as well as Crypto++, so I can't help with a C# example that works as expected with ONVIF authentication.

这是我得到的结果:

$ g++ test.cxx -I. ./libcryptopp.a -o test.exe
$ ./test.exe
tuOSpGlFlIXsozq4HFNeeGeFLEI=

cat test.cxx:

#include <iostream>
#include <string>
#include <vector>

#include "base64.h"
#include "sha.h"

std::string nonce = "LKqI6G/AikKCQrN0zqZFlg==";
std::string dt = "2010-09-16T07:50:45Z";
std::string pwd = "userpassword";

int main(int argc, char* argv[])
{
    CryptoPP::Base64Decoder decoder;
    decoder.Put((byte*)nonce.data(), nonce.size());
    decoder.MessageEnd();
    std::vector<uint8_t> bytes(decoder.MaxRetrievable(),0);
    decoder.Get(&bytes[0],bytes.size());

    CryptoPP::SHA1 hash;
    byte digest[CryptoPP::SHA1::DIGESTSIZE];
    hash.Update(bytes.data(), bytes.size());
    hash.Update((const byte*)dt.c_str(), dt.size());
    hash.Update((const byte*)pwd.c_str(), pwd.size());
    hash.Final(digest);

    CryptoPP::Base64Encoder encoder;
    encoder.Put(digest, 20);
    encoder.MessageEnd();
    std::string hash64(encoder.MaxRetrievable(), 0);
    encoder.Get((byte*)hash64.data(), hash64.size());

    std::cout << hash64 << std::endl;

    return 0;
}

这篇关于使用Crypto ++生成ONVIF身份验证摘要吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-09 10:40