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)

Arguments

x

object

...

additional arguments for methods - not used at the moment

Value

sha256 hash object

Details

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).

Examples

## hash of a file: hash(system.file("DESCRIPTION", package = "ROriginStamp"))
#> #> Create sha356 hash from R file x [/private/var/folders/50/wcr5bjwn75q595n6x82gxj280000gq/T/RtmpBkcx2Y/temp_libpathd0c20fc5a9f/ROriginStamp/DESCRIPTION]
#> 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)
#> #> x is already a hash - returning x unprocessed
#> [1] "2c5d36be542f8f0e7345d77753a5d7ea61a443ba6a9a86bb060332ad56dba38e"