A consistent interface to encrypt/decrypt strings, objects, files and connections in R. Both symmetric and asymmetric encryption methods are supported. Thanks to excellent packages sodium and base64enc.

Design

There are four functions and their (inverses).

  • encryt_string (decrypt_string)
  • encryt_object (decrypt_object)
  • encryt_file (decrypt_file)
  • save_object (retrieve_object)

The following table summarizes their functionality:

Function Input Output Has side-effect
encryt\_string a string string/raw No
encryt\_object a R object raw/string No
encrypt\_file a file on disk TRUE Yes (Output to disk)
save\_object a R object TRUE Yes (Output to disk)

Examples

library("safer")

Save

# symmetric case:
all(
  save_object(iris, conn = "iris_safer.bin")
  , identical(retrieve_object(conn = "iris_safer.bin"), iris)
  , unlink("iris_safer.bin") == 0
)

## [1] TRUE

all(
  save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE)
  , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris)
  , unlink("iris_safer_2.txt") == 0
)

## [1] TRUE

# asymmetric case:
alice <- keypair()
bob   <- keypair()
all(
  save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin")
  , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris)
  , unlink("iris_safer.bin") == 0
)

## [1] TRUE