R/utils.R

#' get_os
#'
#' Returns the operating system. From \url{http://conjugateprior.org/2015/06/identifying-the-os-from-r/}
#'
#' @return A string of the operating system
get_os <- function(){
  sysinf <- Sys.info()
  if (!is.null(sysinf)) {
    os <- sysinf['sysname']
    if (os == 'Darwin')
      os <- "osx"
  } else {
    os <- .Platform$OS.type
    if (grepl("^darwin", R.version$os))
      os <- "osx"
    if (grepl("linux-gnu", R.version$os))
      os <- "linux"
  }
  tolower(os)
}

#' load_rsa_key
#'
#' @param key_location Where the SSH key is located. User can set the
#' environmental variable SSH_KEY_PATH and taRpan will use that to decrypt.
#' If no path provided, it defaults to location saved to config, then to
#' SSH_KEY_PATH environmental variable, then ~/.ssh/id_rsa.pub
load_rsa_key <- function(key_location) {
  if (missing(key_location) || is.null(key_location))
    if (isTRUE(is.character(.tarpan$config$key_location)) &&
        isTRUE(nchar(.tarpan$config$key_location) > 0))
      key_location <- .tarpan$config$key_location
    else
      key_location <- getOption("SSH_KEY_PATH", "~/.ssh/id_rsa")

  if (file.exists(key_location))
    .tarpan$rsa_key <- read_key(key_location)
  else
    .tarpan$rsa_key <- rsa_keygen()

  message("RSA Key loaded!")

  invisible()
}

#' @rdname crypt
tarpan_key_available <- function() {
  if (is.null(.tarpan$rsa_key))
    FALSE
  else
    TRUE
}

#' @rdname crypt
tarpan_key <- function() {
  if (tarpan_key_available())
    .tarpan$rsa_key
  else {
    load_rsa_key()
    tarpan_key()
  }
}

#' Value encryption/decryption
#'
#' @param value The value to be decrypted
#' @inheritParams load_ssh_key
#'
#' @name crypt
tarpan_decrypt_value <- function(value) {
  unserialize(rsa_decrypt(hex2bin(value), tarpan_key()))
}

#' @rdname crypt
tarpan_encrypt_value <- function(value) {
  bin2hex(rsa_encrypt(serialize(value, NULL), tarpan_key()))
}

#' Load Config
#'
#' Loads taRpan config from config file.
#'
#' @export
tarpan_load_config <- function() {
  .tarpan$config <- append(fromJSON(tarpan_config_file()), .tarpan$config)

  invisible()
}
GlobalParametrics/taRpan_readonly documentation built on May 13, 2019, 11:23 a.m.