React Native Quick Crypto
API Reference

Scrypt

Memory-hard password-based key derivation

Scrypt is a password-based key derivation function (KDF) that is designed to be memory-hard. It prevents custom hardware attacks (like ASICs) by requiring large amounts of memory to solve.

Table of Contents

Theory

Standard hashes are computationally cheap. Specialized hardware (ASICs, GPUs) can calculate billions of SHA-256 hashes per second, making them effective at brute-forcing passwords.

Scrypt resists this by requiring large amounts of Memory (RAM) to compute.

  • It generates a large random dataset in memory.
  • It reads excessively from random locations in that dataset.
  • This defeats hardware acceleration because memory is expensive and difficult to parallelize on a massive scale.

Parameters:

  1. N (Cost): CPU/Memory cost.
  2. r (Block Size): Memory block size.
  3. p (Parallelization): Independent threads.
Memory128×r×N bytes\text{Memory} \approx 128 \times r \times N \text{ bytes} CPU Cost4×N×r×p\text{CPU Cost} \approx 4 \times N \times r \times p

Module Methods

scrypt(password, salt, keylen[, options], callback)

Asynchronously derives a key.

Parameters:

Prop

Type

Returns: void

Example:

import { scrypt, randomBytes } from 'react-native-quick-crypto';

const pass = 'correct horse battery staple';
const salt = randomBytes(16);

const opts = { N: 32768, r: 8, p: 1, maxmem: 64 * 1024 * 1024 };

scrypt(pass, salt, 64, opts, (err, key) => {
  if (err) throw err;
  console.log('Derived Key:', key.toString('hex'));
});

scryptSync(password, salt, keylen[, options])

Synchronous version. Warning: This will block the entire JS thread. Use with caution in React Native.

Returns: Buffer


Real-World Examples

Wallet Encryption

Encouraging high-security parameters (~32MB RAM) for encrypting crypto wallets.

import { scrypt, randomBytes, createCipheriv } from 'react-native-quick-crypto';

function encryptWallet(privateKey: Buffer, password: string): Promise<Object> {
    return new Promise((resolve, reject) => {
        const salt = randomBytes(32);
        const iv = randomBytes(16);
        
        // Derive encryption key
        scrypt(password, salt, 32, { N: 32768, r: 8, p: 1 }, (err, derivedKey) => {
            if (err) return reject(err);
            
            const cipherKey = derivedKey.slice(0, 32); 
            const cipher = createCipheriv('aes-256-ctr', cipherKey, iv);
            let ciphertext = cipher.update(privateKey);
            ciphertext = Buffer.concat([ciphertext, cipher.final()]);
            
            resolve({
                kdf: 'scrypt',
                ciphertext: ciphertext.toString('hex')
            });
        });
    });
}

On this page