Is a hash function a Random Oracle(RO) function or will there be a chance that the hash function will alter the file (as part of function or by accident) that it is hashing?

  • 6
    Simple trick to find out: Hash it twice. – Arminius Feb 18 '17 at 15:44
  • 1
    Unless you do something like md5sum file.txt | tee file.txt it will not modify the file. In fact, you could have tried to remove the write permissions on the file chmod -w file.txt and see that md5sum file.txt does not raise an error... if md5sum tried to modify the file that command would fail. – Bakuriu Feb 18 '17 at 17:30
  • What you're asking is similar to, "Will watching TV make me gain weight?" Well, it might, but it's also totally possible to watch TV and not gain weight, and it's also possible to gain weight without watching TV... is the answer yes or no? – user541686 Feb 19 '17 at 9:19

A hash function works on data, i.e. input and output data. If the data come from a file or network or similar and if they get written to a file or somewhere else has nothing to do with the hash function itself but with a program which uses this hash function for a particular purpose. This purpose can be be to only print the resulting hash value to stdout without modifying the input file. But a hash function can also be used in the process of adding a signature to some section of a file, in which case the file gets modified.

| improve this answer | |
  • MD5 does not add a signature right? – Timothy Wong Feb 18 '17 at 15:46
  • 9
    MD5 is an algorithm that takes in a bunch of bytes and spits out another bunch of bytes. Whether those get saved anywhere is entirely up to the particular program that you're using, if any, which you haven't mentioned. – Matti Virkkunen Feb 18 '17 at 15:53
  • 1
    I should be using the md5sum from the Linux terminal @MattiVirkkunen – Timothy Wong Feb 18 '17 at 16:26
  • Which @Rory McCune pointed out in his answer below, does not modify the original file – Timothy Wong Feb 18 '17 at 16:27
  • 2
    @TimothyWongGlash It sounds like you think the hash would/could get added to the file. For one, shasum et al doesn't do this. For another, it would defeat the whole purpose; you simply can't add a file's hash/signature to the file itself without invalidating the hash/signature. The point is for a given file to always produce the same hash, so if you change the file, the hash would change too. A hash is specifically a separate identifier/checksum for a piece of data – Flambino Feb 19 '17 at 4:37

In the general case, creating a hash of a file would not modify the original file.

For example in linux the md5sum command can be used to create a hash of a file.

user@host:/etc$ md5sum /etc/hosts d060f54470d55f7c02e639ed92bff5d3 /etc/hosts

the hash is printed back to the screen and doesn't modify the original file.

| improve this answer | |

There are two parts to a file: The file contents (including the primary and alternate streams), and the metadata about the file. Hashing a file will not change the file contents, but it may change the file metadata. So if your intent was to get the hash of a file and not have anyone know that you have done that, then the metadata might give it away. As an example on Windows, when you access a file you end up changing the file's last access date.

| improve this answer | |
  • On linux the 'atime' is recorded depending on mount options of the file system – bbaassssiiee Feb 19 '17 at 6:34

You asked if it could alter it on accident... yes, but it would have to be a very big accident (or a very malicious, and very stupid coder).

A program which hashes a file needs to read that file. This means it has to open it. Files must be opened for reading or writing or both. A file opened for reading cannot be written to. A file opened for writing cannot be read. So it's extremely unlikely this sort of mistake would happen, let alone pass even the most basic testing.

OTOH a malicious coder could try to alter your file. This is an extremely unlikely attack as it will be blindingly obvious what's happened, and also blindingly obvious in the code. That code wouldn't last long.

You can protect against this with file permissions, make the file read-only and the program cannot write to it (some operating systems and filesystems may allow the file owner or superuser to write anyway). However if the program is run by the owner of the file, the program can change the file's permissions, alter the file, and change it back. That includes all its metadata. Like I said, this sort of bluntly malicious code extremely easy to spot and extremely unlikely to exist in the wild.

The real answer is to use reputable, reviewed, well-tested crypto software and not something you copied off the Internet.

| improve this answer | |
  • 2
    The real answer is to use reputable, reviewed, well-tested crypto software and not something you copied off the Internet. Well, in the end you still copy it off the Internet, regardless how reputable and well-tested it is. – DepressedDaniel Feb 19 '17 at 5:02
  • @DepressedDaniel I get my software by bonded courier, don't you? – Schwern Feb 19 '17 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.