Unable to replicate SHA1 and Base64 Output of Onvif API Example

524 views Asked by At

I've been working with C++/Qt to design a ONVIF Client, to communicate with the cameras in a given network, through XML/SOAP requests and responses.

I am currently struck trying to implement the security mechanism, for the soap requests. From the page 35 of the API guide.

We have the example..

Nonce – LKqI6G/AikKCQrN0zqZFlg==
Date – 2010-09-16T07:50:45Z
Password – userpassword
Resulting Digest – tuOSpGlFlIXsozq4HFNeeGeFLEI=

The "resulting digest" is obtained by computing this formula

Digest = B64ENCODE( SHA1( B64DECODE( Nonce ) + Date + Password ) )

I have trouble arriving at this, by using both the online tools and converter, as well as this Qt sample POC i've written.

#include <QCoreApplication>
#include <QString>
#include <QCryptographicHash>
#include <QDebug>

QString base64_encode(QString string);
QString base64_decode(QString string);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString nonce ="LKqI6G/AikKCQrN0zqZFlg==";
    QString date = "2010-09-16T07:50:45Z";
    QString pass = "userpassword";

    QString nonce_dec = base64_decode(nonce);

    QString res = nonce_dec+date+pass;
    QByteArray resBytes(res.toStdString().c_str());
    QCryptographicHash sha(QCryptographicHash::Sha1);
    sha.addData(resBytes);
    qDebug()<<"Resultant Hash is ";
    QString resHash = base64_encode(sha.result());
    qDebug()<<resHash ;

    return a.exec();
}

QString base64_encode(QString string){
    QByteArray ba;
    ba.append(string);
    return ba.toBase64();
}

QString base64_decode(QString string){
    QByteArray ba;
    ba.append(string);
    return QByteArray::fromBase64(ba);
}

Any help in this regard would be greatly appreciated.

1

There are 1 answers

0
Barath Ravikumar On BEST ANSWER

The problem seems to be the QString storing not using UTF-8 encoding as the default.

I got it to work with this modified snippet.

#include <QCoreApplication>
#include <QString>
#include <QCryptographicHash>
#include <QDebug>

QString base64_encode(QByteArray ba);
QByteArray base64_decode(QByteArray ba);

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    QString nonce ="LKqI6G/AikKCQrN0zqZFlg==";
    QString date = "2010-09-16T07:50:45Z";
    QString pass = "userpassword";   

    QByteArray nonce_dec = base64_decode(nonce.toUtf8());

    QByteArray res = nonce_dec+QByteArray(date.toUtf8())+QByteArray(pass.toUtf8());
    QByteArray resBytes(res.toStdString().c_str());
    QCryptographicHash sha(QCryptographicHash::Sha1);
    sha.addData(resBytes);
    QByteArray shaBytes = sha.result();
    qDebug()<<"Resulting Hash is ";
    QString resHash = base64_encode(shaBytes);    
    qDebug()<<resHash ;
    return a.exec();
}

QString base64_encode(QByteArray ba){
    return ba.toBase64();
}

QByteArray base64_decode(QByteArray ba){
    return QByteArray::fromBase64(ba);
}

Hope it helps anyone, who stumbles upon the same problem.