Skip to contents

WARNING: Use this function with caution! Check the parameters carefully.

lock_dir() and unlock_dir() encrypt or decrypt all files from a given directory using an OpenSSL RSA key pair.

Usage

lock_dir(
  dir,
  public_key = here::here("_ssh", "id_rsa.pub"),
  suffix = ".lockr",
  remove_file = TRUE
)

unlock_dir(
  dir,
  private_key = here::here("_ssh", "id_rsa"),
  suffix = ".lockr",
  remove_file = TRUE,
  password = NULL
)

Arguments

dir

A character string indicating the directory to encrypt or decrypt.

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 NULL. These functions are called for side effects only.

See also

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

Examples

## Locking files -----

ssh_dir <- tempfile("ssh")
dir.create(ssh_dir)

rsa_keygen(ssh_dir)
#>  Keys successfully created at /tmp/Rtmpu67Iup/ssh19f64f18f216.

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

for (i in seq_len(5)) file.create(tempfile(tmpdir = temp_dir))

list.files(temp_dir)
#> [1] "file19f612e48730" "file19f625d46003" "file19f62e93c8f8" "file19f665db8d77"
#> [5] "file19f67d620525"

temp_dir |>
  lock_dir(
    public_key = file.path(ssh_dir, "id_rsa.pub")
  )
#>  Locked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f612e48730.lockr.
#>  Locked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f625d46003.lockr.
#>  Locked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f62e93c8f8.lockr.
#>  Locked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f665db8d77.lockr.
#>  Locked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f67d620525.lockr.

## Unlocking files -----

temp_dir |>
  unlock_dir(
    private_key = file.path(ssh_dir, "id_rsa")
  )
#>  Unlocked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f612e48730.
#>  Unlocked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f625d46003.
#>  Unlocked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f62e93c8f8.
#>  Unlocked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f665db8d77.
#>  Unlocked file written at /tmp/Rtmpu67Iup/dir19f659a1a2f1/file19f67d620525.