library(openssl)
library(digest)
library(tidyverse)
# md5 ---------------------------------------------------------------------
# Support both strings and binary
md5(c("foo", "bar"))
md5("foo", key = "secret")
hash <- md5(charToRaw("foo"))
hash
as.character(hash, sep = ":")
# Compare to digest
digest::digest("foo", "md5", serialize = FALSE)
# Other way around
cars
digest::digest(cars, skip = 0)
md5(serialize(cars, NULL))
# Stream-verify from connections (including files)
myfile <- system.file("CITATION")
md5(file(myfile))
md5(file(myfile), key = "secret")
## Not run: check md5 from: http://cran.r-project.org/bin/windows/base/old/3.1.1/md5sum.txt
md5(url("http://cran.r-project.org/bin/windows/base/old/3.1.1/R-3.1.1-win.exe"))
## End(Not run)
# Use a salt to prevent dictionary attacks
sha1("admin") # googleable
sha1("admin", key = "random_salt_value") #not googleable
# Use a random salt to identify duplicates while anonymizing values
sha256("john") # googleable
sha256(c("john", "mary", "john"), key = "random_salt_value")
# keys --------------------------------------------------------------------
key <- ec_keygen()
key
pubkey <- key$pubkey
pubkey
bin <- write_der(pubkey)
print(bin)
# aes256 ------------------------------------------------------------------
# Symmetric AES encryption ------------------------------------------------
# aes-256 requires 32 byte key
passphrase <- charToRaw("This is super secret")
passphrase
rawToChar(passphrase)
key <- sha256(passphrase)
# symmetric encryption uses same key for decryption
x <- serialize(iris, NULL)
x
y <- aes_cbc_encrypt(x, key = key)
y
x2 <- aes_cbc_decrypt(y, key = key)
x2
stopifnot(identical(x, x2))
# bignum ------------------------------------------------------------------
x <- bignum(123L)
x
y <- bignum("123456789123456789")
y
z <- bignum("D41D8CD98F00B204E9800998ECF8427E", hex = TRUE)
z
# Basic arithmetic
div <- z %/% y
mod <- z %% y
z2 <- div * y + mod
stopifnot(z2 == z)
stopifnot(div < z)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.