A generic function function to calculate the sha256 hash from an R object or from a file.
hash(x) # S3 method for hash hash(x, ...) # S3 method for default hash(x, ...) # S3 method for file hash(x)
x | object |
---|---|
... | additional arguments for methods - not used at the moment |
sha256 hash object
The behavior depends on the class of the argument x
:
an object of class sha256
as returned by the package openssl: the hash is returned as is
character
vector of length 1 containing the name of an existing file: the sha256 hash of the file is calculated
any other R object: is serialized using `serialize(x, connection = NULL, ascii = FALSE, xdr = TRUE, version = 2, refhook = NULL)`` and the sha256 hash is created from that serialized object.
The methods doing the work are:
hash.hash(x)
: returns the object x
hash.file()
: assumes x
is an existing file and tries to calculate the hash from the file
hash.default()
: calculates the hash from the serialized R object x
NB: These functions do assume the object x
is what they expect it to be.
It is recommended to use the generic function hash()
as this functiuon
auti=omatically selects the most appropriate method. Only use these if you
need to force a certain method (e.g. calculate the hash of a file name
instead of the existing file).
Remark: if a hash is in haracter form, it can be converted to a hash
object, by assigning the class hash
(see example).
#> #>#> sha256 af:69:1f:06:ee:50:03:18:22:9c:00:13:ed:57:00:ed:e1:bb:11:09:19:30:13:92:64:9a:2e:8d:d0:43:5d:1e## convert a character vector to a hash: x <- "2c5d36be542f8f0e7345d77753a5d7ea61a443ba6a9a86bb060332ad56dba38e" class(x) <- "hash" hash(x)#> #>#> [1] "2c5d36be542f8f0e7345d77753a5d7ea61a443ba6a9a86bb060332ad56dba38e"