@lindorm/sha
v0.6.1
Published
Thin SHA-1/256/384/512 hashing helper that wraps Node's `crypto.createHash` in a typed class.
Readme
@lindorm/sha
Thin SHA-1/256/384/512 hashing helper that wraps Node's crypto.createHash in a typed class.
Installation
npm install @lindorm/shaThis package is ESM-only. Use import syntax; require is not supported.
Features
ShaKitclass with configurable algorithm and encoding- Instance helpers to
hash,verify, andassertdigests against input data - Static one-shot helpers
S1,S256,S384,S512that producebase64urldigests and accept bothstringandBufferinput ShaErrorthrown byasserton mismatch
Usage
import { ShaKit } from "@lindorm/sha";
const sha = new ShaKit({ algorithm: "SHA512", encoding: "hex" });
const digest = sha.hash("hello world");
sha.verify("hello world", digest); // true
sha.assert("hello world", digest); // void; throws ShaError on mismatchDefaults are algorithm: "SHA256" and encoding: "base64" when no options are passed:
import { ShaKit } from "@lindorm/sha";
const sha = new ShaKit();
const digest = sha.hash("hello world");The static helpers always emit base64url and accept either a string or a Buffer:
import { ShaKit } from "@lindorm/sha";
const a = ShaKit.S256("data");
const b = ShaKit.S256(Buffer.from("data", "utf8"));
// a === b
ShaKit.S1exists for legacy interop only (e.g. the X.509x5tthumbprint per RFC 7515 §4.1.7). SHA-1 is cryptographically broken; do not use it for authentication, integrity, or any security-sensitive purpose.
Catching mismatches:
import { ShaKit, ShaError } from "@lindorm/sha";
const sha = new ShaKit();
const digest = sha.hash("expected");
try {
sha.assert("actual", digest);
} catch (err) {
if (err instanceof ShaError) {
// hash did not match
}
}API
class ShaKit
new ShaKit(options?: ShaKitOptions);| Option | Type | Default | Description |
| ----------- | -------------------------------------------- | ---------- | ------------------------------------------ |
| algorithm | "SHA1" \| "SHA256" \| "SHA384" \| "SHA512" | "SHA256" | Digest algorithm used by instance methods. |
| encoding | BinaryToTextEncoding (Node crypto) | "base64" | Output encoding used by instance methods. |
Instance methods
hash(data: string): string— returns the digest ofdatausing the configured algorithm and encoding.verify(data: string, hash: string): boolean— returnstrueif hashingdataproduceshash.assert(data: string, hash: string): void— throwsShaErrorwith message"Hash does not match"ifverifywould returnfalse.
Static methods
All static methods accept string | Buffer and return a base64url digest. The configured instance algorithm/encoding does not apply.
ShaKit.S1(data: string | Buffer): stringShaKit.S256(data: string | Buffer): stringShaKit.S384(data: string | Buffer): stringShaKit.S512(data: string | Buffer): string
class ShaError
Extends LindormError from @lindorm/errors. Thrown by ShaKit.prototype.assert when the supplied hash does not match the input.
Types
ShaKitOptions—{ algorithm?: ShaAlgorithm; encoding?: BinaryToTextEncoding }CreateShaHashOptions—{ algorithm?: ShaAlgorithm; data: string | Buffer; encoding?: BinaryToTextEncoding }VerifyShaHashOptions—{ algorithm?: ShaAlgorithm; data: string | Buffer; encoding?: BinaryToTextEncoding; hash: string }
ShaAlgorithm is re-exported transitively from @lindorm/types and resolves to "SHA1" | "SHA256" | "SHA384" | "SHA512". BinaryToTextEncoding is the type from Node's built-in crypto module.
License
AGPL-3.0-or-later
