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-6/

Fixed Buffer Hash Processor

Build a hash processor that efficiently computes SHA-256 hashes from data stored in fixed-size buffers, processing only the valid portion of each buffer.

Background

In high-performance systems, data often arrives in fixed-size buffers where only a portion contains valid data. You need to implement a utility that processes these buffers and computes their SHA-256 hash, handling only the valid bytes in each buffer.

Requirements

Implement a BufferHashProcessor class with the following behavior:

  1. Initialize with a maximum buffer size
  2. Accept data chunks of varying actual lengths (up to the max size)
  3. Process only the valid portion of each buffer (not the entire fixed-size buffer)
  4. Compute and return the final SHA-256 hash as a hexadecimal string

The processor should handle multiple data chunks efficiently, processing only the specified number of valid bytes from each buffer.

API

/**
 * Processes data from fixed-size buffers and computes SHA-256 hash.
 */
export class BufferHashProcessor {
  /**
   * Creates a new processor with specified maximum buffer size.
   * @param maxBufferSize - Maximum size of buffers that will be processed
   */
  constructor(maxBufferSize: number);

  /**
   * Adds a data chunk to be hashed.
   * @param buffer - The buffer containing data (may be larger than actual data)
   * @param actualLength - The number of valid bytes in the buffer to process
   */
  addChunk(buffer: Uint8Array, actualLength: number): void;

  /**
   * Finalizes processing and returns the SHA-256 hash.
   * @returns Hexadecimal string representation of the hash
   */
  finalize(): string;

  /**
   * Resets the processor to process a new sequence of buffers.
   */
  reset(): void;
}

Test Cases

Basic single buffer processing

  • Given a 100-byte buffer with 5 valid bytes [0x68, 0x65, 0x6c, 0x6c, 0x6f] (ASCII "hello"), the hash should be "2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824" @test

Multiple buffer chunks

  • Given three buffers: buffer1 (50 bytes, 3 valid: [0x66, 0x6f, 0x6f]), buffer2 (50 bytes, 3 valid: [0x62, 0x61, 0x72]), buffer3 (50 bytes, 4 valid: [0x62, 0x61, 0x7a, 0x7a]), the hash should be "97df3588b5a3f24babc3851b372f0ba71a9dcdded43b14b9d06961bfc1707d9d" @test

Reset and reuse

  • After processing a buffer and getting a hash, calling reset() and processing new data should produce the correct hash for the new data, independent of the previous computation @test

Empty buffer handling

  • Given a 100-byte buffer with 0 valid bytes, the hash should be "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" (SHA-256 of empty input) @test

Implementation

@generates

Dependencies { .dependencies }

@stablelib/sha256 { .dependency }

Provides SHA-256 cryptographic hash function implementation.

@satisfied-by

Install with Tessl CLI

npx tessl i tessl/npm-stablelib--sha256

tile.json