0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

パスワードハッシュ化で安全に! Bcryptで暗号化しよう

Posted at

Bcryptって何者?

Bcryptは、1999年に開発されたパスワードハッシュ関数です。つまり、パスワードを安全に保存するためのツールです。
Bcryptは、「暗号学的ソルト」と「コストファクター」という2つの秘密兵器を持っています。

1. 暗号学的ソルト

ソルトって聞くと、お料理の塩を思い浮かべるかもしれません。でも、ここでいうソルトは、パスワードに加える「スパイス」のようなものです。同じパスワードでも、ソルトを加えることで全く違うハッシュ値になります。

import * as bcrypt from 'bcrypt';

const password = 'myPassword123';
const salt1 = bcrypt.genSaltSync(10);
const salt2 = bcrypt.genSaltSync(10);

const hash1 = bcrypt.hashSync(password, salt1);
const hash2 = bcrypt.hashSync(password, salt2);

console.log(hash1); // $2b$10$X8eP1IeIdF0FfYd5.YW6H.wzT1tZKV0rTedXvNIaSgHJ3ImB2Zmou
console.log(hash2); // $2b$10$7kMjfuK9Ij.UmQOsjxhG6OI7kcSYDlVoqeLx2Y5Eo9tNvSO2Pnr2W

この例では、同じパスワードに対して異なるソルトを使用することで異なるハッシュ値が生成されています!

2. コストファクター

コストファクターは、簡単に言うとハッシュ化の難易度を調整する機能です。 ハッシュ化の計算回数を決定し、値が高いほど、ハッシュ化に時間がかかります。

import * as bcrypt from 'bcrypt';

const password = 'myPassword123';

console.time('Cost factor 10');
bcrypt.hashSync(password, 10);
console.timeEnd('Cost factor 10');

console.time('Cost factor 12');
bcrypt.hashSync(password, 12);
console.timeEnd('Cost factor 12');

// 出力例:
// Cost factor 10: 65.723ms
// Cost factor 12: 254.556ms

この例では、コストファクターを10から12に上げることで、ハッシュ化にかかる時間が約4倍になっています。

Bcryptの特徴

  1. 遅い!でも、それがいい!

    • Bcryptは意図的に計算に時間がかかるように設計されています。一般ユーザーにはほとんど影響がありませんが、総当たり攻撃を試みる攻撃者にとっては大きな障害になります。
  2. 未来への適応力

    • コストファクターのおかげで、将来のハードウェアの進化にも対応できます。
  3. ソルトの自動生成

    • 開発者が忘れてしまっても、Bcryptが自動でソルトを生成します。親切設計ですね!

Bcryptの使い方

様々なプログラミング言語でBcryptを使用できます。ここでは、TypeScriptでの例を見てみましょう。

import * as bcrypt from "bcryptjs";

// パスワードのハッシュ化
export const hashPassword = async (password: string): Promise<string> => {
  const saltRounds = 10; // ソルトのラウンド数を指定
  return bcrypt.hash(password, saltRounds);
};

// パスワードの検証
export const comparePassword = async (
  password: string,
  hash: string
): Promise<boolean> => {
  return bcrypt.compare(password, hash);
};

// 使用例
const password = 'mySecurePassword123';
const hashedPassword = hashPassword(password);

console.log('Hashed password:', hashedPassword);
console.log('Verification (correct password):', comparePassword(password, hashedPassword));
console.log('Verification (incorrect password):', comparePassword('wrongPassword', hashedPassword));

// 出力例:
// Hashed password: $2b$10$X8eP1IeIdF0FfYd5.YW6H.wfT1tZKV0rTedXvNIcYgHJ3ImB2Zmou
// Verification (correct password): true
// Verification (incorrect password): false

まとめ

Bcryptは、パスワードのセキュリティを強化する強力なツールです。ソルトとコストファクターという2つの秘密兵器で、攻撃者を悩ませます。でも使い方は簡単!あなたのアプリケーションにBcryptを導入して、ユーザーのパスワードを守りましょう。

(注意:Bcryptを使っても、「password123」のような弱いパスワードは相変わらず危険です。ユーザーには強力なパスワードの使用を促しましょう!)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?