R equivalent of Python's hashlib.sha256 function

Viewed 1789

I'm trying to replicate the output of the following Python code in R:

import hashlib

x = hashlib.sha256()
x.update("asdf".encode("utf8"))
print(x.digest())
# b'\xf0\xe4\xc2\xf7lX\x91n\xc2X\xf2F\x85\x1b\xea\t\x1d\x14\xd4$z/\xc3\xe1\x86\x94F\x1b\x18\x16\xe1;'

This is my R code:

library(digest)
digest("asdf", algo="sha256", serialize=FALSE)
# "f0e4c2f76c58916ec258f246851bea091d14d4247a2fc3e18694461b1816e13b"

I'm able to get this same output in python by using x.hexdigest() instead of x.digest(). How do I get the output of x.digest() in my R code?

1 Answers

The Python output is the raw bytes of the digest. The R digest function also supports this with the raw argument.

digest("asdf", algo="sha256", serialize=FALSE, raw=TRUE)
Related