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

behavior of decrypt_file in case of a mistake #8

Open
juangomezduaso opened this issue Oct 28, 2020 · 0 comments
Open

behavior of decrypt_file in case of a mistake #8

juangomezduaso opened this issue Oct 28, 2020 · 0 comments

Comments

@juangomezduaso
Copy link

Hi:
I am starting to look at this package, and while experimenting with decrypt_file(), it´s behavior when you give a wrong password in the dialog striked me.
The error you get leaves a zero length decrypted file created anyways, and also, I couldn´t inmediately delete it as it was still "in use" by my R session.

I wonder if some improvement could be done to this function by just catching the error and cleaning what openSSL does.
For instance:

decrypt_file<- function (.path, file_name = NULL, private_key_path = "id_rsa") {
if (!file.exists(.path)) {
stop("Encrypted file cannot be found.")
}
if (!file.exists(private_key_path)) {
stop("Private key cannot be found. \n Should be created with encryptr::genkeys")
}
if (!grepl(".encryptr.bin$", .path)) {
stop("Encrypted file has incorrect name. \n Should be created with encryptr::encrypt_file and end with '.encryptr.bin'")
}
if (is.null(file_name)) {
.file = gsub(".encryptr.bin", "", .path)
}
else {
.file = file_name
}
if (file.exists(.file)) {
stop("Unencrtyped file with same name exists at this location. \n Move or choose new name (file_name) to avoid it being overwritten.")
}
.crypt = readRDS(.path)
zz = file(.file, "wb")
tryCatch(
error= function(cnd){
close(zz)
file.remove(.file)
stop(cnd)
},

openssl::decrypt_envelope(.crypt$data, .crypt$iv, .crypt$session,
key = private_key_path, password = openssl::askpass()) %>%
writeBin(zz)
)
close(zz)
if (file.exists(.file)) {
cat("Decrypted file written with name '", .file,
"'\n", sep = "")
}
}

Regards

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant