LoginSignup
1
4

More than 3 years have passed since last update.

【Node.js】zxcvbnを使ってパスワード強度をチェックする

Posted at

「パスワード強度チェックするようなライブラリって何かあるのかな?」
と興味本位で調べてみたらzxcvbnというものが見つかったので、ご紹介。

zxcvbnとは

Dropbox社製のパスワード強度チェッカーです。
Node.js以外にも色々な言語に対応したライブラリが作られています。

dropbox/zxcvbn: Low-Budget Password Strength Estimation

準備

$ npm i zxcvbn

基本的な使い方

とりあえずhogehogeという文字列に対してパスワード強度をチェックしてみましょう。

const zxcvbn = require('zxcvbn');

const result = zxcvbn('hogehoge');
console.log(result);

基本的な使い方はとても簡単ですね。
第2引数に入力を渡して更に細かい設定をすることも可能なようですが、今回は遊んでみたかっただけなので割愛させてください。

出力内容を見てみましょう。

{
  password: 'hogehoge',
  guesses: 20003,
  guesses_log10: 4.301095134950942,
  sequence: [
    {
      pattern: 'repeat',
      i: 0,
      j: 7,
      token: 'hogehoge',
      base_token: 'hoge',
      base_guesses: 10001,
      base_matches: [Array],
      repeat_count: 2,
      guesses: 20002,
      guesses_log10: 4.301073422940843
    }
  ],
  calc_time: 3, # zxcvbnが計算するのにかかった時間(ミリ秒) あんまり気にしなくていい
  crack_times_seconds: { # パスワードが特定されるまでにかかる時間(秒)
    # 4種類の攻撃パターンごとの想定時間
    # 基本的には`offline_fast_hashing_1e10_per_second`の値だけ見ておけばいいかも
    online_throttling_100_per_hour: 720108,
    online_no_throttling_10_per_second: 2000.3,
    offline_slow_hashing_1e4_per_second: 2.0003,
    offline_fast_hashing_1e10_per_second: 0.0000020003
  },
  crack_times_display: { # パスワードが特定されるまでにかかる時間(わかりやすい形式)
    online_throttling_100_per_hour: '8 days',
    online_no_throttling_10_per_second: '33 minutes',
    offline_slow_hashing_1e4_per_second: '2 seconds',
    offline_fast_hashing_1e10_per_second: 'less than a second'
  },
  score: 1, # 0 ~ 4でパスワード強度を評価する
  feedback: {
    warning: 'Repeats like "abcabcabc" are only slightly harder to guess than "abc"',
    suggestions: [
      'Add another word or two. Uncommon words are better.',
      'Avoid repeated words and characters'
    ]
  }
}

crack_times_secondsの4種類のパターンについては、公式ドキュメントには次のように記述してありました。

  • online_throttling_100_per_hour

online attack on a service that ratelimits password auth attempts.

  • online_no_throttling_10_per_second

online attack on a service that doesn't ratelimit,
or where an attacker has outsmarted ratelimiting.

  • offline_slow_hashing_1e4_per_second

offline attack. assumes multiple attackers,
proper user-unique salting, and a slow hash function
w/ moderate work factor, such as bcrypt, scrypt, PBKDF2.

  • offline_fast_hashing_1e10_per_second

offline attack with user-unique salting but a fast hash
function like SHA-1, SHA-256 or MD5. A wide range of
reasonable numbers anywhere from one billion - one trillion
guesses per second, depending on number of cores and machines.
ballparking at 10B/sec.

うーん、セキュリティって難しい!

参考

1
4
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
1
4