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 3 years have passed since last update.

R で SHA256 値を求める(おまけ付き)

Posted at

使う機会少なすぎて、使いおうと思った時に思い出すこともできないのでメモです。

実行環境

  • Ubuntu 16.04
  • R 3.6.3

RでSHA256

RでSHA256
if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("hello!"))
# -> [1] "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"

if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("ハロ!"))
# -> [1] "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"

コマンドラインから実行する場合

コマンドラインから実行する場合
Rscript -e '
if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("hello!"))
'

その結果が本当に正しいかを確かめる

本当にあっているか不安…。この不安は異なる環境で結果が同じかどうかをチェックしないことには払拭できないと思ったので、調べてみたところ多くの場合でワンライナーで書けるくらい簡単だったのでメモです。

以下のなかから実行できそうなのを2つ3つくらい、日本語も含めて実行して同じ結果かをチェックしたら良いかなって。

Linuxコマンド

SHA256のテスト(Linuxコマンド)
# echo+sha256sumを使う
echo -n "hello!" | sha256sum
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b  -

echo -n "ハロ!" | sha256sum
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42  -

# echo+opensslを使う
echo -n "hello!" | openssl sha256
# -> (stdin)= ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b

echo -n "ハロ!" | openssl sha256
# -> (stdin)= 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42

echo コマンドだと -n オプションを忘れちゃうことがあるので printfコマンドの方が良いかも。

printfでSHA256のテスト(Linuxコマンド)
printf "hello!" | sha256sum
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b  -

printf "ハロ!" | sha256sum
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42  -

Node.js13.5

SHA256のテスト(Node.js)
console.log(require("crypto").createHash("sha256").update("hello!").digest("hex"))
// -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b

console.log(require("crypto").createHash("sha256").update("ハロ!").digest("hex"))
// -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42

コマンドラインから実行する場合

コマンドラインから実行する場合
node -e '
console.log(require("crypto").createHash("sha256").update("hello!").digest("hex"))
'

Python3.5

SHA256のテスト(Python)
# 3.5
print(__import__("hashlib").sha256("hello!".encode("utf-8")).hexdigest())
print(__import__("hashlib").sha256("ハロ!".encode("utf-8")).hexdigest())

コマンドラインから実行する場合

コマンドラインから実行する場合
python3 -c '
print(__import__("hashlib").sha256("hello!".encode("utf-8")).hexdigest())
'

Ruby2.3

SHA256のテスト(Ruby)
require "digest/sha2"
print Digest::SHA256.hexdigest("hello!")
# -> "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"

require "digest/sha2"
print Digest::SHA256.hexdigest("ハロ!")
# -> "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"

# または
require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("hello!")
# -> "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"

require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("ハロ!")
# -> "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"

コマンドラインから実行する場合

コマンドラインから実行する場合
ruby -e '
require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("hello!")
'

Perl5

SHA256のテスト(Perl)
use Digest::SHA qw(sha256_hex);
print sha256_hex("hello!");
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b

use Digest::SHA qw(sha256_hex);
print sha256_hex("ハロ!");
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42

コマンドラインから実行する場合

コマンドラインから実行する場合
perl -e '
use Digest::SHA qw(sha256_hex);
print sha256_hex("hello!");
'

JavaScript

SHA256のテスト(JavaScript)
/*
  https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest から引用
*/
async function digestMessage(message) {
  const msgUint8 = new TextEncoder().encode(message);
  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('');
  return hashHex;
}

console.log(await digestMessage("hello!"))
// -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b

console.log(await digestMessage("ハロ!"))
// -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42

//mapでまとめて実行したいとき
let target = ["hello!", "ハロ!"];
console.log(await Promise.all(target.map(s=>digestMessage(s))))
// -> [
"ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b",
"3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"
]

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?