Block a user
@taxi/crypto-utils (0.2.1)
Published 2025-09-12 09:30:14 +00:00 by omari.mohamed
Installation
@taxi:registry=npm install @taxi/crypto-utils@0.2.1"@taxi/crypto-utils": "0.2.1"About this package
@taxi/crypto-utils
Shared crypto utilities for Taxi apps.
Features:
- Password hashing with bcryptjs
- TOTP generation/verification with otplib
- AES-GCM symmetric encryption/decryption using Web Crypto API
- AES-256-CTR helpers (Node and CryptoJS) with user-specified IV
Install (local monorepo)
Add to an app package.json dependencies:
"@taxi/crypto-utils": "file:../packages/crypto-utils"
Then install in that app folder:
npm install
Usage
import { hashPassword, comparePassword, generateTotpSecret, generateTotpToken, verifyTotpToken, otpauthURL, encrypt, decrypt, encryptAesCtrNode, decryptAesCtrNode, encryptAesCtrJs, decryptAesCtrJs, generateIvHex, encryptNFCCard, decryptNFCCard } from '@taxi/crypto-utils';
// Passwords
const hash = await hashPassword('secret');
const ok = await comparePassword('secret', hash);
// TOTP
const secret = generateTotpSecret();
const token = generateTotpToken(secret);
const valid = verifyTotpToken(token, secret);
const url = otpauthURL(secret, 'user@example.com', 'Taxi');
// Symmetric
const payload = await encrypt('hello', 'passphrase');
const plain = await decrypt(payload, 'passphrase');
// AES-256-CTR (dynamic IV). Key must be 32 bytes hex (64 hex chars). IV must be 16 bytes.
const keyHex = '0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef';
const ivUtf8 = 'zxfv3cr6stq7f2rx'; // 16 chars => 16 bytes (utf8)
const ivHex = generateIvHex(); // alternative random 16-byte hex IV
// Node crypto implementation (returns hex ciphertext)
const encHexNode = encryptAesCtrNode({ foo: 'bar' }, keyHex, ivUtf8);
const decNode = decryptAesCtrNode(encHexNode, keyHex, ivUtf8);
// CryptoJS implementation (compatible with above)
const encHexJs = encryptAesCtrJs({ foo: 'bar' }, keyHex, ivUtf8);
const decJs = decryptAesCtrJs(encHexJs, keyHex, ivUtf8);
// NFC Card payload (format: 64-hex rand + keyHex + 64-hex rand + cipherHex)
// WARNING: This format embeds the key in clear hex. Use only if this is the explicit protocol requirement.
const nfcPayload = encryptNFCCard({ balance: 1000 }, keyHex, ivUtf8);
const nfcData = decryptNFCCard(nfcPayload, ivUtf8);
Notes:
- AES-GCM helpers rely on the global Web Crypto API (
globalThis.crypto), available in modern Node (>=18). In older environments, consider a polyfill or use Node'scryptomodule directly. - AES-256-CTR helpers require a 32-byte key in hex (64 hex chars) and a 16-byte IV. You can pass the IV in
utf8,hex, or let the helpers auto-detect withivEncoding = 'auto'(default). - NFC helpers format is
64-hex random + keyHex + 64-hex random + cipherHex. This intentionally places the key in clear hex to match the requested protocol; do not use in security-sensitive contexts unless mandated.
Dependencies
Dependencies
| ID | Version |
|---|---|
| bcryptjs | ^3.0.2 |
| crypto-js | ^4.2.0 |
| otplib | ^12.0.1 |
Development Dependencies
| ID | Version |
|---|---|
| @types/bcryptjs | ^2.4.6 |
| @types/crypto-js | ^4.2.2 |
| @types/node | ^20 |
| rimraf | ^6.0.1 |
| tsup | ^8.3.5 |
| typescript | ^5.6.3 |