Results 1 to 3 of 3

Thread: QCryptographicHash an entire file

  1. #1
    Join Date
    Oct 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QCryptographicHash an entire file

    Hello all!

    I am successfully hashing an entire file, however, my method seems to produce different results for different block sizes. Let me explain...

    My fear is that I don't want to use a Qt data structure to store an entire file to memory, since I want to hash arbitrarily large files. Hence, I am chunking the file into pieces of 'blocksize', and then using addData() to add the data to the hash.

    The problem is that for different values of 'blocksize', I am getting a different hash. Any ideas?

    Qt Code:
    1. FILE * fd = fopen(fileName.toAscii().data(), "rb");
    2. if (!fd) return;
    3.  
    4.  
    5. fseek(fd, 0, SEEK_END);
    6. long fSize = ftell(fd);
    7. fseek(fd, 0, SEEK_SET);
    8.  
    9. QCryptographicHash krypo(QCryptographicHash::Sha1);
    10. const long blockSize = 8192;
    11. char block[blockSize];
    12. memset(block, 0, blockSize);
    13. fread(block, 1, blockSize, fd);
    14. long c = fSize;
    15. while( c > 0) {
    16.  
    17. krypo.addData(block, (c < blockSize) ? c : blockSize);
    18. c-=(c < blockSize) ? c : blockSize;
    19. }
    20. QByteArray hash = krypo.result();
    To copy to clipboard, switch view to plain text mode 

    I just noticed a bug. I guess it takes posting it to find. I'm not fread'ing in the while loop. I'll fix the bug and see if it works, and post the result.


    Added after 14 minutes:


    Ok, this code works if anyone is interested. Also, please let me know if I should not be using fread, fseek, etc. I hope those work on windows (I use Linux and OS X).

    Qt Code:
    1. fseek(fd, 0, SEEK_END);
    2. hashee.fSize = ftell(fd);
    3. fseek(fd, 0, SEEK_SET);
    4.  
    5. QCryptographicHash krypo(QCryptographicHash::Sha1);
    6. const long blockSize = 8192;
    7. char block[blockSize];
    8. long c = hashee.fSize;
    9. while( c > 0) {
    10. fread(block, 1, (c < blockSize) ? c : blockSize, fd);
    11. krypo.addData(block, (c < blockSize) ? c : blockSize);
    12. c-=(c < blockSize) ? c : blockSize;
    13. }
    14. QByteArray hash = krypo.result();
    To copy to clipboard, switch view to plain text mode 
    Last edited by pyramation; 3rd November 2010 at 18:58.

  2. The following user says thank you to pyramation for this useful post:

    posixprogrammer (9th May 2011)

  3. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QCryptographicHash an entire file

    Why do you complicate things?
    Qt Code:
    1. QCryptographicHash crypto(QCryptographicHash::Sha1);
    2. QFile file(fileName);
    3. file.open(QFile::ReadOnly);
    4. while(!file.atEnd()){
    5. crypto.addData(file.read(8192));
    6. }
    7. QByteArray hash = crypto.result();
    To copy to clipboard, switch view to plain text mode 
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  4. The following 2 users say thank you to wysota for this useful post:

    ArkKup (22nd April 2013), Cupidvogel (8th February 2016)

  5. #3
    Join Date
    Oct 2010
    Posts
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QCryptographicHash an entire file

    @wysota :

    Thanks for the tip!

Similar Threads

  1. QHttp.get() is failing to download the entire file
    By WinchellChung in forum Newbie
    Replies: 0
    Last Post: 19th February 2010, 20:58
  2. QCryptographicHash Question
    By tntcoda in forum Qt Programming
    Replies: 2
    Last Post: 21st October 2008, 09:21
  3. QCryptographicHash
    By live_07 in forum Qt Programming
    Replies: 5
    Last Post: 21st June 2008, 18:26
  4. Building the entire project
    By vermarajeev in forum Qt Programming
    Replies: 14
    Last Post: 28th February 2007, 03:00
  5. Accessing the entire file..
    By Kapil in forum Newbie
    Replies: 1
    Last Post: 28th April 2006, 07:41

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.