Skip to content

Commit

Permalink
set bounds on output size
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 20, 2024
1 parent 09affaa commit 6c4546f
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Version: 0.0.2
Description: Fast, dependency-free SHA-3 cryptographic hash and SHAKE256
extendable-output function (XOF) algorithms. The SHA-3 Secure Hash Standard
was published by NIST in 2015 at <doi:10.6028/NIST.FIPS.202>. Uses the
implementation by the Mbed TLS library from the Trusted Firmware Project
implementation by the 'Mbed TLS' library from the Trusted Firmware Project
<https://www.trustedfirmware.org/projects/mbed-tls/>.
Authors@R:
c(person(given = "Charlie",
Expand Down
4 changes: 2 additions & 2 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@
#' Cryptographic Hashing Using the SHA-3 Algorithm
#'
#' Returns a SHA-3 hash of the supplied R object. This implementation uses code
#' from the Mbed TLS library, under the Trusted Firmware Project.
#' from the 'Mbed TLS' library, under the Trusted Firmware Project.
#'
#' @param x an object.
#' @param size [default 256L] integer output size (bits) of the returned hash -
#' uses the relevant SHA-3 algorithm if one of 224, 256, 384 or 512, or else
#' the SHAKE256 algorithm as an extendable-output function (XOF) for
#' arbitrary bit lengths.
#' arbitrary bit lengths. The supplied value must be between 8 and 2^24.
#' @param convert [default TRUE] whether to output the hash as a character
#' string or, if FALSE, a raw vector.
#'
Expand Down
4 changes: 2 additions & 2 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ knitr::opts_chunk$set(

Fast, dependency-free SHA-3 cryptographic hash and SHAKE256 extendable-output function (XOF) algorithms.

The SHA-3 Secure Hash Standard was published by NIST in 2015 at [doi:10.6028/NIST.FIPS.202](http://dx.doi.org/10.6028/NIST.FIPS.202).
The SHA-3 Secure Hash Standard was published by NIST in 2015 at [doi:10.6028/NIST.FIPS.202](https://dx.doi.org/10.6028/NIST.FIPS.202).

Uses the implementation by the Mbed TLS library from the Trusted Firmware Project <https://www.trustedfirmware.org/projects/mbed-tls/>.
Uses the implementation by the 'Mbed TLS' library from the Trusted Firmware Project <https://www.trustedfirmware.org/projects/mbed-tls/>.

### Installation

Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Fast, dependency-free SHA-3 cryptographic hash and SHAKE256
extendable-output function (XOF) algorithms.

The SHA-3 Secure Hash Standard was published by NIST in 2015 at
[doi:10.6028/NIST.FIPS.202](http://dx.doi.org/10.6028/NIST.FIPS.202).
[doi:10.6028/NIST.FIPS.202](https://dx.doi.org/10.6028/NIST.FIPS.202).

Uses the implementation by the Mbed TLS library from the Trusted
Uses the implementation by the Mbed TLS library from the Trusted
Firmware Project <https://www.trustedfirmware.org/projects/mbed-tls/>.

### Installation
Expand Down
4 changes: 2 additions & 2 deletions man/sha3.Rd

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

4 changes: 2 additions & 2 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ static SEXP nano_hash_char(unsigned char *buf, const size_t sz) {
SEXP secretbase_sha3(SEXP x, SEXP size, SEXP convert) {

const int bits = Rf_asInteger(size);
if (bits < 0)
Rf_error("'size' should not be negative");
if (bits < 8 || bits > (1 << 24))
Rf_error("'size' must be between 8 and 2^24");

const size_t outlen = (size_t) (bits / 8);
unsigned char output[outlen];
Expand Down
6 changes: 4 additions & 2 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ test_equal(sha3("", 256), "a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80
test_equal(sha3("", 384), "0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004")
test_equal(sha3("", 512), "a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26")
test_equal(sha3("secret base", size = 32), "995ebac1")
test_equal(sha3(data.frame(a = 1, b = 2)), "05d4308e79d029b4af5604739ecc6c4efa1f602a23add0ed2d247b7407d4832f")
test_equal(sha3(sha3("secret base", size = 32, convert = FALSE), size = 32), "4d872090")
test_that(sha3(rnorm(1e5), size = 8196), is.character)
test_that(sha3(c("secret", "base"), size = 0), length)
test_equal(sha3(c("secret", "base")), "d906024c71828a10e28865a80f5e81d2cb5cd74067d44852d7039813ba62b0b6")
test_equal(read_integer(sha3("secret base", size = 32, convert = FALSE)), -1044750695L)
test_equal(read_integer(2:4), 2L)
test_that(read_integer(NULL), is.integer)
test_that(read_integer(substitute()), is.integer)
test_error(sha3("secret base", size = -1), "'size' should not be negative")
test_error(sha3("secret base", size = 0), "'size' must be between 8 and 2^24")
test_error(sha3("secret base", size = -1), "'size' must be between 8 and 2^24")

0 comments on commit 6c4546f

Please sign in to comment.