LinuxQuestions.org
Register a domain and help support LQ
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 12-08-2012, 09:09 AM   #1
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 291

Rep: Reputation: 17
Computing hash on a video file


I am supposed to write a code to compute the hash 'h' of a given video file F, the hash function is SHA256. Any advice on how I can do this with Pycrypto.
 
Old 12-08-2012, 06:22 PM   #2
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
The pycrypto library is reasonably documented, so that would be the place to start. The SHA256 page gives an example of how it works.

It isn't clear from your question where you are getting stuck, so you may need to be more specific.
 
Old 12-10-2012, 01:00 PM   #3
Nabeel
Member
 
Registered: Nov 2009
Location: Pakistan
Distribution: Ubuntu
Posts: 291

Original Poster
Rep: Reputation: 17
Well For one I need to know how to open the video file as a hex string in python. I tried the
Code:
open('path_to_video','r').read()
but the idle went being busy and it still is at the moment of this typing. second how do I proceed to make a program that calculates the hash as described below
Quote:
Suppose a web site hosts large video file F that anyone can download. Browsers who download the file need to make sure the file is authentic before displaying the content to the user, So Instead of computing a hash of the entire file, the web site breaks the file into 1KB blocks (1024 bytes). It computes the hash of the last block and appends the value to the second to last block. It then computes the hash of this augmented second to last block and appends the resulting hash to the third block from the end. This process continues from the last block to the first as in the following diagram
When appending the hash value to each block, please append it as binary data, that is, as 32 unencoded bytes (which is 256 bits). If the file size is not a multiple of 1KB then the very last block will be shorter than 1KB, but all other blocks will be exactly 1KB.

Last edited by Nabeel; 12-10-2012 at 01:01 PM.
 
Old 12-10-2012, 07:14 PM   #4
neonsignal
Senior Member
 
Registered: Jan 2005
Location: Melbourne, Australia
Distribution: Debian Wheezy (Fluxbox WM)
Posts: 1,368
Blog Entries: 52

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
Well, you are on the right track.

You normally wouldn't slurp in a large video file in a single read. One reason is that it may be bigger than your available memory, which will slow things down a lot.

The question talks about processing the file in 1024 byte blocks. The read command can take a parameter which is the number of bytes, so it would make sense to only read in 1024 bytes at a time.

Another hint is that it would be wise to open the file in binary mode ("rb" instead of "r"), so that no changes are made to any characters.

Don't think of reading it as a 'hex string'; hexadecimal is just a human representation of the data. Unless a question explicitly talks about converting to hex (which it doesn't), the data is just a string of bytes - it doesn't matter if the bytes represent characters or numbers or video or whatever.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dynamically parse BibTeX and create hash of hash wakatana Programming 11 12-13-2012 05:59 PM
Perl Hashes -- Updating a hash ref via hash value 0.o Programming 5 06-05-2012 01:45 PM
Perl Hash of Hash reference query kdelover Programming 1 02-19-2011 05:47 AM
Hash File Implementation jwstric2 Programming 3 04-06-2005 12:58 PM


All times are GMT -5. The time now is 08:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration