R/folder_decrypt.R

Defines functions folder_decrypt

Documented in folder_decrypt

#' Decrypt a folder.
#' @description
#' Function that decrypts the given object \code{folder_encrypted}.
#' @param folder_encrypted
#' An object, which must be of class \code{personfiles} after decryption.
#' @inheritParams folder
#' @return
#' Returns invisibly the decrypted \code{folder_encrypted}.
#' @examples
#' ### Decrypt a folder with password "secret".
#' folder_encrypted <- folder(password = "secret", folder_dir = tempdir(),
#'                            overwrite = TRUE)
#' folder_decrypt(folder_encrypted = folder_encrypted, password = "secret")

folder_decrypt <- function(folder_encrypted, password = NULL) {
  if(!is.null(password)){
    password <- as.character(password)
    passphrase <- charToRaw(password)
    key <- openssl::sha256(passphrase)
    folder_decrypted <- try(
      unserialize(openssl::aes_cbc_decrypt(folder_encrypted,key = key)),
      silent = TRUE)
    if(inherits(folder_decrypted,"try-error"))
      stop("Wrong password.", call. = FALSE)
    cat("Folder decrypted.\n")
  } else {
    folder_decrypted <- folder_encrypted
  }
  if(!is_personfiles(folder_decrypted))
    stop("Cannot read folder.", call. = FALSE)
  return(invisible(folder_decrypted))
}
loelschlaeger/personfiles documentation built on Dec. 21, 2021, 11:45 a.m.