2
1

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.

DenoでSHA-256ハッシュ

Last updated at Posted at 2023-05-15

毎回調べてる気がするので。

Denoで文字列のSHA256ハッシュ値を求める方法について。

DenoでSHA-256ハッシュ

以下の関数sha256を使うと、文字列のSHA-256ハッシュを得ることができる。

import { toHashString } from "https://deno.land/std@0.188.0/crypto/to_hash_string.ts";

const encoder = new TextEncoder();
async function sha256(input: string): Promise<string> {
  const data = encoder.encode(input);
  const digest = await crypto.subtle.digest("SHA-256", data);
  return toHashString(digest);
}

まずencoder.encodeで文字列をUnit8Arrayに変換する。次にcrypto.subtle.digestでSHA256ハッシュを得る。最後に、toHashStringで16進数文字列表現を得る。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?