An open API service indexing awesome lists of open source software.

https://github.com/dirkschumacher/blake3

BLAKE3 Cryptographic Hash Function in R
https://github.com/dirkschumacher/blake3

blake3 cryptography hashing r

Last synced: about 1 year ago
JSON representation

BLAKE3 Cryptographic Hash Function in R

Awesome Lists containing this project

README

          

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```
# blake3

[![Lifecycle: experimental](https://img.shields.io/badge/lifecycle-experimental-orange.svg)](https://www.tidyverse.org/lifecycle/#experimental)
[![Travis build status](https://travis-ci.org/dirkschumacher/blake3.svg?branch=master)](https://travis-ci.org/dirkschumacher/blake3)
[![Codecov test coverage](https://codecov.io/gh/dirkschumacher/blake3/branch/master/graph/badge.svg)](https://codecov.io/gh/dirkschumacher/blake3?branch=master)
[![AppVeyor build status](https://ci.appveyor.com/api/projects/status/github/dirkschumacher/blake3?branch=master&svg=true)](https://ci.appveyor.com/project/dirkschumacher/blake3)

`blake3` is an interface to the [BLAKE3](https://github.com/BLAKE3-team/BLAKE3) cryptographic hash function.

To quote blake3's readme:

> BLAKE3 is a cryptographic hash function that is:
>
> * Much faster than MD5, SHA-1, SHA-2, SHA-3, and BLAKE2.
> * Secure, unlike MD5 and SHA-1. And secure against length extension, unlike SHA-2.
> * Highly parallelizable across any number of threads and SIMD lanes, because it's a Merkle > tree on the inside.
> * Capable of verified streaming and incremental updates, again because it's a Merkle tree.
> * A PRF, MAC, KDF, and XOF, as well as a regular hash.
> * One algorithm with no variants, which is fast on x86-64 and also on smaller architectures.

> *BLAKE3 is not a password hashing algorithm, because it's designed to be fast, whereas password hashing should not be fast. If you hash passwords to store the hashes or if you derive keys from passwords, we recommend Argon2.*

If you want to store passwords in R, please also take a look at [sodium::password_store](https://download.libsodium.org/doc/password_hashing/default_phf).

## Installation

``` r
remotes::install_github("dirkschumacher/blake3")
```

Also make sure to have a very recent rust compiler. The best way to install the rust toolchain is to use [rustup](https://rustup.rs/).

## API

It is still a very early version of the package. The api currently just has one function to hash `RAW` vectors with an optional 32 byte `RAW` key parameter.

## Example

```{r example}
library(blake3)

input <- "This is a string"
hash <- blake3_hash_raw(charToRaw(input))
(sodium::bin2hex(hash))

```

```{r}
# you can also hash arbitrary R objects by serializing it first
input <- serialize(LETTERS, NULL, version = 2)
hash <- blake3_hash_raw(input)
(sodium::bin2hex(hash))
```

```{r}
# keyed hashes are also supported
key <- blake3_hash_raw(charToRaw("test"))
input <- serialize(LETTERS, NULL, version = 2)
hash <- blake3_hash_raw(input, key)
(sodium::bin2hex(hash))
```

## Benchmark
To get an idea about the speed we hash a random string of 1 million characters and compare it to the `openssl` implementation of `sha1`, `sha2` and `md5`.
```{r}
library(openssl)
set.seed(42)
input <- charToRaw(paste0(sample(LETTERS, 1e6, replace = TRUE), collapse = ""))
microbenchmark::microbenchmark(
md5 = md5(input),
sha1 = sha1(input),
sha2 = sha2(input),
blake3 = blake3_hash_raw(input),
times = 1000
)
```

## License

MIT