Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Need to use QCryptographicHash and convert an STL function



  • I am totally confused with the changes that need to be done to convert an STL function.

    I tried the following: https://stackoverflow.com/a/31012627/1006821

    but the conversion of wstringDevicelocking is just extremely confusing. I have never used STL so the begin and end. I am guessing they are iterators.

    Any help would be useful, Thanks.

    string AuthFile::EncrptedFileName(std::string& userName, std::string& orgName,string& strDeviceLocking)
    { 
      string result;
      string hashVairable = "#" ;
      string hashValue(hashVairable + orgName + hashVairable + "::" + hashVairable + userName + hashVairable);
    
    std::wstring wstringDevicelocking(strDeviceLocking.length(), L' ');
    std::copy(strDeviceLocking.begin(), strDeviceLocking.end(), wstringDevicelocking.begin());
    wstring guid = L"258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
    std::string keyaccesStr(hashValue);
    wstring keyaccessGuid(keyaccesStr.begin(), keyaccesStr.end());
    keyaccessGuid.append(guid);
    keyaccessGuid.append(wstringDevicelocking);
    string utf8String = wstring_to_utf8(keyaccessGuid);
    
    	 unsigned char digestStr2[SHA_DIGEST_LENGTH];
    	 SHA1((unsigned char*)utf8String.c_str(), strlen(utf8String.c_str()), (unsigned char*)&digestStr2);
    	 char* utf8StrSha1Base64 = convertToBase64(digestStr2, SHA_DIGEST_LENGTH);
    	 string sha1DataStr(utf8StrSha1Base64);
    
         //Remove all characters except alphabets
         removeForbiddenChar(&sha1DataStr);
    
         result = sha1DataStr;
    
    	 //printf("\n result [%s] \n", result.c_str());
    
         return result;
    }
    
    
    void AuthFile::removeForbiddenChar(string *s)
    {
        string::iterator it;
    
        for (it = s->begin(); it < s->end(); ++it){
            switch (*it){
            case '/':case '\\':case ':':case '?':case '"':case '<':case '>':case '|':case '=':
                *it = 'B';
            }
        }
    
    }
    

  • Qt Champions 2019

    @vivian said in Need to use QCryptographicHash and convert an STL function:

    I am guessing they are iterators

    Yes, they are.

    std::copy(strDeviceLocking.begin(), strDeviceLocking.end(), wstringDevicelocking.begin());
    

    copies everything from strDeviceLocking (hence begin and end) to wstringDevicelocking.



  • @jsulm Thank you for that.. I just did a straight up copy.

    Now just stuck at the last part where the SHA1 and then converting to base64

    string utf8String = wstring_to_utf8(keyaccessGuid);
    
    	 unsigned char digestStr2[SHA_DIGEST_LENGTH];
    	 SHA1((unsigned char*)utf8String.c_str(), strlen(utf8String.c_str()), (unsigned char*)&digestStr2);
    	 char* utf8StrSha1Base64 = convertToBase64(digestStr2, SHA_DIGEST_LENGTH);
    	 string sha1DataStr(utf8StrSha1Base64);
    
         //Remove all characters except alphabets
         removeForbiddenChar(&sha1DataStr);
    

    This is what I am trying to do.

    { ...
    QString utf8String = keyaccessGuid.toUtf8();
    QByteArray resBytes(utf8String.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 ;
    }
    
    QString base64_encode(QByteArray ba){
        return ba.toBase64();
    }
    
    QByteArray base64_decode(QByteArray ba){
        return QByteArray::fromBase64(ba);
    }
    

    I am hoping I am on the right track.


Log in to reply