diff --git a/DESCRIPTION b/DESCRIPTION index 1774049..04f1bbb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,13 +1,13 @@ Package: secretbase Type: Package Title: Cryptographic Hash, Extendable-Output and Base64 Functions -Version: 1.0.1.9000 +Version: 1.0.2 Description: Fast and memory-efficient streaming hash functions and base64 - encoding and decoding. Performs direct hashing of strings and raw vectors. - Stream hashes files potentially larger than memory, as well as in-memory - objects through R's serialization mechanism. Implementations include the - SHA-256, SHA-3 and 'Keccak' cryptographic hash functions, SHAKE256 - extendable-output function (XOF), and 'SipHash' pseudo-random function. + encoding / decoding. Hashes strings and raw vectors directly. Stream hashes + files potentially larger than memory, as well as in-memory objects through + R's serialization mechanism. Implementations include the SHA-256, SHA-3 and + 'Keccak' cryptographic hash functions, SHAKE256 extendable-output function + (XOF), and 'SipHash' pseudo-random function. Authors@R: c(person(given = "Charlie", family = "Gao", diff --git a/NEWS.md b/NEWS.md index a23b6ee..84f798a 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,6 +1,6 @@ -# secretbase 1.0.1.9000 (development) +# secretbase 1.0.2 -* Improves performance in most situations by optimizing buffer sizes, especially when hashing large files. +* Improves hash performance in most situations, especially for large files, by optimizing buffer sizes. # secretbase 1.0.1 diff --git a/R/secret.R b/R/secret.R index 5688d03..45658ab 100644 --- a/R/secret.R +++ b/R/secret.R @@ -18,12 +18,12 @@ #' secretbase: Cryptographic Hash, Extendable-Output and Base64 Functions #' -#' Fast and memory-efficient streaming hash functions and base64 encoding and -#' decoding. Performs direct hashing of strings and raw vectors. Stream -#' hashes files potentially larger than memory, as well as in-memory objects -#' through R's serialization mechanism. Implementations include the SHA-256, -#' SHA-3 and 'Keccak' cryptographic hash functions, SHAKE256 -#' extendable-output function (XOF), and 'SipHash' pseudo-random function. +#' Fast and memory-efficient streaming hash functions and base64 encoding / +#' decoding. Hashes strings and raw vectors directly. Stream hashes files +#' potentially larger than memory, as well as in-memory objects through R's +#' serialization mechanism. Implementations include the SHA-256, SHA-3 and +#' 'Keccak' cryptographic hash functions, SHAKE256 extendable-output +#' function (XOF), and 'SipHash' pseudo-random function. #' #' @encoding UTF-8 #' @author Charlie Gao \email{charlie.gao@@shikokuchuo.net} diff --git a/README.Rmd b/README.Rmd index fafafe0..08d2368 100644 --- a/README.Rmd +++ b/README.Rmd @@ -31,9 +31,9 @@ knitr::opts_chunk$set( \/_______/ ``` -Fast and memory-efficient streaming hash functions and base64 encoding and decoding. +Fast and memory-efficient streaming hash functions and base64 encoding / decoding. -Performs direct hashing of strings and raw vectors. Stream hashes files potentially larger than memory, as well as in-memory objects through R's serialization mechanism. +Hashes strings and raw vectors directly. Stream hashes files potentially larger than memory, as well as in-memory objects through R's serialization mechanism. Implementations include the SHA-256, SHA-3 and 'Keccak' cryptographic hash functions, SHAKE256 extendable-output function (XOF), and 'SipHash' pseudo-random function. @@ -55,7 +55,7 @@ sha3("秘密の基地の中", bits = 512L) #### Hash strings and raw vectors -Character strings and raw vectors are hashed directly (as per the above). +Character strings and raw vectors are hashed directly (as above). #### Stream hash R objects @@ -102,7 +102,7 @@ keccak("secret base", bits = 384L) sha256("secret base") ``` -For a SHA-256 HMAC, pass a character string or raw vector to 'key': +For SHA-256 HMAC, pass a character string or raw vector to 'key': ```{r hmac} sha256("secret base", key = "秘密の基地の中") ``` diff --git a/README.md b/README.md index 189632e..9c54849 100644 --- a/README.md +++ b/README.md @@ -22,11 +22,11 @@ badge](https://shikokuchuo.r-universe.dev/badges/secretbase?color=ff803d)](https \ / base / \/_______/ -Fast and memory-efficient streaming hash functions and base64 encoding -and decoding. +Fast and memory-efficient streaming hash functions and base64 encoding / +decoding. -Performs direct hashing of strings and raw vectors. Stream hashes files -potentially larger than memory, as well as in-memory objects through R’s +Hashes strings and raw vectors directly. Stream hashes files potentially +larger than memory, as well as in-memory objects through R’s serialization mechanism. Implementations include the SHA-256, SHA-3 and ‘Keccak’ cryptographic @@ -56,8 +56,7 @@ sha3("秘密の基地の中", bits = 512L) #### Hash strings and raw vectors -Character strings and raw vectors are hashed directly (as per the -above). +Character strings and raw vectors are hashed directly (as above). #### Stream hash R objects @@ -118,7 +117,7 @@ sha256("secret base") #> [1] "1951c1ca3d50e95e6ede2b1c26fefd0f0e8eba1e51a837f8ccefb583a2b686fe" ``` -For a SHA-256 HMAC, pass a character string or raw vector to ‘key’: +For SHA-256 HMAC, pass a character string or raw vector to ‘key’: ``` r sha256("secret base", key = "秘密の基地の中") diff --git a/man/secretbase-package.Rd b/man/secretbase-package.Rd index e1d3aba..de9306a 100644 --- a/man/secretbase-package.Rd +++ b/man/secretbase-package.Rd @@ -7,12 +7,12 @@ \alias{secretbase-package} \title{secretbase: Cryptographic Hash, Extendable-Output and Base64 Functions} \description{ -Fast and memory-efficient streaming hash functions and base64 encoding and - decoding. Performs direct hashing of strings and raw vectors. Stream - hashes files potentially larger than memory, as well as in-memory objects - through R's serialization mechanism. Implementations include the SHA-256, - SHA-3 and 'Keccak' cryptographic hash functions, SHAKE256 - extendable-output function (XOF), and 'SipHash' pseudo-random function. +Fast and memory-efficient streaming hash functions and base64 encoding / + decoding. Hashes strings and raw vectors directly. Stream hashes files + potentially larger than memory, as well as in-memory objects through R's + serialization mechanism. Implementations include the SHA-256, SHA-3 and + 'Keccak' cryptographic hash functions, SHAKE256 extendable-output + function (XOF), and 'SipHash' pseudo-random function. } \seealso{ Useful links: diff --git a/src/base.c b/src/base.c index 3b53803..2006683 100644 --- a/src/base.c +++ b/src/base.c @@ -297,7 +297,7 @@ static void sb_serialize(nano_buf *buf, const SEXP object) { &output_stream, (R_pstream_data_t) buf, R_pstream_xdr_format, - SB_SERIAL_VER, + SB_R_SERIAL_VER, NULL, sb_write_bytes, NULL, diff --git a/src/secret.c b/src/secret.c index c6b0be0..ea36ef1 100644 --- a/src/secret.c +++ b/src/secret.c @@ -231,7 +231,7 @@ static inline int sb_integer(SEXP x) { static void * (*const volatile secure_memset)(void *, int, size_t) = memset; #endif -inline void sb_clear_buffer(void *buf, size_t sz) { +inline void sb_clear_buffer(void *buf, const size_t sz) { #ifdef MBEDTLS_CT_ASM memset(buf, 0, sz); asm volatile ("" ::: "memory"); @@ -309,7 +309,7 @@ static void hash_object(mbedtls_sha3_context *ctx, const SEXP x) { } -SEXP sb_hash_sexp(unsigned char *buf, size_t sz, int conv) { +SEXP sb_hash_sexp(unsigned char *buf, const size_t sz, const int conv) { SEXP out; if (conv == 0) { diff --git a/src/secret.h b/src/secret.h index 711e78d..8902f95 100644 --- a/src/secret.h +++ b/src/secret.h @@ -30,33 +30,12 @@ #include #include -#ifndef ANY_ATTRIB -#define ANY_ATTRIB(x) (ATTRIB(x) != R_NilValue) -#endif - -#define SB_DATAPTR(x) (void *) DATAPTR_RO(x) -#define SB_STRING(x) CHAR(*((const SEXP *) DATAPTR_RO(x))) -#define SB_LOGICAL(x) *(int *) DATAPTR_RO(x) -#define SB_ASSERT_LOGICAL(x) if (TYPEOF(x) != LGLSXP) \ -Rf_error("'convert' must be a logical value") -#define SB_ASSERT_STR(x) if (TYPEOF(x) != STRSXP) \ -Rf_error("'file' must be a character string") - -#define SB_R_SERIAL_VER 3 -#define SB_SERIAL_HEADERS 6 -#define SB_BUF_SIZE 65536 - #ifdef WORDS_BIGENDIAN # define MBEDTLS_IS_BIG_ENDIAN 1 #else # define MBEDTLS_IS_BIG_ENDIAN 0 #endif -#define SB_SHA256_SIZE 32 -#define SB_SHA256_BLK 64 -#define SB_SIPH_SIZE 8 -#define SB_SKEY_SIZE 16 - #define MBEDTLS_ERR_BASE64_BUFFER_TOO_SMALL -1 #define MBEDTLS_ERR_BASE64_INVALID_CHARACTER -2 @@ -111,9 +90,26 @@ typedef struct nano_buf_s { size_t cur; } nano_buf; +#define SB_SHA256_SIZE 32 +#define SB_SHA256_BLK 64 +#define SB_SIPH_SIZE 8 +#define SB_SKEY_SIZE 16 +#define SB_R_SERIAL_VER 3 +#define SB_SERIAL_HEADERS 6 +#define SB_BUF_SIZE 65536 #define SB_INIT_BUFSIZE 4096 -#define SB_SERIAL_VER 3 #define SB_SERIAL_THR 134217728 + +#ifndef ANY_ATTRIB +#define ANY_ATTRIB(x) (ATTRIB(x) != R_NilValue) +#endif +#define SB_DATAPTR(x) (void *) DATAPTR_RO(x) +#define SB_STRING(x) CHAR(*((const SEXP *) DATAPTR_RO(x))) +#define SB_LOGICAL(x) *(int *) DATAPTR_RO(x) +#define SB_ASSERT_LOGICAL(x) if (TYPEOF(x) != LGLSXP) \ +Rf_error("'convert' must be a logical value") +#define SB_ASSERT_STR(x) if (TYPEOF(x) != STRSXP) \ +Rf_error("'file' must be a character string") #define NANO_ALLOC(x, sz) \ (x)->buf = R_Calloc(sz, unsigned char); \ (x)->len = sz; \ @@ -130,8 +126,8 @@ Rf_error("serialization exceeds max length of raw vector") #define ERROR_FOPEN(x) Rf_error("file not found or no read permission at '%s'", x) #define ERROR_FREAD(x) Rf_error("file read error at '%s'", x) -void sb_clear_buffer(void *, size_t); -SEXP sb_hash_sexp(unsigned char *, size_t, int); +void sb_clear_buffer(void *, const size_t); +SEXP sb_hash_sexp(unsigned char *, const size_t, const int); SEXP secretbase_base64enc(SEXP, SEXP); SEXP secretbase_base64dec(SEXP, SEXP);