Uses digest::digest() to hash as (salted) value, using 'SHA-256'. If the x isn't already a character vector, it's converted to one (even if x inherits from character).

This approach protects the actual value of x, while still allowing a downstream user to determine which cells were derived from the same x.

For example, suppose a patient's mrn of '111' is hashed, and the output is 'abc'. (To view it's real value, execute OuhscMunge::hash_and_salt_sha_256(111).) When given the value 'abc', is it computational infeasible to determine the input had been '111' (especially when salted). However, when you see that two visits have an mrn of 'abc', you can determine the same patient generated both visits.

hash_and_salt_sha_256(
  x,
  salt = "",
  min_characters = 1L,
  max_characters = 2048L,
  na_if = c("")
)

Arguments

x

A vector of values to convert. it should be a character vector, or something that can be cast to a character vector.

salt

A single-element character vector.

min_characters

The minimum count of characters that x is allowed to be. Must be an integer or numeric data type.

max_characters

The maximum count of characters that x is allowed to be. Must be an integer or numeric data type.

na_if

A vector of characters that should produce a has of NA_character_. Default of c("").

Value

A character vector.

Author

Will Beasley

Examples

x    <- letters[1:5]

salt <- "abc123"
hash_and_salt_sha_256(x, salt)
#> [1] "757410ac646bebb991cdd8140891b09f2d6cbfd2dca7d6fb08c8a4c0c24af1b3"
#> [2] "afdf613dc350afa36ad903f0f1638eb76c6e370d4495feaa1441b91471e76ebd"
#> [3] "2630a2754b0df7096f44173a985092c8381120b4a60c8938ce5c6b14b5983e0c"
#> [4] "4b2502443aced3c2e1428922f0bcc1e1a0eb10d37dd7b71492f27eddf3250c77"
#> [5] "83129564e4c6d6f8410afc77f0a7ffeb7f8993450f9a703bb4954cb109fffc62"

# If an unsalted hash is desired, leave the `salt` parameter blank
hash_and_salt_sha_256(x)
#> [1] "fb1a678ef965ad4a66c712d2161f20319091cb4e7611e1925df671018c833f72"
#> [2] "61ba1f136aebcd31977da80fae3afffe18b83691e2aa2c61f948d85085fd8e56"
#> [3] "2eea4ca3e3a6518aaffdcd5bada74dc92c3e0170ab17f7826aa1d1222f8a1ef5"
#> [4] "d2aebba0a3c95f6d03acf85bd9d41e602b92675ef4741063ebb2091e064d86cd"
#> [5] "09cab1873b8f76f0d880aa963a5bd2f7c858af261acbde2a6cdee4ce993a12b3"

# By default, a zero-length character produces hash of NA.
hash_and_salt_sha_256(c("a", "", "c"))
#> [1] "fb1a678ef965ad4a66c712d2161f20319091cb4e7611e1925df671018c833f72"
#> [2] NA                                                                
#> [3] "2eea4ca3e3a6518aaffdcd5bada74dc92c3e0170ab17f7826aa1d1222f8a1ef5"