@technobuddha > library > Binary > Hash
Class: Sha256
Defined in: sha256.ts:109
Secure Hash Algorithm, SHA2 SHA-256
Example
const sha256 = new Sha256();
sha256.update('hello world', 'utf8');
sha256.digest('hex');
// 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'const sha256 = new Sha256();
sha256.update(new Uint8Array([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64]));
sha256.digest('hex');
// 'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9'Extends
Constructors
Constructor
new Sha256(): Sha256;Defined in: src/sha256.ts:127
Creates a new SHA-256 hash instance and initializes its internal state.
Returns
Sha256
Remarks
The internal state variables are set to the initial SHA-256 constants as specified in FIPS PUB 180-4. Use update to process data and digest to retrieve the hash.
Overrides
Methods
digest()
Call Signature
digest(): Uint8Array;Defined in: src/sha-base.ts:77
Finalizes the hash computation and returns the resulting hash digest. This method performs any necessary padding and processes the final block of data according to the hash algorithm's specification.
Returns
The hash digest
Inherited from
Call Signature
digest(encoding: BinaryEncoding): string;Defined in: src/sha-base.ts:78
Finalizes the hash computation and returns the resulting hash digest. This method performs any necessary padding and processes the final block of data according to the hash algorithm's specification.
Parameters
| Parameter | Type | Description |
|---|---|---|
encoding | BinaryEncoding | Optional. The encoding to use for the output digest (e.g., 'hex', 'base64'). |
Returns
string
An encoded string, depending on the encoding parameter.
Inherited from
update()
Call Signature
update(data: BinaryObject | ArrayLike<number>): this;Defined in: src/sha-base.ts:119
Updates the hash with the given binary data.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | BinaryObject | ArrayLike<number> | The data to update the hash with, as a BinaryObject. |
Returns
this
The hash instance for method chaining.
Inherited from
Call Signature
update(data: string, encoding?: TextEncoding): this;Defined in: src/sha-base.ts:120
Updates the hash with the given string data.
Parameters
| Parameter | Type | Description |
|---|---|---|
data | string | The string data to update the hash with. |
encoding? | TextEncoding | Optional text encoding of the input string (e.g., 'utf8'). |
Returns
this
The hash instance for method chaining.