Skip to content

Instantly share code, notes, and snippets.

@talegari
Last active December 1, 2016 12:18
Show Gist options
  • Save talegari/7aa04e59bd81b44f130e90850863f102 to your computer and use it in GitHub Desktop.
Save talegari/7aa04e59bd81b44f130e90850863f102 to your computer and use it in GitHub Desktop.
[R] Utility to convert a file into an encrypted string using `base64enc` package and vice versa
#######################################################################
# FileStringR
#######################################################################
# author : talegari (Srikanth KS)
# license : GNU AGPLv3 (http://choosealicense.com/licenses/agpl-3.0/)
# about : a utility to convert a file into an encrypted string using
# `base64enc` package and vice versa
# depends : R (>= 3)with packages: `tools`, `base64enc`, `assertthat`
# `sodium`
#######################################################################
#######################################################################
# file_string
#######################################################################
# convert a file to a string
# string is written to a file with name of the inputFile appened by .txt
# encrypt the string using a key(a string)
file_string = function(inputFile
, key = NULL
, silent = FALSE){
assertthat::assert_that(file.exists(inputFile))
assertthat::assert_that(assertthat::is.readable(inputFile))
outputFile <- paste0(inputFile, ".", "txt")
assertthat::assert_that(!file.exists(outputFile))
assertthat::assert_that(is.null(key) || assertthat::is.string(key))
assertthat::assert_that(assertthat::is.flag(silent))
if(!is.null(key)){
key <- sodium::hash(charToRaw(key), size = 32)
# get sodium output
so <- sodium::data_encrypt(
msg = charToRaw(base64enc::base64encode(what = inputFile))
, key = key
, nonce = base64enc::base64decode("TMfldNKuD1N191QTIooIZ49xrwnRzdqa")
)
aString <- base64enc::base64encode(so)
} else{
aString <- base64enc::base64encode(what = inputFile)
}
ofc <- file(outputFile, "w")
on.exit(close(ofc))
writeLines(text = aString , con = ofc)
if(silent == FALSE){
message(paste0(" DONE! encoded output written to: ", outputFile))
}
return(invisible(TRUE))
}
#######################################################################
# string_file
#######################################################################
# convert a string to a file
# string is read from the inputFile and output file is created by removing
# ".txt" extension from the inputFile's name
string_file = function(inputFile
, key = NULL
, silent = FALSE
, remove = FALSE){
assertthat::assert_that(file.exists(inputFile))
assertthat::assert_that(assertthat::is.readable(inputFile))
outputFile <- tools::file_path_sans_ext(inputFile)
assertthat::assert_that(!file.exists(outputFile))
if(is.null(key)){
# write the base64 decoded msg to file
ofc <- file(outputFile, "wb")
base64enc::base64decode(file = inputFile, output = ofc)
} else{
msg_bin <- base64enc::base64decode(file = inputFile)
if(length(msg_bin) == 0){
stop("Invalid input file.")
}
key <- sodium::hash(charToRaw(key), size = 32)
nonce <- base64enc::base64decode("TMfldNKuD1N191QTIooIZ49xrwnRzdqa")
# check whether the msg and key correspond
msg_ser <- try(sodium::data_decrypt(bin = msg_bin
, key = key
, nonce = nonce)
, silent = TRUE)
if(class(msg_ser) == "try-error"){
stop("The file is corrupted OR the key is wrong.")
}
ofc <- file(outputFile, "wb")
base64enc::base64decode(what = rawToChar(msg_ser)
, output = ofc
)
}
close(ofc)
if(silent == FALSE){
message(paste0(" DONE! decoded output written to: ", outputFile))
}
if(remove == TRUE){
unlink(inputFile)
}
return(invisible(TRUE))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment