Skip to content

Commit

Permalink
optimize for SipHash-1-3
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Apr 4, 2024
1 parent caba4ce commit ac03505
Show file tree
Hide file tree
Showing 11 changed files with 78 additions and 110 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Package: secretbase
Type: Package
Title: Cryptographic Hash and Extendable-Output Functions
Version: 0.3.0.9007
Version: 0.3.0.9008
Description: Fast and memory-efficient streaming hash functions. Performs direct
hashing of strings, raw bytes, and files potentially larger than memory, as
well as hashing in-memory objects through R's serialization mechanism,
Expand Down
1 change: 0 additions & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,4 @@
export(sha256)
export(sha3)
export(siphash13)
export(siphash24)
useDynLib(secretbase, .registration = TRUE)
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# secretbase 0.3.0.9007 (development)
# secretbase 0.3.0.9008 (development)

* Adds HMAC generation to `sha256()`.
* Adds SipHash pseudo-random function (PRF) as a fast, cryptographically-strong keyed hash.
Expand Down
20 changes: 6 additions & 14 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,8 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
#' SipHash Pseudorandom Function
#'
#' Returns a fast, cryptographically-strong SipHash keyed hash of the supplied
#' object or file. SipHash-1-3 is optimised for performance, whereas
#' SipHash-2-4 is recommended for security. Note: SipHash is not a
#' cryptographic hash algorithm.
#' object or file. SipHash-1-3 is optimised for performance. Note: SipHash
#' is not a cryptographic hash algorithm.
#'
#' @inheritParams sha3
#' @param key [default NULL] a character string or raw vector comprising the 16
Expand Down Expand Up @@ -179,11 +178,11 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
#' # SipHash-1-3 hash as raw vector:
#' siphash13("secret base", convert = FALSE)
#'
#' # SipHash-2-4 hash using a character string key:
#' siphash24("secret", key = "base")
#' # SipHash-1-3 hash using a character string key:
#' siphash13("secret", key = "base")
#'
#' # SipHash-2-4 hash using a raw vector key:
#' siphash24("secret", key = charToRaw("base"))
#' # SipHash-1-3 hash using a raw vector key:
#' siphash13("secret", key = charToRaw("base"))
#'
#' # SipHash-1-3 hash a file:
#' file <- tempfile(); cat("secret base", file = file)
Expand All @@ -195,10 +194,3 @@ sha256 <- function(x, key = NULL, convert = TRUE, file)
siphash13 <- function(x, key = NULL, convert = TRUE, file)
if (missing(file)) .Call(secretbase_siphash13, x, key, convert) else
.Call(secretbase_siphash13_file, file, key, convert)

#' @rdname siphash13
#' @export
#'
siphash24 <- function(x, key = NULL, convert = TRUE, file)
if (missing(file)) .Call(secretbase_siphash24, x, key, convert) else
.Call(secretbase_siphash24_file, file, key, convert)
36 changes: 20 additions & 16 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,16 @@ install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")

### Quick Start

#### SHA-3 and XOF usage:
```{r secretbase}
library(secretbase)
```

#### SHA-3

- For the SHA-3 cryptographic hash algorithm, specify 'bits' as `224`, `256`, `384` or `512`
- For the SHAKE256 extendable-output function (XOF), specify any other bit length

```{r secretbase}
library(secretbase)
```{r sha3}
sha3("secret base")
sha3("secret base", convert = FALSE)
Expand All @@ -63,9 +65,10 @@ sha3("秘密の基地の中", bits = 512)
```

#### Hash arbitrary R objects:
#### Hash arbitrary R objects

- Uses memory-efficient 'streaming' serialization, without allocation of the serialized object
- Character strings and raw vectors (without attributes) are hashed 'as is'
- Other objects are hashed using memory-efficient 'streaming' serialization, without allocation of the serialized object
- Portable as always uses R serialization version 3 big-endian representation, skipping headers (which contain R version and native encoding information)

```{r streaming}
Expand All @@ -74,7 +77,7 @@ sha3(data.frame(a = 1, b = 2), bits = 160)
sha3(NULL)
```

#### Hash files:
#### Hash files

- Performed in a streaming fashion, accepting files larger than memory

Expand All @@ -86,7 +89,7 @@ sha3(file = file)
unlink(file)
```

#### Hash to integer:
#### Hash to integer

- Specify 'convert' as `NA` (and 'bits' as `32` for a single integer value)
- May be supplied as deterministic random seeds for R's pseudo random number generators (RNGs)
Expand All @@ -99,24 +102,25 @@ sha3("秘密の基地の中", bits = 32, convert = NA)

For use in parallel computing, this is a valid method for reducing to a negligible probability that RNGs in each process may overlap. This may be especially suitable when first-best alternatives such as using recursive streams are too expensive or unable to preserve reproducibility. <sup>[2]</sup>

#### Generating a SHA-256 HMAC:
#### SHA-256

- Use `sha256()` passing a character string or raw vector to 'key'.
```{r sha256}
sha256("secret base")
```

- For a SHA-256 HMAC, pass a character string or raw vector to 'key'

```{r hmac}
sha256("secret base", key = "秘密の基地の中")
```

#### Using SipHash:
#### SipHash

- SipHash is a fast, cryptographically-strong keyed hash.
- Pass a character string or raw vector to 'key'. Up to 16 bytes (128 bits) of the key data is used.
- SipHash-1-3 is optimized for performance; SipHash-2-4 recommended for security.
- SipHash-1-3 is optimized for performance
- Pass a character string or raw vector to 'key' - up to 16 bytes (128 bits) of the key data is used

```{r siphash}
siphash13("secret base", key = charToRaw("秘密の基地の中"))
siphash24("secret base", key = charToRaw("秘密の基地の中"))
```

### References
Expand Down
42 changes: 23 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,18 @@ install.packages("secretbase", repos = "https://shikokuchuo.r-universe.dev")

### Quick Start

#### SHA-3 and XOF usage:
``` r
library(secretbase)
```

#### SHA-3

- For the SHA-3 cryptographic hash algorithm, specify ‘bits’ as `224`,
`256`, `384` or `512`
- For the SHAKE256 extendable-output function (XOF), specify any other
bit length

``` r
library(secretbase)

sha3("secret base")
#> [1] "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520"

Expand All @@ -78,10 +80,12 @@ sha3("秘密の基地の中", bits = 512)
#> [1] "e30cdc73f6575c40d55b5edc8eb4f97940f5ca491640b41612e02a05f3e59dd9c6c33f601d8d7a8e2ca0504b8c22f7bc69fa8f10d7c01aab392781ff4ae1e610"
```

#### Hash arbitrary R objects:
#### Hash arbitrary R objects

- Uses memory-efficient ‘streaming’ serialization, without allocation of
the serialized object
- Character strings and raw vectors (without attributes) are hashed ‘as
is’
- Other objects are hashed using memory-efficient ‘streaming’
serialization, without allocation of the serialized object
- Portable as always uses R serialization version 3 big-endian
representation, skipping headers (which contain R version and native
encoding information)
Expand All @@ -94,7 +98,7 @@ sha3(NULL)
#> [1] "b3e37e4c5def1bfb2841b79ef8503b83d1fed46836b5b913d7c16de92966dcee"
```

#### Hash files:
#### Hash files

- Performed in a streaming fashion, accepting files larger than memory

Expand All @@ -104,7 +108,7 @@ sha3(file = file)
#> [1] "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520"
```

#### Hash to integer:
#### Hash to integer

- Specify ‘convert’ as `NA` (and ‘bits’ as `32` for a single integer
value)
Expand All @@ -126,29 +130,29 @@ be especially suitable when first-best alternatives such as using
recursive streams are too expensive or unable to preserve
reproducibility. <sup>\[2\]</sup>

#### Generating a SHA-256 HMAC:
#### SHA-256

- Use `sha256()` passing a character string or raw vector to ‘key’.
``` r
sha256("secret base")
#> [1] "1951c1ca3d50e95e6ede2b1c26fefd0f0e8eba1e51a837f8ccefb583a2b686fe"
```

- For a SHA-256 HMAC, pass a character string or raw vector to ‘key’

``` r
sha256("secret base", key = "秘密の基地の中")
#> [1] "ec58099ab21325e792bef8f1aafc0a70e1a7227463cfc410931112705d753392"
```

#### Using SipHash:
#### SipHash

- SipHash is a fast, cryptographically-strong keyed hash.
- Pass a character string or raw vector to ‘key’. Up to 16 bytes (128
bits) of the key data is used.
- SipHash-1-3 is optimized for performance; SipHash-2-4 recommended for
security.
- SipHash-1-3 is optimized for performance
- Pass a character string or raw vector to ‘key’ - up to 16 bytes (128
bits) of the key data is used

``` r
siphash13("secret base", key = charToRaw("秘密の基地の中"))
#> [1] "a1f0a751892cc7dd"

siphash24("secret base", key = charToRaw("秘密の基地の中"))
#> [1] "1bedfe817cac0562"
```

### References
Expand Down
16 changes: 6 additions & 10 deletions man/siphash13.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions src/init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,6 @@ static const R_CallMethodDef callMethods[] = {
{"secretbase_sha256_file", (DL_FUNC) &secretbase_sha256_file, 3},
{"secretbase_siphash13", (DL_FUNC) &secretbase_siphash13, 3},
{"secretbase_siphash13_file", (DL_FUNC) &secretbase_siphash13_file, 3},
{"secretbase_siphash24", (DL_FUNC) &secretbase_siphash24, 3},
{"secretbase_siphash24_file", (DL_FUNC) &secretbase_siphash24_file, 3},
{NULL, NULL, 0}
};

Expand Down
1 change: 0 additions & 1 deletion src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,6 @@ typedef struct secretbase_sha256_context {

typedef struct secretbase_siphash_context {
int skip;
unsigned N;
CSipHash *ctx;
} secretbase_siphash_context;

Expand Down
Loading

0 comments on commit ac03505

Please sign in to comment.