Skip to content

infinity.sha512

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

Module: infinity.crypto

Example:

infinity.loadModule('infinity.crypto');

let hash = infinity.sha512.hash('dataToHash');
console.debug(hash);
//1c4f93685598ea8a75f247dc9f630b57ca1e690d5d1a1ad8184ffdeb39ba17850b945af4e810f62d2a3ed452519efd863f8d4515503c7682cc2f874d53943e6b

hash = infinity.sha512.hash('dataToHash', infinity.crypto.outputFormat.base64);
console.debug(hash);
//HE+TaFWY6op18kfcn2MLV8oeaQ1dGhrYGE/96zm6F4ULlFr06BD2LSo+1FJRnv2GP41FFVA8doLML4dNU5Q+aw==

Functions

hash()

Returns the SHA-512 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 hash = infinity.sha512.hash(dataToHash);

hashFile()

Returns the SHA-512 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 hash = infinity.sha512.hashFile('dataToHash.txt');

hashStream()

Returns the SHA-512 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 hash = infinity.sha512.hashStream(stream);

hmac()

Returns a keyed hash value (HMAC-SHA512) 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.sha512.hmac('message', 'secretKey');
console.debug(hmac);
//3749d173a2f0eeea92b13b1dc79f58b20028a889fc503ffc6bf27e204b7503b9138710d0285677378e29fcf5dd65c15a78f7669d900b74f9c3d2a1d8742e2a14