[LINUX] Find the SHA256 value with R (with bonus)

It's a memo because I have too few opportunities to use it and I can't remember when I thought about using it.

Execution environment

SHA256 in R

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!"))
'

Make sure the result is really correct

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.

Linux commands

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

Find the SHA256 value with R (with bonus)
Find the definition of the value of errno
[Python] Find the second smallest value.
Extract the maximum value with pandas.
Find the Levenshtein Distance with python
Find the optimal value of a function with a genetic algorithm (Part 2)
Find the maximum value python (fixed ver)
Find the second derivative with JAX automatic differentiation
Find a position above the threshold with NumPy
Find the divisor of the value entered in python
Find out the day of the week with datetime
Describe ec2 with boto3 and retrieve the value
Find the shortest path with the Python Dijkstra's algorithm
Find the sum of unique values with pandas crosstab
Take the value of SwitchBot thermo-hygrometer with Raspberry Pi
Log the value of SwitchBot thermo-hygrometer with Raspberry Pi
Find out the location of packages installed with pip
Extract the maximum value with pandas and change that value
Find the maximum Python
I tried to find the entropy of the image with python
Memo to get the value on the html-javascript side with jupyter
I tried to find the average of the sequence with TensorFlow
Inherit the standard library to find the average value of Queue
Your URL didn't respond with the value of the challenge parameter.
Find the index of the maximum value (minimum value) of a multidimensional array
Multiply the x-axis value by n when plotting with pylab
The true value of Terraform automation starting with Oracle Cloud