@validator-sdk/pubkey
v1.0.8
Published
A lightweight Node.js module for validating hexadecimal strings and values.
Readme
@hex-validator/v2
A lightweight Node.js module for validating hexadecimal strings and values.
Description
This module provides utilities to validate if input values contain only valid hexadecimal characters (0-9 and a-f/A-F) and returns validation results. It's designed for validating hex strings, particularly useful for hash validation and cryptographic applications.
Features
- ✅ Validate hexadecimal strings
- ✅ Support for both string and Buffer inputs
- ✅ TypeScript support with full type definitions
- ✅ hash validation
- ✅ Lightweight and fast
- ✅ MIT Licensed
Installation
npm install @hex-validator/v2Usage
Basic Validation
import { validateHex } from '@hex-validator/v2';
// Validate a hex string
const isValid = validateHex('a1b2c3d4e5f6789');
console.log(isValid); // true or false
// Validate with Buffer input
const buffer = Buffer.from('hello world');
const isValidBuffer = validateHex(buffer);
console.log(isValidBuffer); // true or falseTypeScript Support
import { validateHex } from '@hex-validator/v2';
function checkHash(hash: string): boolean {
return validateHex(hash, 'utf8');
}
// Example usage
const hash = 'a1b2c3d4e5f6789012345678901234567890abcdef1234567890abcdef123456';
const isValid = checkHash(hash);API Reference
validateHex(input: string | Buffer, encoding?: BufferEncoding): boolean
Validates if the input contains only valid hexadecimal characters (0-9, a-f, A-F).
Parameters:
input(string | Buffer): The input to validateencoding(BufferEncoding, optional): Buffer encoding for string inputs (default: 'utf8')
Returns:
boolean:trueif input is valid hex,falseotherwise
Examples:
// Valid hex strings
validateHex('a1b2c3d4'); // true
validateHex('1234567890abcdef'); // true
validateHex('ABCDEF123456'); // true
// Invalid hex strings
validateHex('hello world'); // false
validateHex('g1234567'); // false (contains 'g')
validateHex('12345@678'); // false (contains '@')
// Buffer input
const buffer = Buffer.from('a1b2c3d4', 'hex');
validateHex(buffer); // trueRequirements
- Node.js >= 16.0.0
- TypeScript (for type definitions)
Development
Build
npm run buildThis will compile TypeScript files to the dist/ directory.
Dependencies
bn.js: Big number library for handling large integerscrypto: Node.js built-in crypto module
License
MIT © Dern Mateo
Repository
Keywords
validation, utility, node, hex, hash, typescript
