pra-decode
v2.0.0
Published
A lightweight JavaScript library for secure SHA-256 hashing and decoding.
Downloads
7
Maintainers
Readme
pra-decode
pra-decode is a comprehensive JavaScript hashing utility featuring SHA-256 implementation with multiple encoding options and comparison functionality.
Features
- 🔒 Secure Hashing: SHA-256 algorithm implementation
- 🔄 Multiple Encodings: Support for hex, base64, and binary formats
- ↔️ Comparison Utility: Easily verify hashes against plaintext
- ⚡ Lightweight: Zero external dependencies
- 🛠 Utility Functions: Encoding conversion helpers
Installation
npm install pra-decode
# or
yarn add pra-decodeBasic Usage
const praDecode = require('pra-decode');
// Hash a string (defaults to hex output)
const hash = praDecode.toHash('password123');
console.log('Hashed password:', hash);
// Verify a password
const isValid = praDecode.toCompare('password123', hash);
console.log('Password valid:', isValid); // trueAPI Reference
Core Methods
toHash(input, [options])
Generates SHA-256 hash of input.
Parameters:
input(string|Buffer): Input to hashoptions(object): Optional settingsinputEncoding(string): 'utf8' (default), 'hex', or 'base64'outputEncoding(string): 'hex' (default), 'base64', or 'binary'
Returns:
string (hex/base64) or Uint8Array (binary)
Examples:
// Hex output (default)
praDecode.toHash('hello');
// Base64 output
praDecode.toHash('hello', { outputEncoding: 'base64' });
// Binary output
praDecode.toHash('hello', { outputEncoding: 'binary' });
// Hex input
praDecode.toHash('68656c6c6f', { inputEncoding: 'hex' });toCompare(input, hash, [options])
Compares input with a hash.
Parameters:
input(string|Buffer): Input to comparehash(string): Hash to compare againstoptions(object): Optional settingsinputEncoding(string): 'utf8' (default), 'hex', or 'base64'hashEncoding(string): 'hex' (default), 'base64', or 'binary'
Returns:
boolean - true if input matches hash
Example:
const valid = praDecode.toCompare('password123', storedHash, {
hashEncoding: 'base64'
});sha256(input, [options])
Alias for toHash() with identical parameters and return values.
Utility Methods
Access encoding utilities through praDecode.utils:
const { utils } = praDecode;
// Convert between formats
const bytes = new Uint8Array([72, 101, 108, 108, 111]);
const hex = utils.bytesToHex(bytes); // '48656c6c6f'
const base64 = utils.bytesToBase64(bytes); // 'SGVsbG8='
// Convert back
const newBytes = utils.hexToBytes('48656c6c6f');Available utility functions:
bytesToHex(bytes)hexToBytes(hex)bytesToBase64(bytes)base64ToBytes(base64)
Advanced Usage
File Hashing Example
const fs = require('fs');
const praDecode = require('pra-decode');
const fileBuffer = fs.readFileSync('example.txt');
const fileHash = praDecode.toHash(fileBuffer, {
inputEncoding: 'binary',
outputEncoding: 'hex'
});
console.log('File hash:', fileHash);Performance
The implementation is optimized for performance:
- Processes data in 512-bit chunks
- Uses bitwise operations for efficient computation
- Minimal memory overhead
Security Notes
- SHA-256 is cryptographically secure for most applications
- For password storage, consider using PBKDF2 or bcrypt with salt
- Always compare hashes using constant-time comparison in security contexts
Contributing
Contributions are welcome! Please:
- Fork the repository
- Create a feature branch
- Commit your changes
- Push to the branch
- Open a Pull Request
License
MIT
Contact
For support or questions:
- 📧 Email: [email protected]
- 🔗 GitHub: PrashantSharma0512
