diff --git a/DESCRIPTION b/DESCRIPTION index 2c39199..be4d55c 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -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 . Uses the - implementation by the Mbed TLS library from the Trusted Firmware Project + implementation by the 'Mbed TLS' library from the Trusted Firmware Project . Authors@R: c(person(given = "Charlie", diff --git a/R/base.R b/R/base.R index 252a27f..633d656 100644 --- a/R/base.R +++ b/R/base.R @@ -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. #' diff --git a/README.Rmd b/README.Rmd index 4e6fe11..ca6ca07 100644 --- a/README.Rmd +++ b/README.Rmd @@ -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 . +Uses the implementation by the 'Mbed TLS' library from the Trusted Firmware Project . ### Installation diff --git a/README.md b/README.md index 7310ebc..7251adb 100644 --- a/README.md +++ b/README.md @@ -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 . ### Installation diff --git a/man/sha3.Rd b/man/sha3.Rd index f1a90be..cea876e 100644 --- a/man/sha3.Rd +++ b/man/sha3.Rd @@ -12,7 +12,7 @@ sha3(x, size = 256L, convert = TRUE) \item{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.} \item{convert}{[default TRUE] whether to output the hash as a character string or, if FALSE, a raw vector.} @@ -22,7 +22,7 @@ A raw vector or character string depending on 'convert'. } \description{ 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. } \details{ For argument 'x', a scalar string or raw vector (with no attributes) diff --git a/src/secret.c b/src/secret.c index ac1c6fe..0bff3f9 100644 --- a/src/secret.c +++ b/src/secret.c @@ -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]; diff --git a/tests/tests.R b/tests/tests.R index 9675ee0..ba89a7c 100644 --- a/tests/tests.R +++ b/tests/tests.R @@ -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")