HashByteArray

From Spheriki

Jump to: navigation, search

Generates an MD5 'fingerprint' of the given data in the ByteArray.


Contents

Usage

string HashByteArray(byte_array);
  • byte_array A Sphere ByteArray object, holding the data to find the MD5 of.


Examples

const MSG = "Marshmallow pie?";
var msg_byte_array = CreateByteArrayFromString(MSG);
Abort("MD5 of \"" + MSG + "\" = " + HashByteArray(msg_byte_array));

The above example will generate the MD5 'fingerprint' of the string "Marshmallow pie?", and display it. Note that the MD5 produced will always be the same, as long as the string does not change.


var password = get_user_password();
var passhash = HashByteArray(CreateByteArrayFromString(password));
send_pass_hash(passhash);

This demonstrates how a client in a client-server application can send a password without worrying about it being intercepted en route to the server. The password entered by the user is used to generate an MD5 hash. Meanwhile, the server gets the MD5 hash made when the user registered, and compares it with the MD5 received from the client. If they match, it means the password the client gave and the one used during registration match, and so they can login.

This way, a simple login system can be developed, while not compromising the security of the user's password.


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 the ByteArray will always produce the same MD5 hash sequence. This can be used to verify the integrity of data, or, as demonstrated in the above examples, check a password without having the send the password itself over insecure channels.
  • 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 an entire file, use the HashFromFile() function.


See also

Personal tools