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?

More than 1 year has passed since last update.

sha256.js【JavaScript】

Last updated at Posted at 2023-04-11

async function digestMessage(message) {
  const msgUint8 = new TextEncoder().encode(message);                           // (utf-8 の) Uint8Array にエンコードする
  const hashBuffer = await crypto.subtle.digest('SHA-256', msgUint8);           // メッセージをハッシュする
  const hashArray = Array.from(new Uint8Array(hashBuffer));                     // バッファーをバイト列に変換する
  const hashHex = hashArray.map(b => b.toString(16).padStart(2, '0')).join(''); // バイト列を16進文字列に変換する
  return hashHex;
}

const text = 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.';
const digestHex = await digestMessage(text);
console.log(digestHex);

// echo -n 'An obscure body in the S-K System, your majesty. The inhabitants refer to it as the planet Earth.' | sha256sum

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?