Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update genkeys() interface #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions DESCRIPTION
Expand Up @@ -21,10 +21,14 @@ LazyData: true
BugReports: https://github.com/SurgicalInformatics/encryptr/issues
URL: https://github.com/SurgicalInformatics/encryptr
Imports:
askpass,
dplyr,
knitr,
openssl,
purrr,
readr,
rlang
RoxygenNote: 6.1.0
Suggests:
testthat,
withr
23 changes: 15 additions & 8 deletions R/genkeys.R
Expand Up @@ -10,6 +10,8 @@
#' reason.
#' @param public_key_name Character string. Do not change default unless good
#' reason.
#' @param password Password for private key. This password cannot be recovered
#' if lost, so please store the password in a safe location.
#'
#' @return Two files containing the public key and encrypted private key are
#' written to the working directory.
Expand All @@ -19,7 +21,9 @@
#'
#' @examples
#' genkeys()
genkeys <- function(private_key_name = "id_rsa", public_key_name = "id_rsa.pub"){
genkeys <- function(private_key_name = "id_rsa",
public_key_name = paste0(private_key_name, ".pub"),
password = ask_pass()){
if(file.exists(private_key_name)){
stop("Private key file with this name already exists. Delete it or change file name.")
}
Expand All @@ -30,11 +34,14 @@ genkeys <- function(private_key_name = "id_rsa", public_key_name = "id_rsa.pub")
key <- openssl::rsa_keygen()
pubkey <- as.list(key)$pubkey

openssl::write_pem(key,
"id_rsa",
password = openssl::askpass(prompt = "Please choose a password for your private key.
This password CANNOT be recovered if lost.
Please store the password in a safe location."))
openssl::write_pem(pubkey,
"id_rsa.pub")
openssl::write_pem(key, private_key_name, password = password)
openssl::write_pem(pubkey, public_key_name)
}

ask_pass <- function() {
askpass::askpass(prompt = paste(
"Please choose a password for your private key.",
"This password CANNOT be recovered if lost.",
"Please store the password in a safe location.",
sep = "\n"))
}
7 changes: 6 additions & 1 deletion man/genkeys.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions tests/testthat.R
@@ -0,0 +1,4 @@
library(testthat)
library(encryptr)

test_check("encryptr")
54 changes: 54 additions & 0 deletions tests/testthat/test-genkeys.R
@@ -0,0 +1,54 @@
context("test-genkeys")

# Run tests in temp directory
# https://github.com/r-lib/testthat/issues/664#issuecomment-340809997

dir_empty <- function(x){
unlink(x, recursive = TRUE, force = TRUE)
dir.create(x)
}

test_with_dir <- function(desc, ...){
new <- tempfile()
dir_empty(new)
withr::with_dir( # or local_dir()
new = new,
code = {
tmp <- capture.output(
testthat::test_that(desc = desc, ...)
)
}
)
invisible()
}

test_with_dir("genkeys() defaults create id_rsa and id_rsa.pub", {
genkeys(password = NULL)
expect_true(file.exists("id_rsa"))
expect_true(file.exists("id_rsa.pub"))
})

test_with_dir("genkeys() errors if files already exist", {
file.create("id_rsa.pub")
expect_error(genkeys(password = NULL), regexp = "Public key .+ exists")
file.create("id_rsa")
expect_error(genkeys(password = NULL), regexp = "Private key .+ exists")
})

test_with_dir("genkeys() creates keys with requested names", {
genkeys("custom", password = NULL)
expect_true(file.exists("custom"))
expect_true(file.exists("custom.pub"))

genkeys("public", "private", password = NULL)
expect_true(file.exists("public"))
expect_true(file.exists("private"))
})

test_with_dir("genkeys() uses password argument", {
genkeys(password = "testpassword")
check_message <- "Message"
x <- openssl::rsa_encrypt(charToRaw(check_message), "id_rsa.pub")
y <- openssl::rsa_decrypt(x, "id_rsa", "testpassword")
expect_equal(rawToChar(y), check_message)
})