0

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);
}
DarwinIcesurfer
  • 1,073
  • 4
  • 25
  • 42
  • Your casting should be fine because buffer.data() returns char* for representing of characters (cannot be minus value). In this case, plain char and unsigned char are the same. http://stackoverflow.com/questions/2054939/char-is-signed-or-unsigned-by-default – Lwin Htoo Ko Oct 12 '12 at 04:37

2 Answers2

1

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);
...
Community
  • 1
  • 1
NG_
  • 6,895
  • 7
  • 45
  • 67
0

You can use the following method.

const char * QByteArray::data()
Lwin Htoo Ko
  • 2,326
  • 4
  • 26
  • 38
  • If I replace the line with `const unsigned char * input = const unsigned char * buffer::data();` The compiler returns an error 'conflicting declaration 'const unsigned char* input'. ( Note that an unsigned char is required.) – DarwinIcesurfer Oct 12 '12 at 04:11