CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-stablelib--sha256

SHA-256 cryptographic hash function implementation for TypeScript and JavaScript

91

0.98x
Overview
Eval results
Files

task.mdevals/scenario-10/

SHA-256 Hash Buffer Manager

A utility for computing SHA-256 hashes with custom buffer management to minimize memory allocations in performance-critical applications.

Requirements

Implement a hash buffer manager that:

  1. Maintains a pre-allocated buffer pool for hash outputs
  2. Computes SHA-256 hashes and writes results directly to pre-allocated buffers
  3. Tracks which buffers are in use and which are available
  4. Supports computing multiple hashes without creating new buffer allocations for each hash operation

The manager should allow users to compute hashes efficiently by reusing buffers from the pool. When a buffer is no longer needed, it should be returned to the pool for reuse.

Implementation

@generates

API

/**
 * Manages a pool of pre-allocated buffers for SHA-256 hash outputs
 */
export class HashBufferManager {
  /**
   * Creates a new hash buffer manager with a specified pool size
   * @param poolSize - Number of buffers to pre-allocate in the pool
   */
  constructor(poolSize: number);

  /**
   * Computes a SHA-256 hash and writes it to a buffer from the pool
   * @param data - Input data to hash
   * @returns An object containing the hash buffer and a release function
   * @throws Error if no buffers are available in the pool
   */
  computeHash(data: Uint8Array): { buffer: Uint8Array; release: () => void };

  /**
   * Returns statistics about buffer pool usage
   * @returns Object with total and available buffer counts
   */
  getPoolStats(): { total: number; available: number };
}

Test Cases

  • Given a manager with pool size 3, computing a hash should return a 32-byte buffer containing the hash and a release function @test
  • Given a manager with pool size 2, after computing 2 hashes without releasing buffers, attempting to compute a third hash should throw an error @test
  • Given a manager with pool size 2, after computing a hash and releasing it, the available buffer count should increase back to the original size @test
  • Given the same input data, hashes computed by different calls to computeHash should produce identical hash values @test

Dependencies { .dependencies }

@stablelib/sha256 { .dependency }

Provides SHA-256 cryptographic hash functionality.

Install with Tessl CLI

npx tessl i tessl/npm-stablelib--sha256

tile.json