1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
|
#' Vectorized hash/hmac functions
#'
#' All hash functions either calculate a hash-digest for \code{key == NULL} or HMAC
#' (hashed message authentication code) when \code{key} is not \code{NULL}. Supported
#' inputs are binary (raw vector), strings (character vector) or a connection object.
#'
#' The most efficient way to calculate hashes is by using input \link{connections},
#' such as a \link[base:connections]{file()} or \link[base:connections]{url()} object.
#' In this case the hash is calculated streamingly, using almost no memory or disk space,
#' regardless of the data size. When using a connection input in the \link{multihash}
#' function, the data is only read only once while streaming to multiple hash functions
#' simultaneously. Therefore several hashes are calculated simultanously, without the
#' need to store any data or download it multiple times.
#'
#' Functions are vectorized for the case of character vectors: a vector with \code{n}
#' strings returns \code{n} hashes. When passing a connection object, the contents will
#' be stream-hashed which minimizes the amount of required memory. This is recommended
#' for hashing files from disk or network.
#'
#' The sha2 family of algorithms (sha224, sha256, sha384 and sha512) is generally
#' recommended for sensitive information. While sha1 and md5 are usually sufficient for
#' collision-resistant identifiers, they are no longer considered secure for cryptographic
#' purposes.
#'
#' In applications where hashes should be irreversible (such as names or passwords) it is
#' often recommended to use a random \emph{key} for HMAC hashing. This prevents attacks where
#' we can lookup hashes of common and/or short strings. See examples. A common special case
#' is adding a random salt to a large number of records to test for uniqueness within the
#' dataset, while simultaneously rendering the results incomparable to other datasets.
#'
#' The \code{blake2b} and \code{blake2s} algorithms are only available if your system has
#' libssl 1.1 or newer.
#'
#' @param x character vector, raw vector or connection object.
#' @param key string or raw vector used as the key for HMAC hashing
#' @param size must be equal to 224 256 384 or 512
#' @references Digest types: \url{https://www.openssl.org/docs/man1.1.1/man1/openssl-dgst.html}
#' @export
#' @aliases hmac mac
#' @rdname hash
#' @name hashing
#' @useDynLib openssl R_digest_raw R_digest
#' @examples # Support both strings and binary
#' md5(c("foo", "bar"))
#' md5("foo", key = "secret")
#'
#' hash <- md5(charToRaw("foo"))
#' as.character(hash, sep = ":")
#'
#' # Compare to digest
#' digest::digest("foo", "md5", serialize = FALSE)
#'
#' # Other way around
#' 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")
#'
#' \dontrun{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"))}
#'
#' # 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")
sha1 <- function(x, key = NULL){
rawstringhash(x, "sha1", key)
}
#' @rdname hash
#' @export
sha224 <- function(x, key = NULL){
rawstringhash(x, "sha224", key)
}
#' @rdname hash
#' @export
sha256 <- function(x, key = NULL){
rawstringhash(x, "sha256", key)
}
#' @rdname hash
#' @export
sha384 <- function(x, key = NULL){
rawstringhash(x, "sha384", key)
}
#' @rdname hash
#' @export
sha512 <- function(x, key = NULL){
rawstringhash(x, "sha512", key)
}
#' @rdname hash
#' @export
sha2 <- function(x, size = 256, key = NULL){
rawstringhash(x, paste0("sha", size), key)
}
#' @rdname hash
#' @export
md4 <- function(x, key = NULL){
rawstringhash(x, "md4", key)
}
#' @rdname hash
#' @export
md5 <- function(x, key = NULL){
rawstringhash(x, "md5", key)
}
#' @rdname hash
#' @export
blake2b <- function(x, key = NULL){
rawstringhash(x, "blake2b512", key)
}
#' @rdname hash
#' @export
blake2s <- function(x, key = NULL){
rawstringhash(x, "blake2s256", key)
}
#' @rdname hash
#' @export
ripemd160 <- function(x, key = NULL){
rawstringhash(x, "ripemd160", key)
}
#' @rdname hash
#' @export
#' @param algos string vector with names of hashing algorithms
multihash <- function(x, algos = c('md5', 'sha1', 'sha256', 'sha384', 'sha512')){
if(inherits(x, 'connection')){
connectionhashes(x, algos = algos)
} else if(is.raw(x)){
out <- lapply(algos, function(algo){rawstringhash(x, algo = algo, key = NULL)})
structure(out, names = algos)
} else if(is.character(x)){
m <- vapply(algos, function(algo){stringhash(x, algo = algo, key = NULL)}, FUN.VALUE = x)
if(length(x) == 1)
m <- t(m)
data.frame(m, stringsAsFactors = FALSE)
}
}
# Low level interfaces, not exported.
rawhash <- function(x, algo, key = NULL){
stopifnot(is.raw(x))
stopifnot(is.null(key) || is.raw(key))
.Call(R_digest_raw, x, as.character(algo), key)
}
#' @useDynLib openssl R_digest
stringhash <- function(x, algo, key = NULL){
stopifnot(is.character(x))
stopifnot(is.null(key) || is.raw(key))
.Call(R_digest,x, as.character(algo), key)
}
connectionhashes <- function(con, algos){
if(!isOpen(con)){
open(con, "rb")
on.exit(close(con))
}
mds <- lapply(algos, function(algo){
structure(md_init(algo), algo = algo)
})
if(summary(con)$text == "binary"){
while(length(data <- readBin(con, raw(), 512*1024))){
lapply(mds, md_feed, data = data)
}
} else {
while(length(data <- readLines(con, n = 1L, warn = FALSE))){
lapply(mds, md_feed, data = charToRaw(data))
}
}
hashes <- lapply(mds, function(md){
structure(md_final(md), class = c("hash", attr(md, 'algo')))
})
structure(hashes, names = algos)
}
connectionhmac <- function(con, algo, key){
if(is.character(key))
key <- charToRaw(key)
hmac <- hmac_init(algo, key);
if(!isOpen(con)){
open(con, "rb")
on.exit(close(con))
}
if(summary(con)$text == "binary"){
while(length(data <- readBin(con, raw(), 1024))){
hmac_feed(hmac, data)
}
} else {
while(length(data <- readLines(con, n = 1L, warn = FALSE))){
hmac_feed(hmac, charToRaw(data))
}
}
hmac_final(hmac)
}
rawstringhash <- function(x, algo, key){
if(is.character(key))
key <- charToRaw(key)
hash <- if(inherits(x, "connection")){
if(is.null(key)){
connectionhashes(x, algo)[[algo]]
} else {
connectionhmac(x, algo, key)
}
} else if(is.raw(x)){
rawhash(x, algo, key)
} else if(is.character(x)){
stringhash(x, algo, key)
} else {
stop("Argument 'x' must be raw or character vector.")
}
out <- structure(hash, class = c("hash", algo))
if(!is.null(key))
class(out) <- c(class(out), "hmac")
out
}
hash_type <- function(hash){
if(!is.raw(hash))
stop("hash must be raw vector or hex string")
if(inherits(hash, "md5") || length(hash) == 16){
"md5"
} else if(inherits(hash, "sha1") || length(hash) == 20){
"sha1"
} else if(inherits(hash, "sha256") || length(hash) == 32){
"sha256"
} else{
stop("Hash of length ", length(hash), " not supported")
}
}
is_hexraw <- function(str){
is.character(str) &&
(length(str) == 1) &&
grepl("^[a-f0-9 :]+$", tolower(str))
}
hex_to_raw <- function(str){
stopifnot(length(str) == 1)
str <- gsub("[ :]", "", str)
len <- nchar(str)/2
out <- raw(len)
for(i in 1:len){
out[i] <- as.raw(as.hexmode(substr(str, 2*i-1, 2*i)))
}
out
}
parse_hash <- function(x){
if(is.raw(x)) return(x)
if(is.character(x)) return(hex_to_raw(x[1]))
stop("Invalid hash: ", x)
}
#' @export
print.hash <- function(x, sep = ":", ...){
if(is.raw(x))
cat(class(x)[-1], as.character(x, sep = sep), "\n")
else
print(unclass(x, ...))
}
#' @export
as.character.hash <- function(x, sep = "", ...){
if(is.raw(x))
structure(paste(unclass(x), collapse = sep), class = class(x))
else if(is.character(x))
unclass(x)
else x
}
|