Difference between revisions of "Legacy:HashFromFile"
From Spherical
(Created page with "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 ...") |
(API convention) |
||
(One intermediate revision by the same user not shown) | |||
Line 1: | Line 1: | ||
Generates an MD5 hash of a file, like a fingerprint that only changes if the file changes. | Generates an MD5 hash of a file, like a fingerprint that only changes if the file changes. | ||
− | =Usage= | + | ==Usage== |
− | + | {{Usage|returns=string|func=HashFromFile|params=filename}} | |
* '''filename''' String. A path relative to the current game's other/ directory (".." to backtrack), specifying the file to generate the MD5 hash of. | * '''filename''' String. A path relative to the current game's other/ directory (".." to backtrack), specifying the file to generate the MD5 hash of. | ||
Line 9: | Line 9: | ||
− | =Examples= | + | ==Examples== |
<syntaxhighlight> | <syntaxhighlight> | ||
Line 29: | Line 29: | ||
− | =Notes= | + | ==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 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 | + | * The same sequence of bytes in a [[API:ByteArray|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. | * 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 find the MD5 of just a byte array, use the [[API:HashByteArray|HashByteArray]]() function. |
* To generate MD5 hashes of files outside the others/ directory, use one of the following prefixes, followed by the desired path: | * To generate MD5 hashes of files outside the others/ directory, use one of the following prefixes, followed by the desired path: | ||
Line 46: | Line 46: | ||
See also | See also | ||
− | * [[HashByteArray]]() | + | * Sphere [[API:ByteArray|ByteArray]] object |
− | * [[Abort]]() | + | * [[API:HashByteArray|HashByteArray]]() |
+ | * [[API:Abort|Abort]]() | ||
* [http://www.ietf.org/rfc/rfc1321.txt[RFC 1321]] MD5 algorithm | * [http://www.ietf.org/rfc/rfc1321.txt[RFC 1321]] MD5 algorithm |
Latest revision as of 18:58, 21 May 2013
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
- Sphere ByteArray object
- HashByteArray()
- Abort()
- [RFC 1321] MD5 algorithm