DEV Community

Cover image for Getting Started with eslint-plugin-crypto
Ofri Peretz
Ofri Peretz

Posted on

Getting Started with eslint-plugin-crypto

24 cryptography rules. Weak algorithms. Secure random. Key management.

Quick Install

npm install --save-dev eslint-plugin-crypto
Enter fullscreen mode Exit fullscreen mode

Flat Config

// eslint.config.js
import crypto from 'eslint-plugin-crypto';

export default [crypto.configs.recommended];
Enter fullscreen mode Exit fullscreen mode

Run ESLint

npx eslint .
Enter fullscreen mode Exit fullscreen mode

You'll see output like:

src/hash.ts
  15:27 error  πŸ”’ CWE-328 CVSS:7.5 | Weak hash algorithm: MD5
               Fix: Use crypto.createHash('sha256')

src/token.ts
  28:22 error  πŸ”’ CWE-338 | Math.random() used for security
               Fix: Use crypto.randomBytes(32).toString('hex')
Enter fullscreen mode Exit fullscreen mode

Rule Overview

Category Rules Examples
Hash Algorithms 3 MD5, SHA1, weak hash
Random Generation 3 Math.random(), predictable salt
Symmetric Encryption 6 ECB mode, static IV, weak cipher
Key Management 4 Hardcoded keys, weak key derivation
Timing Attacks 1 Unsafe string comparison
Key Length 2 Short keys, insufficient entropy

Quick Wins

Before

// ❌ Weak hash
crypto.createHash('md5').update(data);

// ❌ Predictable random
const token = Math.random().toString(36);

// ❌ ECB mode
crypto.createCipheriv('aes-256-ecb', key, null);
Enter fullscreen mode Exit fullscreen mode

After

// βœ… Strong hash
crypto.createHash('sha256').update(data);

// βœ… Secure random
const token = crypto.randomBytes(32).toString('hex');

// βœ… GCM mode
crypto.createCipheriv('aes-256-gcm', key, iv);
Enter fullscreen mode Exit fullscreen mode

Available Presets

// Security-focused configuration
crypto.configs.recommended;

// All rules enabled
crypto.configs.all;
Enter fullscreen mode Exit fullscreen mode

Quick Reference

# Install
npm install --save-dev eslint-plugin-crypto

# Config (eslint.config.js)
import crypto from 'eslint-plugin-crypto';
export default [crypto.configs.recommended];

# Run
npx eslint .
Enter fullscreen mode Exit fullscreen mode

πŸ“¦ npm: eslint-plugin-crypto
πŸ“– Full Rule List

⭐ Star on GitHub


πŸš€ grep -r "md5" in your codebase!

GitHub | LinkedIn | Dev.to

Top comments (0)