-
Notifications
You must be signed in to change notification settings - Fork 12
/
encrypt_file.R
71 lines (64 loc) · 2.42 KB
/
encrypt_file.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#' Encrypt a file
#'
#' Encryption and decryption with asymmetric keys is computationally expensive.
#' This is how \code{\link{encrypt}} works, in order to allow each piece of data
#' in a data frame to be decrypted without compromise of the whole data frame.
#' This works on the presumption that each cell contains less than 245 bytes of
#' data.
#'
#' File encryption requires a different approach as files are often larger in
#' size. This function encrypts a file using a a symmetric "session" key and the
#' AES-256 cipher. This key is itself then encrypted using a public key
#' generated using \code{\link{genkeys}}. In OpenSSL this combination is
#' referred to as an envelope.
#'
#' @param .path Quoted path to file to encrypt.
#' @param crypt_file_name Optional new name to give encrypted file. Must end with ".encryptr.bin".
#' @param public_key_path Quoted path to public key, created with
#' \code{\link{genkeys}}.
#'
#' @return The encrypted file is saved.
#' @export
#'
#' @examples
#' # This will run:
#' # Create example file to encrypt
#' # write.csv(gp, "gp.csv")
#' # genkeys()
#' # encrypt_file("gp.csv")
#'
#' # For CRAN and testing:
#' \dontrun{
#' # Run only once in decrypt_file example
#' temp_dir = tempdir() # temp directory for testing only
#' genkeys(file.path(temp_dir, "id_rsa"))
#' write.csv(gp, file.path(temp_dir, "gp.csv"))
#' encrypt_file(file.path(temp_dir, "gp.csv"), public_key_path = file.path(temp_dir, "id_rsa.pub"))
#' }
encrypt_file <- function(.path, crypt_file_name = NULL, public_key_path = "id_rsa.pub") {
if (!file.exists(.path)) {
stop("File for encryption cannot be found.")
}
# The following doesn't work with URL
# if (!file.exists(public_key_path)) {
# stop("Public key cannot be found. \n Should be created with encryptr::genkeys")
# }
if(is.null(crypt_file_name)){
.crypt_file = paste0(.path, ".encryptr.bin")
} else {
.crypt_file = crypt_file_name
if (!grepl(".encryptr.bin$", .crypt_file)){
stop("Encrypted file has incorrect name. \n Should be created with encryptr::encrypt_file and end with '.encryptr.bin'")
}
}
if (file.exists(.crypt_file)) {
stop("Encrypted file with this name already exists. Delete or choose a new name.")
}
# Encrypt
openssl::encrypt_envelope(.path, public_key_path) %>%
saveRDS(file = .crypt_file)
if (file.exists(.crypt_file)){
cat("Encrypted file written with name '",
.crypt_file, "'\n", sep = "")
}
}