25
21

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.

node.jsでmd5計算するとき、hashオブジェクトは使い捨てる

Last updated at Posted at 2014-05-12

md5sum計算したくなった。
ググると、1番目2番目に動くコードある。
2番目の方、ちゃんと使える。
ただ、1番目の方を参考に↓のコード書いたら、エラー出た。

var md5hash = require('crypto').createHash('md5');
var md5hex = function(src){
  md5hash.update(src, 'binary');
  return md5hash.digest('hex');
};

// =>  TypeError: HashUpdate fail

ググったら、hashオブジェクト再利用するなっていわれてた
以下引用。

@Shahor: you cannot reuse hash objects. Your example works when you change this snippet:

 var userHash = hasher.update(socket.id).digest('base64');

to this:

 var userHash = require('crypto').createHash('sha1').update(socket.id).digest('base64');

つまり最初のコードはこれでおk。(2番めの方のコードと同じ)

var crypto = require('crypto');
var md5hex = function(src){
  var md5hash = crypto.createHash('md5');
  md5hash.update(src, 'binary');
  return md5hash.digest('hex');
};
25
21
1

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?