Skip to content

Commit

Permalink
Add new functions: md5sum(), sha256sum()
Browse files Browse the repository at this point in the history
  • Loading branch information
florisvdh committed Mar 18, 2021
1 parent 4c64cbc commit 08b3307
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 0 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Imports:
forcats,
git2rdata (>= 0.2.0),
magrittr,
openssl,
plyr,
purrr,
rlang,
Expand Down
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export(download_zenodo)
export(expand_types)
export(fileman_folders)
export(fileman_up)
export(md5sum)
export(read_GRTSmh)
export(read_GRTSmh_base4frac)
export(read_GRTSmh_diffres)
Expand All @@ -26,6 +27,7 @@ export(read_types)
export(read_watercourse_100mseg)
export(read_watersurfaces)
export(read_watersurfaces_hab)
export(sha256sum)
importFrom(assertthat,assert_that)
importFrom(assertthat,is.flag)
importFrom(assertthat,is.string)
Expand Down Expand Up @@ -65,8 +67,11 @@ importFrom(dplyr,vars)
importFrom(forcats,fct_reorder)
importFrom(git2rdata,read_vc)
importFrom(magrittr,set_colnames)
importFrom(openssl,md5)
importFrom(openssl,sha256)
importFrom(plyr,mapvalues)
importFrom(purrr,map)
importFrom(purrr,map_chr)
importFrom(rlang,.data)
importFrom(rlang,na_lgl)
importFrom(rprojroot,find_root)
Expand Down
84 changes: 84 additions & 0 deletions R/filemanagement.R
Original file line number Diff line number Diff line change
Expand Up @@ -383,3 +383,87 @@ fileman_up <- function(name,



#' Calculate file checksums (OpenSSL implementation)
#'
#' The functions calculate the checksum (cryptographic digest, hash value) of
#' one or multiple files.
#' They can be used to verify file integrity.
#'
#' The function names were chosen to match those of GNU coreutils.
#'
#' The functions use the OpenSSL implementation of the \code{openssl} package,
#' and silently close file connections.
#' Note that \code{n2khab} will mask
#' \code{\link[tools:md5sum]{tools::md5sum()}},
#' which is a standalone implementation.
#'
#'
#' @param files Character vector of file path(s).
#' File path(s) can be absolute or relative.
#'
#' @return
#' Character vector with the same length as \code{file}.
#'
#' @family functions regarding file management for N2KHAB projects
#'
#' @examples
#' # creating two different temporary files:
#' file1 <- tempfile()
#' file2 <- tempfile()
#' files <- c(file1, file2)
#' file.create(files)
#' writeLines("some text", file(file2))
#'
#' # computing two alternative checksums:
#' md5sum(files)
#' sha256sum(files)
#'
#' \dontrun{
#' # This will error:
#' files <- c(file1, file2, tempfile(), tempfile())
#' md5sum(files)
#' }
#'
#' @importFrom openssl
#' md5
#' sha256
#' @importFrom purrr
#' map_chr
#'
#' @name checksum
#' @export
md5sum <- function(files) {
assert_that_allfiles_exist(files)
checksums <- map_chr(files, ~paste(md5(file(.))))
names(checksums) <- basename(files)
silently_close_connections()
return(checksums)
}

#' @rdname checksum
#' @export
sha256sum <- function(files) {
assert_that_allfiles_exist(files)
checksums <- map_chr(files, ~paste(sha256(file(.))))
names(checksums) <- basename(files)
silently_close_connections()
return(checksums)
}

#' @importFrom assertthat
#' assert_that
#' @keywords internal
assert_that_allfiles_exist <- function(x) {
exist <- file.exists(x)
assert_that(all(exist),
msg = paste0("The following file(s) do not exist:\n",
paste0(x[!exist], collapse = "\n")))
}

#' @keywords internal
silently_close_connections <- function() {
oldopt <- options(warn = -1)[[1]]
closeAllConnections()
options(warn = oldopt)
}

59 changes: 59 additions & 0 deletions man/checksum.Rd

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

1 change: 1 addition & 0 deletions man/download_zenodo.Rd

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

1 change: 1 addition & 0 deletions man/fileman_folders.Rd

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

1 change: 1 addition & 0 deletions man/fileman_up.Rd

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

2 changes: 2 additions & 0 deletions pkgdown/_pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,5 @@ reference:
- fileman_up
- convert_dec_to_base4frac
- convert_base4frac_to_dec
- md5sum
- sha256sum

0 comments on commit 08b3307

Please sign in to comment.