7
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

数字6桁パスワードの総当たり JavaScript(node.js)編

Posted at

JavaScriptで学ぶ関数型プログラミングを読書中に、某所で盛り上がってた6桁の数値のmd5値を作成するコードを、練習がてらに書いたよ。

forとかeach書いたら負けだと思って書いた。
性能は残念ながらでてない。

var _ = require('underscore');
_.mixin(require('underscore.string').exports());
var crypto = require('crypto');

// log output function
var log = (function(msg){
  var base = new Date().getTime(); // start time
  return function(msg){
    var now = new Date().getTime();
    console.log([msg, ':', now - base, 'msec'].join(''));
    base = now;
  };
})();

// master data
var salt = 'hoge$';
var hash = '4b364677946ccf79f841114e73ccaf4f';
log("inited");

// create input data '000000'-'999999'
var passes = _.map(_.range(0,1000000), function(n){return _.pad(n,6,'0');});
log("created data");

// passes -> hashes(md5)
var hashes = _.map(
        passes, function(src){
          var md5 = crypto.createHash('md5');
          md5.update(salt + src, 'utf8');
          return md5.digest('hex');
        });
log("hashed alldata");

// key:hash value:pass
var dict = _.object(hashes,passes);
log("dicted, pass is " + dict[hash]);

実行結果

$ node md5.js
inited:0msec
created data:707msec
hashed alldata:3835msec
dicted, pass is 567890:679msec

まぁ性能出すための戦いじゃないけどおそい。
入力データの作成だけで700msec、
MD5の計算で4000msec、
それをオブジェクトへ詰めるだけで700msec…
関数型に慣れるための習作だしもうこれでいいよね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?