Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Need to use QCryptographicHash and convert an STL function

Need to use QCryptographicHash and convert an STL function

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 628 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • vivianV Offline
    vivianV Offline
    vivian
    wrote on last edited by
    #1

    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';
            }
        }
    
    }
    
    jsulmJ 1 Reply Last reply
    0
    • vivianV vivian

      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';
              }
          }
      
      }
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @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.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      vivianV 1 Reply Last reply
      3
      • jsulmJ jsulm

        @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.

        vivianV Offline
        vivianV Offline
        vivian
        wrote on last edited by
        #3

        @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.

        1 Reply Last reply
        0

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved