It's a memo because I have too few opportunities to use it and I can't remember when I thought about using it.
SHA256 in R
if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("hello!"))
# -> [1] "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"
if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("Halo!"))
# -> [1] "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"
When running from the command line
When running from the command line
Rscript -e '
if (!require("openssl")) install.packages("openssl")
print(openssl::sha256("hello!"))
'
I'm worried if it really matches ... I thought that this anxiety could not be dispelled without checking whether the results were the same in different environments, so when I looked it up, it was easy enough to write in one liner in many cases, so it is a memo.
I wondered if I should run two or three of the following that seem to be possible, including Japanese, and check if the results are the same.
SHA256 test (Linux command)
#echo+use sha256sum
echo -n "hello!" | sha256sum
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b -
echo -n "Halo!" | sha256sum
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42 -
#echo+use openssl
echo -n "hello!" | openssl sha256
# -> (stdin)= ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b
echo -n "Halo!" | openssl sha256
# -> (stdin)= 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42
The printf
command may be better, as the echo
command may forget the -n
option.
SHA256 test with printf (Linux command)
printf "hello!" | sha256sum
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b -
printf "Halo!" | sha256sum
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42 -
Node.js13.5
SHA256 test (Node.js)
console.log(require("crypto").createHash("sha256").update("hello!").digest("hex"))
// -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b
console.log(require("crypto").createHash("sha256").update("Halo!").digest("hex"))
// -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42
When running from the command line
When running from the command line
node -e '
console.log(require("crypto").createHash("sha256").update("hello!").digest("hex"))
'
Python3.5
SHA256 test (Python)
#3.5
print(__import__("hashlib").sha256("hello!".encode("utf-8")).hexdigest())
print(__import__("hashlib").sha256("Halo!".encode("utf-8")).hexdigest())
When running from the command line
When running from the command line
python3 -c '
print(__import__("hashlib").sha256("hello!".encode("utf-8")).hexdigest())
'
Ruby2.3
SHA256 test (Ruby)
require "digest/sha2"
print Digest::SHA256.hexdigest("hello!")
# -> "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"
require "digest/sha2"
print Digest::SHA256.hexdigest("Halo!")
# -> "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"
#Or
require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("hello!")
# -> "ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b"
require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("Halo!")
# -> "3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"
When running from the command line
When running from the command line
ruby -e '
require "openssl"
print OpenSSL::Digest::SHA256.hexdigest("hello!")
'
Perl5
SHA256 test (Perl)
use Digest::SHA qw(sha256_hex);
print sha256_hex("hello!");
# -> ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b
use Digest::SHA qw(sha256_hex);
print sha256_hex("Halo!");
# -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42
When running from the command line
When running from the command line
perl -e '
use Digest::SHA qw(sha256_hex);
print sha256_hex("hello!");
'
JavaScript
SHA256 test (JavaScript)
/*
https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/Quoted from 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("Halo!"))
// -> 3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42
//When you want to execute all at once with map
let target = ["hello!", "Halo!"];
console.log(await Promise.all(target.map(s=>digestMessage(s))))
// -> [
"ce06092fb948d9ffac7d1a376e404b26b7575bcc11ee05a4615fef4fec3a308b",
"3564dcbd2cb0145ebf4a2f1219f9354a4c91936f0bdfe545bbc5140bcd7bba42"
]
Recommended Posts