UNPKG

merkle-tree-lib

Version:

Merkle Tree implementation with BIP340 tagged hash support.

74 lines (73 loc) 2.16 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HashStrategyFactory = exports.Keccak256Strategy = exports.SHA512Strategy = exports.SHA256Strategy = void 0; const crypto_1 = __importDefault(require("crypto")); /** * SHA-256 hash strategy implementation */ class SHA256Strategy { constructor() { this.name = 'sha256'; } hash(data) { const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data); return crypto_1.default.createHash('sha256').update(buffer).digest(); } getAlgorithmName() { return this.name; } } exports.SHA256Strategy = SHA256Strategy; /** * SHA-512 hash strategy implementation */ class SHA512Strategy { constructor() { this.name = 'sha512'; } hash(data) { const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data); return crypto_1.default.createHash('sha512').update(buffer).digest(); } getAlgorithmName() { return this.name; } } exports.SHA512Strategy = SHA512Strategy; /** * Keccak-256 hash strategy implementation (used in Ethereum) */ class Keccak256Strategy { constructor() { this.name = 'keccak256'; } hash(data) { const buffer = Buffer.isBuffer(data) ? data : Buffer.from(data); return crypto_1.default.createHash('sha3-256').update(buffer).digest(); } getAlgorithmName() { return this.name; } } exports.Keccak256Strategy = Keccak256Strategy; /** * Factory to create hash strategy instances */ class HashStrategyFactory { static createStrategy(algorithm) { switch (algorithm) { case 'sha256': return new SHA256Strategy(); case 'sha512': return new SHA512Strategy(); case 'keccak256': return new Keccak256Strategy(); default: throw new Error(`Unsupported hash algorithm: ${algorithm}`); } } } exports.HashStrategyFactory = HashStrategyFactory;