Skip to content

infinity.md5

Provides functionality for creating MD5 hashes and MD5-based HMACs out of strings, files and streams.

MD5 is cryptographically broken. Use it only for checksums and compatibility, not for security purposes.

Module: infinity.crypto

Example:

infinity.loadModule('infinity.crypto');

let hash = infinity.md5.hash('dataToHash');
console.debug(hash);
//844fb0adcfe50cb7edf8bd26e5575383

hash = infinity.md5.hash('dataToHash', infinity.crypto.outputFormat.base64);
console.debug(hash);
//hE+wrc/lDLft+L0m5VdTgw==

Functions

hash()

Returns the MD5 hash of the given string. The string is encoded as UTF-8 before hashing. An empty string gives the hash of empty data (before 3.11: an empty string).

Signature:

hash( value: string, outputFormat?: infinity.crypto.outputFormat ): string

Parameters:

  • value: string

    The string to be hashed.

  • outputFormat: infinity.crypto.outputFormat, optional

    The format of the result: infinity.crypto.outputFormat.hex (lowercase hexadecimal, default) or infinity.crypto.outputFormat.base64.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let hashedData = infinity.md5.hash(dataToHash);

hashFile()

Returns the MD5 hash of the contents of the given file. An empty file gives the hash of empty data (before 3.11: an empty string). Throws an exception if the file cannot be opened.

Signature:

hashFile( fileName: string, outputFormat?: infinity.crypto.outputFormat ): string

Parameters:

  • fileName: string

    Filename, relative path (location relative to the folder with the used INFINITY.JS executable file) or absolute path to the file which has to be hashed.

  • outputFormat: infinity.crypto.outputFormat, optional

    The format of the result: infinity.crypto.outputFormat.hex (lowercase hexadecimal, default) or infinity.crypto.outputFormat.base64.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let hashedData = infinity.md5.hashFile('dataToHash.txt');

hashStream()

Returns the MD5 hash of the contents of the given stream. The stream is always hashed from the beginning, regardless of its current position; afterwards the position is at the end of the stream. An empty stream gives the hash of empty data (before 3.11: an empty string).

Signature:

hashStream( stream: infinity.stream, outputFormat?: infinity.crypto.outputFormat ): string

Parameters:

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let hashedData = infinity.md5.hashStream(stream);

hmac()

Returns a keyed hash value (HMAC-MD5) of the given string. Message and key are encoded as UTF-8. An empty value or key is used as is (before 3.11: an empty string was returned).

Signature:

hmac( value: string, key: string, outputFormat?: infinity.crypto.outputFormat ): string

Parameters:

  • value: string

    Message to be hashed.

  • key: string

    Shared secret key used for generating the HMAC variant of the message digest.

  • outputFormat: infinity.crypto.outputFormat, optional

    The format of the result: infinity.crypto.outputFormat.hex (lowercase hexadecimal, default) or infinity.crypto.outputFormat.base64.

Return type: string

Example:

infinity.loadModule('infinity.crypto');
let hmac = infinity.md5.hmac('message', 'secretKey');
console.debug(hmac);
//4b70c04437779007bf3ae7c1c9063cc7