# The functions defined in this file need package "PKI"
# generateKeyFile --------------------------------------------------------------
#' Generate a Decryption Key File
#'
#' @param target full path to the file to which the key shall be written
#' @export
#' @seealso \code{\link{createPasswordFile}}, \code{\link{getPassword}}
generateKeyFile <- function(target)
{
checkNamespace("PKI", "generateKeyFile")
PKI::PKI.save.key(key = PKI::PKI.genRSAkey(), target = target)
}
# checkNamespace ---------------------------------------------------------------
checkNamespace <- function(packageName, functionName)
{
if (! requireNamespace(packageName, quietly = TRUE)) {
stop(
"Please install the package ", hsQuoteChr(packageName), " to use ",
"this function: ", hsQuoteChr(functionName), call. = FALSE
)
}
}
# createPasswordFile -----------------------------------------------------------
#' Create a File Storing a Password in Encrypted Form
#'
#' @param account optional. Name of account the user is asked to enter the
#' password for. Only used for the input prompt.
#' @param keyFile path to the file containing the encryption/decryption key,
#' as generated by \code{\link{generateKeyFile}}
#' @param passwordFile path to the password file to be created. An existing file
#' will be overwritten!
#' @param password password for account. If \code{NULL} (default) the user will
#' be asked to enter the password on the console
#' @export
#' @seealso \code{\link{generateKeyFile}}, \code{\link{getPassword}}
createPasswordFile <- function(
account = NULL, keyFile, passwordFile, password = NULL
)
{
checkNamespace("PKI", "createPasswordFile")
password <- defaultIfNULL(password, askForPassword(account))
password.encrypted <- PKI::PKI.encrypt(
charToRaw(password), key = PKI::PKI.load.key(file = keyFile)
)
writeBin(password.encrypted, passwordFile)
}
# askForPassword ---------------------------------------------------------------
askForPassword <- function(account = NULL)
{
clearConsole()
prompt <- sprintf(
"Enter password%s: ",
if (is.null(account)) "" else paste0("for account '", account, "'")
)
userInput <- readline(prompt)
clearConsole()
invisible(userInput)
}
# getPassword ------------------------------------------------------------------
#' Get Encrypted Password from File Using Key
#'
#' Given the path to a file containing an encrypted password and given the path
#' to the file containing the key that was used to do the encryption this
#' function returns the original password, invisibly.
#'
#' @param passwordFile path to the password file, as generated by
#' \code{\link{createPasswordFile}}.
#' @param keyFile path to the key file, as generated by
#' \code{\link{generateKeyFile}}.
#' @return The password (character) or \code{NA} if no password is stored, both
#' invisibly.
#' @export
#' @seealso \code{\link{generateKeyFile}}, \code{\link{createPasswordFile}}
getPassword <- function(passwordFile, keyFile)
{
checkNamespace("PKI", "getPassword")
if (! file.exists(passwordFile)) {
stop("Password file '", passwordFile, "' does not exist.")
}
password.encrypted <- readBin(passwordFile, what = "raw", n = 256)
invisible(rawToChar(PKI::PKI.decrypt(
what = password.encrypted,
key = PKI::PKI.load.key(file = keyFile)
)))
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.