UNPKG

fritzbox-api

Version:

Straightforward, lightweight and extendable Node.js library to communicate with FRITZ!Box devices

54 lines (47 loc) 1.86 kB
// Copyright (c) 2026, Thorsten A. Weintz. All rights reserved. // Licensed under the MIT license. See LICENSE in the project root for license information. import { createHash, pbkdf2Sync, randomBytes } from 'crypto'; /** * Utility class providing hashing and key derivation functions. * Contains only static methods and is not intended to be instantiated. */ class HashUtil { /** * Creates an MD5 hash from the given input. * * @param {string|Buffer} data Input data to hash. * @returns {string} Hash value as a hexadecimal string. */ static md5 = (data) => createHash('md5').update(data).digest('hex'); /** * Derives a key using the PBKDF2 (Password-Based Key Derivation Function 2) algorithm. * * @param {string|Buffer} value Input value (e.g., password or intermediate hash). * @param {string} saltHex Salt as a hexadecimal string. * @param {number|string} iterations Number of iterations (work factor). * @param {number} [keyLength=32] Length of the derived key in bytes. * @param {string} [digest='sha256'] Hash algorithm to use (e.g., 'sha256', 'sha512'). * @returns {Buffer} Derived key as a Buffer. */ static pbkdf2 = (value, saltHex, iterations, keyLength = 32, digest = 'sha256') => pbkdf2Sync( value, Buffer.from(saltHex, 'hex'), Number(iterations), keyLength, digest ); /** * Generates a cryptographically secure random hex string. * * @param {number} size Number of random bytes. * @returns {string} Random value as hex string. */ static randomHex = (size = 8) => randomBytes(size).toString('hex'); } /** * Exports @see HashUtil as default class. */ export default HashUtil;