Skip to contents

lock_file() and unlock_file encrypt or decrypt any kind of file using an OpenSSL RSA key pair.

Usage

lock_file(
  file,
  public_key = here::here(".ssh", "id_rsa.pub"),
  suffix = ".lockr",
  remove_file = TRUE
)

unlock_file(
  file,
  private_key = here::here(".ssh", "id_rsa"),
  suffix = ".lockr",
  remove_file = TRUE,
  password = NULL
)

Arguments

file

A character string with the file path to be encrypted/decrypted. For security reasons, encrypted files must end with the suffix parameter.

public_key

(optional) An openssl RSA public key or a character string specifying the public key path. See rsa_keygen() to learn how to create an RSA key pair (default: here::here(".ssh", "id_rsa.pub")).

suffix

(optional) A character string specifying the suffix to add when encrypting or remove when decrypting. Must start with . (default: ".lockr").

remove_file

(optional) A logical value indicating if the original file must be removed after the encryption/decryption process (default: TRUE).

private_key

(optional) An openssl RSA private key or a character string specifying the private key path. See rsa_keygen() to learn how to create an RSA key pair (default: here::here(".ssh", "id_rsa")).

password

(optional) A character string specifying the password for decrypting a protected private key. For security, avoid hardcoding passwords in scripts. Use askpass() to prompt for secure input (default: NULL).

Value

An invisible string containing the locked/unlocked file path.

See also

Other lock/unlock functions: lock_dir(), save_and_lock()

Examples

## Creating Test Files and Keys -----

temp_dir <- tempfile("dir")
dir.create(temp_dir)

temp_file <- tempfile(tmpdir = temp_dir)

rsa_keygen(temp_dir)
#>  Keys successfully created at /tmp/RtmpwNDBbf/dir1c0d40635a9b.

con <- file(temp_file, "w+")
cat("Test", file = temp_file, sep = "\n")
list.files(temp_dir)
#> [1] "file1c0d3f26f3da" "id_rsa"           "id_rsa.pub"      
suppressWarnings(readLines(con))
#> [1] "Test"
close(con)

## Locking Files -----

temp_file |>
  lock_file(
    public_key = file.path(temp_dir, "id_rsa.pub")
  )
#>  Locked file written at /tmp/RtmpwNDBbf/dir1c0d40635a9b/file1c0d3f26f3da.lockr.

temp_file_locked <- paste0(temp_file, ".lockr")

con <- file(temp_file_locked, "rb")
list.files(temp_dir)
#> [1] "file1c0d3f26f3da.lockr" "id_rsa"                 "id_rsa.pub"            
suppressWarnings(readLines(con))
#> [1] "\037\x8b\b"                                                                                                                                                                                      
#> [2] ">\xf9\x9d\006\x9b\xa2W\xdaH\xac\xda(\xbe\xd0\xebwů\025^\x8f\xb5\v\xef\xffe\021\xee\xb4jܖuyf\xf2\xee\xc5\xfb?\xe6\x9a\xcbT\xd7\025o\x9f.6\xf3\xc1\xc2S\xda˿*\x89f\xce4\x88"                       
#> [3] "\016[[\xc9\xf9\xc5\xce\xc2a熃L\xe2N-\x8b#\xa5|\xae\x9d\xdaY\xf7\xe7\xe4>\x86\xc3G\xcbLxJ\x82\x8d\xa7\xd6o\xf9\xf8\xdcjŦ"                                                                         
#> [4] "o\xf6\xc4ɜ\xcd5\x91\x90\x9eɧc\001\v\x9f\x84ٶ\xdb.=\xd5Y\xfbkŭl\xa3s\xdf\03610\xb001\x80\002\x88\x85\x81\023\024\xa8y\x89\xb9\xa9\xc5 \x85\x90\xd0\006\v2e\x96AY\xecũ\xc5ř\xf9yP.KJbI\"\x90\xfe\a"
close(con)

## Unlocking Files -----

temp_file_locked |>
  unlock_file(
    private_key = file.path(temp_dir, "id_rsa")
  )
#>  Unlocked file written at /tmp/RtmpwNDBbf/dir1c0d40635a9b/file1c0d3f26f3da.

list.files(temp_dir)
#> [1] "file1c0d3f26f3da" "id_rsa"           "id_rsa.pub"      
con <- file(temp_file, "r+")
readLines(con)
#> [1] "Test"
close(con)