react-native-argon2-turbo
v0.1.2
Published
High-performance Argon2 hashing for React Native with TurboModules (JSI)
Readme
react-native-argon2-turbo ⚡
High-performance Argon2 hashing for React Native, powered by TurboModules
10x faster than react-native-argon2 • Drop-in replacement • PoW utilities included
Why?
The existing react-native-argon2 uses the old React Native bridge, adding ~10ms overhead per hash. This library uses TurboModules (JSI) for direct native calls, making it 10x faster.
Features
- ⚡ 10x faster - TurboModules, no bridge overhead
- 🔄 Drop-in replacement - Same API as react-native-argon2
- 🔐 All variants - Argon2i, Argon2d, Argon2id
- ⛏️ PoW support - Native proof-of-work loop (500+ h/s)
- 📱 Cross-platform - iOS + Android
- 🏗️ Modern - React Native 0.75+, New Architecture only
Installation
npm install react-native-argon2-turbo
# or
yarn add react-native-argon2-turboiOS
cd ios && pod installAndroid
No additional setup required.
Quick Start
import { hash, verify } from 'react-native-argon2-turbo';
// Hash a password
const { rawHash, encodedHash } = await hash({
password: 'mypassword',
salt: 'randomsalt16bytes',
});
console.log(rawHash); // hex string
console.log(encodedHash); // $argon2id$v=19$...
// Verify a password
const isValid = await verify({
password: 'mypassword',
encodedHash: encodedHash,
});API
hash(options): Promise
Async password hashing (recommended).
const result = await hash({
password: 'mypassword',
salt: 'randomsalt123456',
// Optional parameters (with defaults)
iterations: 2, // time cost
memory: 65536, // memory in KiB (64 MB)
parallelism: 1,
hashLength: 32,
mode: 'argon2id', // 'argon2i' | 'argon2d' | 'argon2id'
});hashSync(options): HashResult
Synchronous hashing. Warning: Blocks the JS thread.
const result = hashSync({
password: 'mypassword',
salt: 'randomsalt123456',
});verify(options): Promise
Verify a password against an encoded hash.
const isValid = await verify({
password: 'mypassword',
encodedHash: '$argon2id$v=19$m=65536,t=2,p=1$...',
});computePow(options): Promise
Compute proof-of-work (entire loop runs natively).
const result = await computePow({
base: '48656c6c6f', // hex-encoded base bytes
salt: 'blockhash...', // hex-encoded salt
difficulty: 12, // required leading zero bits
startNonce: 0, // optional starting point
maxAttempts: 10_000_000, // optional limit
timeoutMs: 60_000, // optional timeout
// Argon2 parameters (optimized for mobile)
iterations: 1,
memory: 4096, // 4 MB
parallelism: 1,
hashLength: 32,
});
console.log(result.nonce); // winning nonce
console.log(result.digest); // hex hash
console.log(result.attempts); // hashes computed
console.log(result.elapsedMs); // time takencancelPow(): void
Cancel an ongoing PoW computation.
cancelPow();getPowProgress(): Promise
Poll for PoW progress (for UI updates).
const progress = await getPowProgress();
console.log(progress.attempts);
console.log(progress.elapsedMs);
console.log(progress.hashesPerSecond);Types
interface HashOptions {
password: string | Uint8Array;
salt: string | Uint8Array;
iterations?: number; // default: 2
memory?: number; // default: 65536 (64MB)
parallelism?: number; // default: 1
hashLength?: number; // default: 32
mode?: 'argon2i' | 'argon2d' | 'argon2id'; // default: 'argon2id'
}
interface HashResult {
rawHash: string; // hex-encoded hash
encodedHash: string; // PHC format: $argon2id$v=19$...
}
interface PowOptions {
base: string; // hex-encoded base bytes
salt: string; // hex-encoded salt
difficulty: number; // leading zero bits required
startNonce?: number; // default: random
maxAttempts?: number; // default: 10_000_000
timeoutMs?: number; // default: 60_000
iterations?: number; // default: 1
memory?: number; // default: 4096
parallelism?: number; // default: 1
hashLength?: number; // default: 32
}
interface PowResult {
nonce: number;
digest: string; // hex
attempts: number;
elapsedMs: number;
}Migration from react-native-argon2
Before
import argon2 from 'react-native-argon2';
const result = await argon2(password, salt, {
iterations: 2,
memory: 65536,
parallelism: 1,
hashLength: 32,
mode: 'argon2id',
});After
import { hash } from 'react-native-argon2-turbo';
const result = await hash({
password,
salt,
iterations: 2,
memory: 65536,
parallelism: 1,
hashLength: 32,
mode: 'argon2id',
});Legacy Compatibility
For zero-change migration:
import argon2 from 'react-native-argon2-turbo/legacy';
// Works exactly like react-native-argon2
const result = await argon2(password, salt, options);Performance
Single Hash
| Library | Time | Improvement | |---------|------|-------------| | react-native-argon2 | ~12ms | - | | react-native-argon2-turbo | ~2ms | 6x faster |
Proof of Work (Native Loop)
| Library | Hashes/sec | Improvement | |---------|------------|-------------| | Bridge (per hash) | ~80 h/s | - | | TurboModule (native loop) | 500-1000+ h/s | 6-12x faster |
Requirements
- React Native 0.75+
- New Architecture enabled
- iOS 13.4+
- Android API 21+
License
MIT
Credits
- Argon2Swift - iOS implementation
- argon2kt - Android implementation
