Question

I'm trying to use the Botan library to create a SHA-256 hash of a couple QStrings - a password and a salt. I have made many attempts trying to get a concatenated salt+password into the correct type for input into the Botan process function. The documentation for the class I'm trying to use in Botan is found at botan sha-256 documentation My latest attempt is

///
/// Computes salted data hash
///

QByteArray MyClass::HashData(QString salt, QString password)
{
    QString concatenated = salt+password;
    QByteArray input(concatenated.toLocal8Bit());
    Botan::SHA_256 sha;
    Botan::SecureVector<Botan::byte> saltedHash
            = sha.process(&input,input.count()-1);  // -1 to get rid of the \0
    return QByteArray(saltedHash);
}

When I compile I get an error: no matching function for call to 'Botan::SHA_256::process(QByteArray*, int)'... candiates are: /usr/include/botan-1.10/botan/buf_comp.h:101: Botan::SecureVector Botan::Buffered_Computation::process(const byte*, size_t)

How can I cast or copy a QString or a QByteArray into a const byte*?

EDIT: After I posted the question I tried a few more approaches. One that appears to work is attached below, but I'm uncomfortalbe using the reinterpret_cast because it seams like it could lead to problems that I'm not aware of in my c++ noob state.

Botan::SecureVector<Botan::byte>  MyClass::HashData(QString salt, QString password)
{
    QString concatenated = salt+password;
    QByteArray buffer(concatenated.toLocal8Bit());
    unsigned char * input = reinterpret_cast< unsigned char*>(buffer.data());

    Botan::SHA_256 sha;
    Botan::SecureVector<Botan::byte> saltedHash
            = sha.process(input,buffer.count()-1);  // -1 to get rid of the \0
    return (saltedHash);
}
Was it helpful?

Solution

You can use the following method.

const char * QByteArray::data()

OTHER TIPS

There were same problem: QByteArray convert to/from unsigned char *

But why don't you use reinterpret_cast, for example this way:

...
QString salt = "salt";
QString password = "password";
QString concatenated = QString("%1%2").arg(salt).arg(password);
unsigned char * input = (unsigned char *) concatenated. toLocal8Bit().data();
printf("%s", input);
...
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top