{"_id":"@codewithshinde/pwdhasher","_rev":"2-f27d15cdacef9d628a7dbf0ff8e37eb0","name":"@codewithshinde/pwdhasher","dist-tags":{"latest":"1.0.0"},"versions":{"1.0.0":{"name":"@codewithshinde/pwdhasher","version":"1.0.0","author":{"name":"codewithshinde"},"license":"ISC","_id":"@codewithshinde/pwdhasher@1.0.0","maintainers":[{"name":"brewcode","email":"codewithshinde@gmail.com"}],"homepage":"https://github.com/codewithshinde/pwd-hasher#readme","bugs":{"url":"https://github.com/codewithshinde/pwd-hasher/issues"},"dist":{"shasum":"fde7d5affe8f4c4cd6dcce731b359ac0d7620444","tarball":"https://registry.npmjs.org/@codewithshinde/pwdhasher/-/pwdhasher-1.0.0.tgz","fileCount":5,"integrity":"sha512-ANQ3AtUZtPkjX9MYSYteIttrU1uHoIJEKo3yhafokWoDxsMHhuJ8qUqdj01KpsFZDTtni82yDpn2ZAV6yTlo/w==","signatures":[{"sig":"MEQCIFDz059F7Umi/TTB2nFhvK9/mysTNsGf0eKaCFbWdRkTAiA5JaBfhAk6/AhDdB8yTFr2DdGzgjkBiPEb9syIU6Wg5Q==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":16519},"main":"./dist/index.js","types":"./dist/index.d.ts","module":"./dist/index.mjs","gitHead":"1273b0ef53b2d1444853afe190479af16c29f1b4","scripts":{"build":"tsup src/index.ts --format cjs,esm --dts"},"_npmUser":{"name":"brewcode","email":"codewithshinde@gmail.com"},"repository":{"url":"git+https://github.com/codewithshinde/pwd-hasher.git","type":"git"},"_npmVersion":"10.5.2","description":"pwd-hasher is a simple, zero-dependency Node.js utility for hashing, verifying, and generating strong passwords using the built-in crypto module. It uses the modern and secure scrypt key derivation function.","directories":{},"_nodeVersion":"20.13.0","dependencies":{"util":"^0.12.5","tslib":"^2.3.0"},"_hasShrinkwrap":false,"devDependencies":{"tsup":"^6.1.2","typescript":"^5.9.2","@types/node":"^24.3.0"},"_npmOperationalInternal":{"tmp":"tmp/pwdhasher_1.0.0_1755466823814_0.19836197875343786","host":"s3://npm-registry-packages-npm-production"}}},"time":{"created":"2025-08-17T21:40:23.731Z","modified":"2026-02-03T20:20:20.579Z","1.0.0":"2025-08-17T21:40:23.986Z"},"bugs":{"url":"https://github.com/codewithshinde/pwd-hasher/issues"},"author":{"name":"codewithshinde"},"license":"ISC","homepage":"https://github.com/codewithshinde/pwd-hasher#readme","repository":{"url":"git+https://github.com/codewithshinde/pwd-hasher.git","type":"git"},"description":"pwd-hasher is a simple, zero-dependency Node.js utility for hashing, verifying, and generating strong passwords using the built-in crypto module. It uses the modern and secure scrypt key derivation function.","maintainers":[{"email":"codewithshinde@gmail.com","name":"karthikshindee"}],"readme":"# PWD Hasher\n\n`pwd-hasher` is a simple, zero-dependency Node.js utility for hashing, verifying, and generating strong passwords using the built-in `crypto` module. It uses the modern and secure `scrypt` key derivation function.\n\n[](https://www.google.com/search?q=https://www.npmjs.com/package/pwd-hasher)\n[](https://www.google.com/search?q=https://www.npmjs.com/package/pwd-hasher)\n[](https://opensource.org/licenses/ISC)\n\n-----\n\n## \\#\\# Installation\n\nYou can install the package using npm or yarn:\n\n```bash\nnpm install pwd-hasher\n```\n\nor\n\n```bash\nyarn add pwd-hasher\n```\n\n-----\n\n## \\#\\# Core Concept: Why Hashing is Essential 🔒\n\nYou should **never store passwords in plain text**. If your database is ever compromised, attackers would gain access to every user's account information.\n\nPassword hashing is a **one-way process** that turns a password into a fixed-length string of characters, called a hash. This process is not reversible, meaning you cannot \"un-hash\" a password to get back to the original text. The only way to verify a password is to hash it again using the same \"salt\" (a random string) and see if the resulting hashes match.\n\nThis package handles the entire process for you.\n\n-----\n\n## \\#\\# Real-World Usage: Authentication Workflow\n\nHere’s how you would use `pwd-hasher` in a typical web application for user sign-up, login, and password updates.\n\n### \\#\\#\\# 1. User Registration (Sign-Up)\n\nWhen a new user creates an account, you must hash their password before saving it to your database.\n\n```typescript\nimport { getHashedPassword } from 'pwd-hasher';\n\nasync function handleUserSignUp(email, password) {\n  try {\n    // 1. Hash the user's chosen password.\n    const hashedPassword = await getHashedPassword(password);\n    \n    // Example hash: '16#a4e8b3f2c1d0.c8e7a6b4d2f0a1b9c3e8d7f6a5b4c3d2'\n\n    // 2. Save the user's email and the *hashedPassword* to your database.\n    // DO NOT save the original password.\n    await db.collection('users').insertOne({\n      email: email,\n      passwordHash: hashedPassword // Store the hash, not the password\n    });\n\n    console.log('User registered successfully!');\n  } catch (error) {\n    console.error('Error during registration:', error);\n  }\n}\n\n// Simulate a new user signing up\nhandleUserSignUp('test@example.com', 'mySecurePassword123');\n```\n\n### \\#\\#\\# 2. User Login\n\nWhen a user tries to log in, you retrieve their stored hash from the database and use `verifyHash` to securely compare it with the password they just entered.\n\n```typescript\nimport { verifyHash } from 'pwd-hasher';\n\nasync function handleUserLogin(email, passwordFromLoginForm) {\n  try {\n    // 1. Find the user in the database by their email.\n    const user = await db.collection('users').findOne({ email: email });\n\n    if (!user) {\n      console.log('Login failed: User not found.');\n      return;\n    }\n\n    // 2. Compare the password from the login form with the stored hash.\n    const isPasswordCorrect = await verifyHash(\n      passwordFromLoginForm,\n      user.passwordHash\n    );\n\n    if (isPasswordCorrect) {\n      console.log('✅ Password is correct. Logging in...');\n      // Proceed with creating a session, JWT, etc.\n    } else {\n      console.log('❌ Invalid credentials.');\n    }\n  } catch (error) {\n    console.error('An error occurred during login:', error);\n  }\n}\n\n// Simulate a login attempt\nhandleUserLogin('test@example.com', 'mySecurePassword123');\n```\n\n### \\#\\#\\# 3. Updating or Resetting a Password\n\nThe process for updating a password is the same as registration: you hash the **new** password and replace the old hash in the database.\n\n```typescript\nimport { getHashedPassword } from 'pwd-hasher';\n\nasync function updateUserPassword(email, newPassword) {\n  try {\n    // 1. Hash the new password.\n    const newHashedPassword = await getHashedPassword(newPassword);\n\n    // 2. Find the user and update their passwordHash in the database.\n    await db.collection('users').updateOne(\n      { email: email },\n      { $set: { passwordHash: newHashedPassword } }\n    );\n\n    console.log('Password updated successfully!');\n  } catch (error) {\n    console.error('Error updating password:', error);\n  }\n}\n\n// Simulate a user changing their password\nupdateUserPassword('test@example.com', 'myNewStrongerPassword456');\n```\n\n-----\n\n## \\#\\# API Reference\n\n### \\#\\#\\# `getHashedPassword(password, [salt], [len])`\n\nHashes a password with a salt using `scrypt`.\n\n  * **`password: string`**: The plaintext password to hash.\n  * **`salt?: string`** (optional): A salt to use. If not provided, a random one is generated.\n  * **`len?: number`** (optional): The length of the derived key. Defaults to `16`.\n  * **Returns**: `Promise<string>` - The final hash in the format `len#salt.hash`.\n\n### \\#\\#\\# `verifyHash(password, hashPassword)`\n\nVerifies a plaintext password against a hash generated by this package.\n\n  * **`password: string`**: The plaintext password to check.\n  * **`hashPassword: string`**: The stored hash string (e.g., from your database).\n  * **Returns**: `Promise<boolean>` - `true` if the password matches, `false` otherwise.\n\n### \\#\\#\\# `generateStrongPassword([length])`\n\nGenerates a cryptographically-random password.\n\n  * **`length?: number`** (optional): The desired password length. Must be at least 8. Defaults to `12`.\n  * **Returns**: `string` - The generated strong password.\n\n-----\n\n## \\#\\# License\n\nThis project is licensed under the **ISC License**.","readmeFilename":"README.md"}