2
3

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.

JavaScriptでハッシュ

Last updated at Posted at 2018-10-06

jsSHA
hashing

const jsSHA = require('jssha');

let shaObj = new jsSHA('SHA-256', 'TEXT');
shaObj.update('input');
let hash = shaObj.getHash('HEX');
console.log(hash);
hash = shaObj.getHash('B64');
console.log(hash);

shaObj = new jsSHA('SHA-512', 'TEXT');
shaObj.update('input');
hash = shaObj.getHash('HEX');
console.log(hash);
hash = shaObj.getHash('B64');
console.log(hash);
c96c6d5be8d08a12e7b5cdc1b207fa6b2430974c86803d8891675e76fd992c20
yWxtW+jQihLntc3Bsgf6ayQwl0yGgD2IkWdedv2ZLCA=
dc6d6c30f2be9c976d6318c9a534d85e9a1c3f3608321a04b4678ef408124d45d7164f3e562e68c6c0b6c077340a785824017032fddfa924f4cf400e6cbb6adc
3G1sMPK+nJdtYxjJpTTYXpocPzYIMhoEtGeO9AgSTUXXFk8+Vi5oxsC2wHc0CnhYJAFwMv3fqST0z0AObLtq3A==

ハッシュアルゴリズムを変更したい場合は
アルゴリズムを再設定するメソッドが無いみたいなので
毎回newしないといけない様子。

パスワード用

node-password-hash

verify機能もある

const passwordHash = require('password-hash');

let counter = 0;
while (counter < 5) {
  var hashedPassword = passwordHash.generate(
    'password123',
    {
      algorithm: "sha256"
    }
  );
  console.log(hashedPassword);
  counter++;
}
sha256$9febcf65$1$0f1ef2081fd2a5f38a035e130a5ab8933d727131173b4c43b39a345cb1016793
sha256$0b37ea0a$1$f33d50b64282c4db99a7caf66eae46bef6a45fb63814895f4feb03b9a86fca07
sha256$e6329c37$1$f0e2a4eb756bb830a1805ab650af893d833503e03e04302b7a370df5a5267b6a
sha256$db1ec9b8$1$655ac7c9af1b6d7aff2f79faaae823952d9519b6009ff06477a799ec5738d845
sha256$df5cbf6a$1$53b6c0177c3357c848a7d273e244d302ecd0ec65559bf06c197be946d71f91d8
2
3
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
2
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?