Bcrypt

From G1Joshi/Agent-Skills · View interactive version

Bcrypt is an AI agent skill from the GitHub repository G1Joshi/Agent-Skills. Expert bcrypt hashing assistance covering secure password hashing, salt rounds, and timing attack prevention. Use when hashing passwords, verifying credentials, or managing authentication state.

How to use this skill

Skill file

Bcrypt

Bcrypt is a password-hashing function designed to be slow, protecting against brute-force attacks. It incorporates a salt to protect against rainbow table attacks.

When to Use

Quick Start

import bcrypt from "bcrypt";

const saltRounds = 10;
const myPlaintextPassword = "s0m3password";

// Hashing
const hash = await bcrypt.hash(myPlaintextPassword, saltRounds);
// Store 'hash' in DB: $2b$10$EpIxT98h....

// Verifying
const match = await bcrypt.compare("s0m3password", hash);
if (match) {
  // Login successful
}

Core Concepts

The Modular Crypt Format ($2b$ Cost $ Salt + Hash)

Bcrypt outputs a standardized 60-character string encoding the algorithm revision, work factor, 128-bit salt, and 184-bit hash:

$2b$12$e8Y5W5t2KqfO.oXWf1NfTeu4GZ7hJ2xM.wKzV8L8T7p6Q9r8S1t2u
 ── ── ────────────────────── ──────────────────────────────
 │  │             │                         │
 │  │             │                         └── 31-character checksum/hash
 │  │             └──────────────────────────── 22-character base64 salt
 │  └────────────────────────────────────────── Work Factor (2^12 = 4,096 iterations)
 └───────────────────────────────────────────── Algorithm Identifier (2b)

Salting & Adaptive Work Factor

Salting prevents rainbow table attacks; the cost factor dictates exponential computation time:

import bcrypt from "bcrypt";

// Cost factor of 12 takes ~200-300ms on modern server CPUs
const SALT_ROUNDS = 12;

export async function hashPassword(plainText: string): Promise<string> {
  const salt = await bcrypt.genSalt(SALT_ROUNDS);
  return await bcrypt.hash(plainText, salt);
}

Constant-Time Password Verification

Prevents side-channel timing attacks by performing comparisons in constant time:

export async function verifyCredentials(
  plainText: string,
  hash: string,
): Promise<boolean> {
  // Constant-time execution ensures identical response latency regardless of match index
  return await bcrypt.compare(plainText, hash);
}

Common Patterns

Password Hashing and Constant-Time Verification

Problem: Storing plaintext or unsalted hashes allows rainbow table and timing-attack exploitation.

Solution:

Hash with optimal cost factor (12-14 rounds) and verify asynchronously:

import bcrypt from "bcrypt";

const SALT_ROUNDS = 12;

export async function hashPassword(plainPassword) {
  return await bcrypt.hash(plainPassword, SALT_ROUNDS);
}

export async function verifyPassword(plainPassword, hashedPassword) {
  return await bcrypt.compare(plainPassword, hashedPassword);
}

Best Practices

Do:

Don't:

Troubleshooting

| Error | Cause | Solution |

| :------------------------------------- | :-------------------------------------------------------------- | :---------------------------------------------------------------------------------- |

| Illegal arguments: undefined, string | Plaintext password or hash passed as null/undefined to compare. | Validate that both arguments are non-empty strings before calling bcrypt.compare. |

| High CPU usage / slow responses | Salt rounds set too high (>15) blocking event loop. | Benchmark work factor; recommended value is 12 (approx ~250ms). |

| data and hash arguments required | Parameter ordering reversed in compare function. | Use bcrypt.compare(candidatePassword, storedHash). |

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.