Skip to content

xstd-js/sha256

Repository files navigation

npm (scoped) npm NPM npm type definitions coverage

Shows a black logo in light color mode and a white one in dark color mode.

@xstd/sha256

SHA-256 implementation

📦 Installation

yarn add @xstd/sha256
# or
npm install @xstd/sha256 --save

📜 Documentation

Class

declare class SHA256 {
  constructor(): SHA256;
  
  /**
   * Indicates whether the hash was finalized
   */
  readonly finished: boolean;
  
  /**
   * Cleans internal buffers and re-initializes hash state.
   */
  clean(): void;
  
  /**
   * Updates hash state with the given data.
   *
   * @param {Uint8Array} data The data to hash.
   * @param {number} [dataLength] The number of bytes to use from `data`.
   * @throws {Error} When trying to update already finalized hash.
   * @return {this} To chain calls
   */
  update(data: Uint8Array, dataLength?: number): this;
  
  /**
   * Finalizes the hash computation and puts hash into `output`.
   * If hash was already finalized, puts the same value.
   *
   * @param {Sha256Output} output The array to put hash in.
   * @return {this} To chain calls
   */
  finish(output: Sha256Output): this;
  
  /**
   * Returns the final hash digest.
   *
   * @return {Uint8Array} The final hash digest.
   */
  digest(): Uint8Array;
  digest<GOutput extends Sha256Output>(output: GOutput): GOutput;
}

Example:

const hashed = new SHA256().update(new TextEncoder().encode("Hello, world!")).digest();

/*
hashed = Unew Uint8Array([
  7, 242, 189, 239, 52, 237, 22, 227, 161, 186, 13, 187, 126, 71, 184, 253, 152, 28, 224, 204,
  179, 225, 191, 229, 100, 216, 44, 66, 60, 186, 126, 71,
])
 */

Function

/**
 * Compute SHA-256 hash of the given data.
 *
 * @param {Uint8Array} data The data to hash.
 * @return {Uint8Array} The hash digest.
 */
declare function sha256(data: Uint8Array): Uint8Array;
declare function sha256<GOutput extends Sha256Output>(data: Uint8Array, output: GOutput): GOutput;

Example:

const hashed = sha256(new TextEncoder().encode("Hello, world!"));

About

SHA-256 implementation

Resources

License

Contributing

Stars

Watchers

Forks

Packages

No packages published