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

File Integrity Verifier

Build a file integrity verification system that computes cryptographic checksums for files and verifies them later.

Requirements

Create a module that provides two main functions:

  1. computeChecksum(filePath): Reads a file in chunks and computes its SHA-256 hash

    • Read the file in 8KB chunks to efficiently handle large files
    • Return the hex-encoded hash string
    • Handle file reading errors appropriately
  2. verifyChecksum(filePath, expectedHash): Verifies if a file matches an expected hash

    • Compute the current file hash
    • Compare with the provided expected hash
    • Return true if they match, false otherwise

The implementation should handle files of any size efficiently by processing them in chunks rather than loading them entirely into memory.

Dependencies { .dependencies }

@stablelib/sha256 { .dependency }

Provides SHA-256 cryptographic hashing functionality.

@stablelib/hex { .dependency }

Provides hexadecimal encoding/decoding utilities.

Test Cases

  • Computing checksum of a small file (< 1KB) returns correct SHA-256 hash in hex format @test
  • Computing checksum of a large file (> 100KB) processed in chunks returns correct hash @test
  • Verifying a file against correct checksum returns true @test
  • Verifying a file against incorrect checksum returns false @test

Implementation

@generates

API

/**
 * Computes the SHA-256 checksum of a file by reading it in chunks.
 *
 * @param filePath - Path to the file to hash
 * @returns Promise that resolves to hex-encoded SHA-256 hash
 * @throws Error if file cannot be read
 */
export function computeChecksum(filePath: string): Promise<string>;

/**
 * Verifies if a file matches an expected SHA-256 checksum.
 *
 * @param filePath - Path to the file to verify
 * @param expectedHash - Expected hex-encoded SHA-256 hash
 * @returns Promise that resolves to true if hash matches, false otherwise
 * @throws Error if file cannot be read
 */
export function verifyChecksum(filePath: string, expectedHash: string): Promise<boolean>;

Install with Tessl CLI

npx tessl i tessl/npm-stablelib--sha256

tile.json