Skip to content

Commit

Permalink
secretbase 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Sep 9, 2024
1 parent 7ceb23e commit 10e0c66
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 58 deletions.
12 changes: 6 additions & 6 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 6 additions & 6 deletions R/secret.R
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down
8 changes: 4 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand All @@ -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

Expand Down Expand Up @@ -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 = "秘密の基地の中")
```
Expand Down
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 = "秘密の基地の中")
Expand Down
12 changes: 6 additions & 6 deletions man/secretbase-package.Rd

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

2 changes: 1 addition & 1 deletion src/base.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 20 additions & 24 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,33 +30,12 @@
#include <Rinternals.h>
#include <R_ext/Visibility.h>

#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

Expand Down Expand Up @@ -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; \
Expand All @@ -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);
Expand Down

0 comments on commit 10e0c66

Please sign in to comment.