简体   繁体   中英

QString to const byte* conversion

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

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

您可以使用以下方法。

const char * QByteArray::data()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM