encrypt {cyphr} | R Documentation |
Easy encryption and decryption
Description
Wrapper functions for encryption. These functions wrap
expressions that produce or consume a file and arrange to encrypt
(for producing functions) or decrypt (for consuming functions).
The forms with a trailing underscore (encrypt_
,
decrypt_
) do not use any non-standard evaluation and may be
more useful for programming.
Usage
encrypt(expr, key, file_arg = NULL, envir = parent.frame())
decrypt(expr, key, file_arg = NULL, envir = parent.frame())
encrypt_(expr, key, file_arg = NULL, envir = parent.frame())
decrypt_(expr, key, file_arg = NULL, envir = parent.frame())
Arguments
expr |
A single expression representing a function call that would be called for the side effect of creating or reading a file. |
key |
A |
file_arg |
Optional hint indicating which argument to
|
envir |
Environment in which |
Details
These functions will not work for all functions. For example
pdf
/dev.off
will create a file but we can't wrap
those up (yet!). Functions that modify a file (e.g.,
appending) also will not work and may cause data loss.
Examples
# To do anything we first need a key:
key <- cyphr::key_sodium(sodium::keygen())
# Encrypted write.csv - note how any number of arguments to
# write.csv will be passed along
path <- tempfile(fileext = ".csv")
cyphr::encrypt(write.csv(iris, path, row.names = FALSE), key)
# The new file now exists, but you would not be able to read it
# with read.csv because it is now binary data.
file.exists(path)
# Wrap the read.csv call with cyphr::decrypt()
dat <- cyphr::decrypt(read.csv(path, stringsAsFactors = FALSE), key)
head(dat)
file.remove(path)
# If you have a function that is not supported you can specify the
# filename argument directly. For example, with "write.dcf" the
# filename argument is called "file"; we can pass that along
path <- tempfile()
cyphr::encrypt(write.dcf(list(a = 1), path), key, file_arg = "file")
# Similarly for decryption:
cyphr::decrypt(read.dcf(path), key, file_arg = "file")