Giter VIP home page Giter VIP logo

safer's Introduction

safer

A consistent interface to encrypt/decrypt strings, objects, files and connections in R.

  • symmetric and asymmetric encryption methods are supported.
  • Based on libsodium crypto library.

Design

There are four functions and their inverses.

  • encryt_string (decrypt_string)
  • encryt_object (decrypt_object)
  • encryt_file (decrypt_file)
  • save_object (retrieve_object)

The following table summarizes their functionality:

Function Input Output Has side-effect
`encryt_string` a string string/raw No
`encryt_object` a R object raw/string No
`encrypt_file` a file on disk TRUE Yes (Output to disk)
`save_object` a R object TRUE Yes (Output to disk)

Examples

library("safer")

String

# symmetric case:
temp <- encrypt_string("hello, how are you", key = "secret")
all(
  is.character(temp)
  , decrypt_string(temp, "secret") == "hello, how are you"
  , class(try(decrypt_string(temp, "nopass"), silent = TRUE)) == "try-error"
  )

## [1] TRUE

res <- encrypt_string("tatvamasi", ascii = FALSE)
isTRUE(identical(decrypt_string(res), "tatvamasi"))

## [1] TRUE

# asymmetric case:
alice <- keypair()
bob   <- keypair()
temp  <- encrypt_string("hello asymmetric", alice$private_key, bob$public_key)
temp2 <- decrypt_string(temp, bob$private_key, alice$public_key)
identical("hello asymmetric", temp2)

## [1] TRUE

Henceforth, we shall default password for symmetric case: pass.

Object

# symmetric case:
temp <- encrypt_object(1:3)
all(
  is.raw(temp)
  , decrypt_object(temp) == 1:3)

## [1] TRUE

temp <- encrypt_object(iris, ascii = TRUE)
all(
  is.character(temp)
  , decrypt_object(temp) == iris
  , identical(decrypt_object(temp), iris))

## [1] TRUE

rm(temp)

# asymmetric case:
alice <- keypair()
bob   <- keypair()
temp  <- encrypt_object(1:10, alice$private_key, bob$public_key)
temp2 <- decrypt_object(temp, bob$private_key, alice$public_key)
identical(1:10, temp2)

## [1] TRUE

File

# symmetric case:
write.table(iris, "iris.csv")
all(
  encrypt_file("iris.csv", outfile = "iris_encrypted.bin")
  , file.exists("iris_encrypted.bin")
  , decrypt_file("iris_encrypted.bin", outfile = "iris_2.csv")
  , file.exists("iris_2.csv")
  , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv")
  , unlink("iris.csv") == 0
  , unlink("iris_2.csv") == 0
  , unlink("iris_encrypted.bin") == 0
)

## [1] TRUE

write.table(iris, "iris.csv")
all(
  encrypt_file("iris.csv", outfile = "iris_encrypted.txt", ascii = TRUE)
  , file.exists("iris_encrypted.txt")
  , decrypt_file("iris_encrypted.txt", outfile = "iris_2.csv", ascii = TRUE)
  , file.exists("iris_2.csv")
  , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv")
  , unlink("iris.csv") == 0
  , unlink("iris_2.csv") == 0
  , unlink("iris_encrypted.txt") == 0
)

## [1] TRUE

# asymmetric case:
alice <- keypair()
bob   <- keypair()
write.table(iris, "iris.csv")
all(
  encrypt_file("iris.csv", alice$private_key, bob$public_key, outfile = "iris_encrypted.bin")
  , file.exists("iris_encrypted.bin")
  , decrypt_file("iris_encrypted.bin", bob$private_key, alice$public_key, outfile = "iris_2.csv")
  , file.exists("iris_2.csv")
  , tools::md5sum("iris_2.csv") == tools::md5sum("iris.csv")
  , unlink("iris.csv") == 0
  , unlink("iris_2.csv") == 0
  , unlink("iris_encrypted.bin") == 0
)

## [1] TRUE

Save

# symmetric case:
all(
  save_object(iris, conn = "iris_safer.bin")
  , identical(retrieve_object(conn = "iris_safer.bin"), iris)
  , unlink("iris_safer.bin") == 0
)

## [1] TRUE

all(
  save_object(iris, conn = "iris_safer_2.txt", ascii = TRUE)
  , identical(retrieve_object(conn = "iris_safer_2.txt", ascii = TRUE), iris)
  , unlink("iris_safer_2.txt") == 0
)

## [1] TRUE

# asymmetric case:
alice <- keypair()
bob   <- keypair()
all(
  save_object(iris, alice$private_key, bob$public_key, conn = "iris_safer.bin")
  , identical(retrieve_object(conn = "iris_safer.bin", bob$private_key, alice$public_key), iris)
  , unlink("iris_safer.bin") == 0
)

## [1] TRUE

safer's People

Contributors

talegari avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

diplodata

safer's Issues

R Safer Package incompatibility with R version 3.5.2

Dear All,

I am facing an issue where the SAFER Package is not available in R version 3.5.2, as we did used this in our code for a prior version so moving to 3.5.2, why is it not available and any alternatives we have to do a similar activity as Safer (if it cannot be made available in R version 3.5.2).

Appreciate your valuable inputs.

What is the algorithm used?

Hi, great package, but what is the crypto algorithm used and how do you select one specific algorithm to use. I am planning to first encrypt a json object in Nodejs and then decrypt it in R. But then I need to know which algorithm I use AES, DES, Bcrypt etc.. Or is it curve25519?

I am trying to encrypt in nodejs and then decrypt it in R with symetric encryption.

Creating shorter encrypted strings

Is it possible to control the length of the encrypted string? For example testing on a vector of 100 strings with 20 characters each, the encrypted strings are 48 characters each which can add substantial size to large data sets with millions or billions of observations.

Connection 'retrieve_object'

Is it possible for the 'conn' argument in 'retrieve_object' to be a URL or other non-local location?

Reading the files from anything other than a local file system returns "Path ... does not exist". Is this functionality that can easily be added either within safer or is there a work-around?

Thanks!
Matt

Make encoding url friendly

base64 encoding produces '+' and '/' which cause problem when used in as a part of url.

TODO:

  • Use base64url
  • Come up with a design to not break backward compatibility
  • Should this be extended beyond encode_string

Issue was raised by Aliona in a private conversation.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.