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

    Ok, I used this:

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

    After completed, overallBytesRead is "8738865152", but the file size of image is "8738846720". He reads more bytes as on dvd is! I'm confused!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Calculate MD5 sum of a big file

    Line 11 should be :
    Qt Code:
    1. hash.addData(buf, bytesRead);
    To copy to clipboard, switch view to plain text mode 

  3. #3
    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

    This is not a failing of Qt, but a failing of understanding the data you are handling. The DVD/CD will almost always contain extra padding data at the end of the supplied image to meet requirements of the specifications. You should read (up to) as many bytes from the DVD as are in the image you are trying to compare with. So, for example, a Gentoo image:
    Qt Code:
    1. // Original image file
    2. chrisw@newton ~ $ dd if=install-amd64-minimal-20110609.iso bs=2048 | md5sum
    3. 64900+0 records in // <<< this is the number of blocks in the image
    4. 64900+0 records out
    5. 132915200 bytes (133 MB) copied, 4.39815 s, 30.2 MB/s
    6. 3acf53667fcf1d03e98068ee4af5f4a3 -
    7.  
    8. // Reading all data from the raw device... fails
    9. chrisw@newton ~ $ dd if=/dev/cdrom bs=2048 | md5sum
    10. 64963+0 records in
    11. 64963+0 records out
    12. b0700288a316b71dee09ed87dce3b160 - // <<<< Not good
    13. 133044224 bytes (133 MB) copied, 39.3402 s, 3.4 MB/s
    14.  
    15. // reading correct number of blocks from the device matches
    16. chrisw@newton ~ $ dd if=/dev/cdrom bs=2048 count=64900 | md5sum
    17. 64900+0 records in
    18. 64900+0 records out
    19. 3acf53667fcf1d03e98068ee4af5f4a3 - // <<< Sweet :)
    20. 132915200 bytes (133 MB) copied, 37.9759 s, 3.5 MB/s
    To copy to clipboard, switch view to plain text mode 

  4. #4
    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

    Okay, I understand! But how can I do this in my code? This works for me:

    Qt Code:
    1. QCryptographicHash hash(QCryptographicHash::Md5);
    2. QFile in("/dev/cdrom");
    3. QFileInfo fileInfo("/home/chris/backup.img");
    4. qint64 imageSize = fileInfo.size();
    5.  
    6. if (in.open(QIODevice::ReadOnly)) {
    7. char buf[2048];
    8. int bytesRead;
    9. qint64 overallBytesRead = 0;
    10.  
    11. while ((bytesRead = in.read(buf, 2048)) > 0) {
    12. overallBytesRead += bytesRead;
    13. hash.addData(buf, 2048);
    14.  
    15. if (overallBytesRead == imageSize) {
    16. break;
    17. }
    18. }
    19.  
    20. in.close();
    21. qDebug() << "overall bytes read:" << overallBytesRead;
    22. qDebug() << hash.result().toHex();
    23. }
    24. else {
    25. qDebug() << "Failed to open device!";
    26. }
    To copy to clipboard, switch view to plain text mode 

    Is that right?

  5. #5
    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

    This works because the image will be an exact number of 2048 byte blocks. If you change that buffer size then you might need to handle reading a last partial block in the read at line 11 and line 13. For example if you used at 10000 byte buffer with my Gentoo image of 132,915,200 bytes the last block will be 5200 bytes and you do not want to read more than that or your hash will be affected.

    I'd be inclined to do it this way:
    Qt Code:
    1. QCryptographicHash hash(QCryptographicHash::Md5);
    2. QFile in("/dev/cdrom");
    3. QFileInfo fileInfo("/home/chris/backup.img");
    4. qint64 imageSize = fileInfo.size();
    5.  
    6. const int bufferSize = 10000;
    7. if (in.open(QIODevice::ReadOnly)) {
    8. char buf[bufferSize];
    9. int bytesRead;
    10.  
    11. int readSize = qMin(imageSize, bufferSize);
    12. while (readSize > 0 && (bytesRead = in.read(buf, readSize)) > 0) {
    13. imageSize -= bytesRead;
    14. hash.addData(buf, bytesRead);
    15. readSize = qMin(imageSize, bufferSize);
    16. }
    17.  
    18. in.close();
    19. qDebug() << hash.result().toHex();
    20. }
    21. else {
    22. qDebug() << "Failed to open device!";
    23. }
    To copy to clipboard, switch view to plain text mode 

    There is always more than one way to do it.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,360
    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

    The smartest thing would probably be to detect the end of the image data instead of relying on having the image size given upfront. Padding probably begins with some fixed pattern or there is some ioctl call that can return the real data size. Otherwise it wouldn't be possible to create the image in the first place.
    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.


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

    In the case of my Gentoo CDROM the extra 63 blocks on the CD are all zero. Unfortunately, so are at least the last 100 blocks of the original image. The end of my VirtualBox additions CD image and a data DVD is similar. I don't claim to know the complete in-and-outs of the various standards but it does seem that zero padding is done at several stages in the mastering and writing of an image (-pad option in mkisofs for example).

    If the point of the exercise is to compare an image to the version on a disc then knowing the image size up front is hardly unreasonable.

  8. #8
    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

    Okay, I know the image size at this point. I read "image size" / 2048 blocks from DVD in a "for" loop. That works, check sums are identical!

    Thank you very much to all for your help!!!

  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

    Sorry, but another problem: With Windows I cannot open the dvd drive!

    Qt Code:
    1. QFile dvd("D:");
    2.  
    3. if (dvd.open(QIODevice::ReadOnly)) {
    4. (...)
    5. }
    To copy to clipboard, switch view to plain text mode 

    dvd.open returns false, as admin too! Any ideas?

  10. #10
    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

    Devices are not files on Windows. The entire conversation to date has been about Linux/UNIX systems so nobody has pointed this out. You may need to use Windows API calls to do this, e.g. http://www.codeproject.com/Articles/...ding-Audio-CDs

    From that article it may be worth trying QFile dvd("\\\\.\\D:") as the file name first... but I wouldn't hold much hope.

  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

    QFile dvd("\\\\.\\D:") doesn't work ... pity!

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.