Skip to content

behavior of decrypt_file in case of a mistake #8

@juangomezduaso

Description

@juangomezduaso

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions