Skip to content

Commit

Permalink
docs wording
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 23, 2024
1 parent 1d82ccb commit 5cc6c63
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ URL: https://shikokuchuo.net/secretbase/,
Encoding: UTF-8
Depends:
R (>= 3.5)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
11 changes: 4 additions & 7 deletions R/base.R
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,6 @@
#'
#' @useDynLib secretbase, .registration = TRUE
#'
#' @docType package
#'
"_PACKAGE"

# secretbase - Main Functions --------------------------------------------------
Expand All @@ -47,11 +45,10 @@
#' Returns a SHA-3 hash of the supplied R object.
#'
#' @param x an object.
#' @param bits [default 256L] output size of the returned hash - uses the
#' relevant SHA-3 hash function if one of 224, 256, 384 or 512, or
#' else the SHAKE256 extendable-output function (XOF) for arbitrary values.
#' The supplied value must be between 8 and 2^24, and is automatically
#' coerced to integer.
#' @param bits [default 256L] output size of the returned hash. If one of 224,
#' 256, 384 or 512, uses the relevant SHA-3 cryptographic hash function. For
#' other values, uses the SHAKE256 extendable-output function (XOF). The
#' supplied value must be between 8 and 2^24, and is coerced to integer.
#' @param convert [default TRUE] if TRUE, the hash is converted to its hex
#' representation as a character string, if FALSE, output directly as a raw
#' vector, or if NA, a vector of (32-bit) integer values.
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ sha3("", bits = 512)
Hash arbitrary R objects:

- done in-place, in a 'streaming' fashion, by R serialization but without allocation of the serialized object
- ensures portability by always using R serialization version 3, big endian representation, skipping the headers
- ensures portability by always using R serialization version 3, big endian representation, skipping the headers (which contain R version and native encoding information)

```{r streaming}
sha3(data.frame(a = 1, b = 2), bits = 160)
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ Hash arbitrary R objects:
- done in-place, in a ‘streaming’ fashion, by R serialization but
without allocation of the serialized object
- ensures portability by always using R serialization version 3, big
endian representation, skipping the headers
endian representation, skipping the headers (which contain R version
and native encoding information)

``` r
sha3(data.frame(a = 1, b = 2), bits = 160)
Expand Down
9 changes: 4 additions & 5 deletions man/sha3.Rd

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

25 changes: 15 additions & 10 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,34 +241,39 @@ SEXP secretbase_sha3(SEXP x, SEXP bits, SEXP convert) {
const int size = Rf_asInteger(bits);
if (size < 8 || size > (1 << 24))
Rf_error("'bits' must be between 8 and 2^24");

const size_t outlen = (size_t) (size / 8);
unsigned char output[outlen];

mbedtls_sha3_id id;
mbedtls_sha3_context ctx;
SEXP out;

switch (size) {
case 224:
id = MBEDTLS_SHA3_224; break;
id = MBEDTLS_SHA3_224;
break;
case 256:
id = MBEDTLS_SHA3_256; break;
id = MBEDTLS_SHA3_256;
break;
case 384:
id = MBEDTLS_SHA3_384; break;
id = MBEDTLS_SHA3_384;
break;
case 512:
id = MBEDTLS_SHA3_512; break;
id = MBEDTLS_SHA3_512;
break;
default:
id = MBEDTLS_SHA3_SHAKE256; break;
id = MBEDTLS_SHA3_SHAKE256;
break;
}

mbedtls_sha3_context ctx;
mbedtls_sha3_init(&ctx);
mbedtls_sha3_starts(&ctx, id);

switch (TYPEOF(x)) {
case STRSXP:
if (XLENGTH(x) == 1 && ATTRIB(x) == R_NilValue) {
const char *s = CHAR(STRING_ELT(x, 0));
mbedtls_sha3_update(&ctx, (const uint8_t *) s, (size_t) strlen(s));
mbedtls_sha3_update(&ctx, (const uint8_t *) s, strlen(s));
goto finish;
}
break;
Expand Down Expand Up @@ -300,7 +305,7 @@ SEXP secretbase_sha3(SEXP x, SEXP bits, SEXP convert) {
finish:

mbedtls_sha3_finish(&ctx, output, outlen);

switch (conv) {
case 0:
out = Rf_allocVector(RAWSXP, outlen);
Expand All @@ -312,8 +317,8 @@ SEXP secretbase_sha3(SEXP x, SEXP bits, SEXP convert) {
default:
out = Rf_allocVector(INTSXP, outlen / sizeof(int));
memcpy(STDVEC_DATAPTR(out), output, outlen);
break;
}

return out;

}

0 comments on commit 5cc6c63

Please sign in to comment.