Skip to content

infinity.sha256

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

Module: infinity.crypto

Example:

infinity.loadModule('infinity.crypto');

let hash = infinity.sha256.hash('dataToHash');
console.debug(hash);
//0e396429f7fb2760a15e7d2326ec64b3d7db2c52b673a77c15a8250d66a94c93

hash = infinity.sha256.hash('dataToHash', infinity.crypto.outputFormat.base64);
console.debug(hash);
//DjlkKff7J2ChXn0jJuxks9fbLFK2c6d8FaglDWapTJM=

Functions

hash()

Returns the SHA-256 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.sha256.hash(dataToHash);

hashFile()

Returns the SHA-256 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.sha256.hashFile('dataToHash.txt');

hashStream()

Returns the SHA-256 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.sha256.hashStream(stream);

hmac()

Returns a keyed hash value (HMAC-SHA256) 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.sha256.hmac('message', 'secretKey');
console.debug(hmac);
//c45aee961de82908cbc65e0fe663dd8414c878503a1ed44fab7cbd4759484b29