Results 1 to 20 of 23

Thread: Calculate MD5 sum of a big file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Calculate MD5 sum of a big file

    Hi all,

    I need a md5 sum of a file with a big size. I found this:

    Qt Code:
    1. QFile file("/home/chris/backup.img");
    2.  
    3. if (file.open(QIODevice::ReadOnly)) {
    4. QByteArray fileData = file.readAll();
    5. QByteArray hashData = QCryptographicHash::hash(fileData, QCryptographicHash::Md5);
    6.  
    7. qDebug() << hashData.toHex();
    8. }
    To copy to clipboard, switch view to plain text mode 

    but with a file size > 8 GB it's a memory overkill!

    Are there any other methods?

    Thanks!
    Chris

  2. #2
    Join Date
    Dec 2008
    Location
    Poland
    Posts
    383
    Thanks
    52
    Thanked 42 Times in 42 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Android

    Default Re: Calculate MD5 sum of a big file

    QCryptographicHash::hash() is static "helper" function, and what You are looking for is QCryptographicHash::addData().

    This was answered here i.e. http://www.qtcentre.org/threads/3567...an-entire-file
    In the near future - corporate networks reach out to the stars. Electrons and light flow throughout the universe.
    The advance of computerization however, has not yet wiped out nations and ethnic groups.

  3. #3
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    That works, thanks for the hint!

    Is there a way to calculate md5 sum from a dvd (/dev/cdrom) with this method? It musts read the dvd raw data, not files on disc!

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Calculate MD5 sum of a big file

    Open and read the /dev/cdrom device directly: you get the bytes from one end of the disc to the other.

  5. #5
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    How? QFile doesn't work!

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Calculate MD5 sum of a big file

    Sure it does, but QIODevice::atEnd() is not reliable on block special files so you have to adopt a slightly different approach:
    Qt Code:
    1. QFile in("/dev/cdrom");
    2. if (in.open(QIODevice::ReadOnly)) {
    3. qDebug() << "Opened ok";
    4. char buf[2048];
    5. int bytesRead;
    6. while ((bytesRead = in.read(buf, 2048)) > 0) {
    7. qDebug() << "Read" << bytesRead;
    8. }
    9. qDebug() << "Final bytesRead value" << bytesRead;
    10. in.close();
    11. }
    To copy to clipboard, switch view to plain text mode 
    This will fail to open if there is no disc in the drive. In a real program you would use a larger buffer, distinguish between bytesRead == 0 and -1 etc.

  7. #7
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    Thanks for the hint, but it doesn't work for me! The md5 sum from image and dvd are not the same! On command line they are identical! Or is the code wrong?

    Qt Code:
    1. QCryptographicHash hash(QCryptographicHash::Md5);
    2. QFile in("/dev/cdrom");
    3.  
    4. if (in.open(QIODevice::ReadOnly)) {
    5. char buf[2048];
    6.  
    7. while (in.read(buf, 2048) > 0) {
    8. hash.addData(buf, 2048);
    9. }
    10.  
    11. in.close();
    12. qDebug() << hash.result().toHex();
    13. }
    14. else {
    15. qDebug() << "Failed to open device!";
    16. }
    To copy to clipboard, switch view to plain text mode 

  8. #8
    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: Calculate MD5 sum of a big file

    How many bytes did you manage to read from the device?
    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.


  9. #9
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    ...the while loop reads all data from dvd!?

  10. #10
    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: Calculate MD5 sum of a big file

    Quote Originally Posted by realdarkman71 View Post
    ...the while loop reads all data from dvd!?
    Was this supposed to be an answer to my question? If so, then it's not what I expected. I expected that you will change your code to count how many bytes were really read, not that you expect the loop to read "all" the data.
    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.


  11. #11
    Join Date
    Oct 2010
    Location
    Earth
    Posts
    50
    Thanks
    3
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    Ok, but how? ...sorry!

Similar Threads

  1. Replies: 7
    Last Post: 15th February 2012, 13:51
  2. Qtcreator won't calculate correctly
    By thefatladysingsopera in forum Newbie
    Replies: 3
    Last Post: 3rd June 2011, 23:43
  3. calculate draw time
    By johnsoga in forum Qt Programming
    Replies: 1
    Last Post: 30th May 2009, 02:03
  4. how to calculate difference b/w two times
    By dummystories in forum Newbie
    Replies: 1
    Last Post: 9th March 2009, 13:58
  5. Replies: 0
    Last Post: 6th March 2009, 08:19

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.