Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Method to encrypt a single field #5

Open
aetiologicCanada opened this issue Jun 6, 2017 · 4 comments
Open

Method to encrypt a single field #5

aetiologicCanada opened this issue Jun 6, 2017 · 4 comments

Comments

@aetiologicCanada
Copy link

Often one needs to encrypt one or more fields from a tibble/dataframe/data table with a function which returns the cypher to the appropriate row of the table, and then one would drop the plaintext. eg.

iris_confidential <- iris %?>% mutate(
encrypted_species = magic_function(species, ...)
) %>% select (-species)

I have been unable to coax sodium into doing a single-column encryption, or more precisely when I do I get an object back that has more observations than the data frame has rows. One can't then push the resulting vector back into the data_frame. If there a way to coax magic_function() out of sodium?

See:

library(sodium)
key <- hash(charToRaw("This is a secret passphrase"))
msg <- serialize(iris$Species, NULL)
str(msg)
raw [1:748] 58 0a 00 00 ...
nonce <- random(24)
cipher <- data_encrypt(msg, key, nonce)

str(cipher)
atomic [1:764] 55 31 fa 90 ...

  • attr(*, "nonce")= raw [1:24] 03 8e 9b 37 ...

You can't stuff either cipher or msg back into the data frame because the row-numbers don't align.

@jeroen
Copy link
Member

jeroen commented Jun 6, 2017

You could store them in a list column like this:

# Example data
key <- hash(charToRaw("This is a secret passphrase"))
mydata <- iris
mydata$cipher <- lapply(mydata$Species, function(x) {
  sodium::data_encrypt(serialize(as.character(x), NULL), key = key)
})

vapply(mydata$cipher, function(cipher){
  unserialize(sodium::data_decrypt(cipher, key = key))
}, "character")

@jeroen
Copy link
Member

jeroen commented Jun 6, 2017

Alternatively you could convert each raw value to a string using e.g. sodium::bin2hex or jsonlite::base64_enc.

@aetiologicCanada
Copy link
Author

aetiologicCanada commented Jun 6, 2017 via email

@jeroen
Copy link
Member

jeroen commented Jun 6, 2017

In that case you probably want to map the raw vectors to strings with sodium::bin2hex.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants