Legacy:HashFromFile

From Spherical
Jump to: navigation, search

Generates an MD5 hash of a file, like a fingerprint that only changes if the file changes.

Usage

string HashFromFile(filename);


  • filename String. A path relative to the current game's other/ directory (".." to backtrack), specifying the file to generate the MD5 hash of.
  • string is returned, containing the 128-bit MD5 fingerprint in the form of hexadecimal digits (32 chars long).


Examples

function game()
{
  // Defaults to directory 'game_name/other/'
  var file1 = "my_file_a.txt";
  var file2 = "my_file_b.txt";
  
  if (HashFromFile(file1) == HashFromFile(file2))
    Abort("Heh, " + file1 + " and " + file2 + " are identical!\n");
  else
    Abort(file1 + " and " + file2 + " are different.\n");
}

The above example will compare two files, not by comparing their contents against each other, but by computing and comparing their hashes. The game will then state whether the files are equivalent or not.


Notes

  • The return value of this function is a 32-character (128-bit) hexadecimal representation of the MD5 'fingerprint' of the data. The Sphere implementation follows the RSA reference implementation of RFC 1321, which means that it produces the same output MD5 as Unix md5 utilities.
  • The same sequence of bytes in a ByteArray will always produce the same MD5 hash sequence. This can be used to verify if a file has been corrupted or tampered with, or to check, say, if files online are different to the version on the local machine, indicating that an update is in order.
  • The MD5 algorithm is a one-way hashing algorithm: this means that it is easy to make from source data, but difficult (but not impossible) to find the source data given the MD5 sum.
  • To find the MD5 of just a byte array, use the HashByteArray() function.
  • To generate MD5 hashes of files outside the others/ directory, use one of the following prefixes, followed by the desired path:
    • '~/' or '../' to access the game's base directory.
    • '/common/' to access files from '/Sphere/common/', a directory that can be shared by all games.


See also