R/folder_encrypt.R

Defines functions folder_encrypt

Documented in folder_encrypt

#' Encrypt a folder.
#' @description
#' Function that encrypts the given object \code{folder_decrypted}.
#' @param folder_decrypted
#' An object of class \code{personfiles}.
#' @inheritParams folder
#' @return
#' Returns invisibly the encrypted \code{folder_decrypted}.
#' @examples
#' ### Encrypt a folder that has no password yet.
#' folder_decrypted <- folder(folder_dir = tempdir(), overwrite = TRUE)
#' folder_encrypt(folder_decrypted = folder_decrypted, password = "secret")

folder_encrypt <- function(folder_decrypted, password = NULL) {
  if(!is_personfiles(folder_decrypted))
    stop("Cannot read folder.", call. = FALSE)
  if(!is.null(password)){
    password <- as.character(password)
    folder_decrypted <- serialize(folder_decrypted, connection = NULL)
    passphrase <- charToRaw(password)
    key <- openssl::sha256(passphrase)
    folder_encrypted <- openssl::aes_cbc_encrypt(folder_decrypted, key = key)
    cat("Folder encrypted.\n")
  } else {
    folder_encrypted <- folder_decrypted
  }
  return(invisible(folder_encrypted))
}
loelschlaeger/personfiles documentation built on Dec. 21, 2021, 11:45 a.m.