Skip to content

Commit

Permalink
more accurate error message
Browse files Browse the repository at this point in the history
  • Loading branch information
shikokuchuo committed Jan 24, 2024
1 parent 9d08d8b commit ae5eea0
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/secret.c
Original file line number Diff line number Diff line change
Expand Up @@ -347,21 +347,21 @@ SEXP secretbase_sha3_file(SEXP x, SEXP bits, SEXP convert) {
const int size = validate_bitlength(bits);
const size_t outlen = (size_t) (size / 8);
unsigned char output[outlen];
unsigned char buf[4096];
unsigned char buf[SB_BUF_SIZE];
size_t cur;

mbedtls_sha3_id id = id_from_size(size);
mbedtls_sha3_context ctx;
mbedtls_sha3_init(&ctx);
mbedtls_sha3_starts(&ctx, id);

FILE *f = fopen(file, "rb");
if (f == NULL)
Rf_error("file not found");
while ((cur = fread(buf, 1, sizeof(buf), f))) {
FILE *fp = fopen(file, "rb");
if (fp == NULL)
Rf_error("file not found or accessible");
while ((cur = fread(buf, 1, sizeof(buf), fp))) {
mbedtls_sha3_update(&ctx, buf, cur);
}
fclose(f);
fclose(fp);

mbedtls_sha3_finish(&ctx, output, outlen);

Expand Down
1 change: 1 addition & 0 deletions src/secret.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

#define SB_R_SERIAL_VER 3
#define SB_SERIAL_HEADERS 6
#define SB_BUF_SIZE 4096

typedef enum {
MBEDTLS_SHA3_NONE = 0,
Expand Down
8 changes: 4 additions & 4 deletions tests/tests.R
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ test_error(sha3("secret base", bits = 0), "'bits' must be between 8 and 2^24")
test_error(sha3("secret base", bits = -1), "'bits' must be between 8 and 2^24")
test_error(sha3("secret base", bits = 2^24 + 1), "'bits' must be between 8 and 2^24")
# File interface tests:
hash_func <- function(file = tempfile()) {
cat("secret base", file = file)
hash_func <- function(file, string) {
on.exit(unlink(file))
cat(string, file = file)
sha3sum(file)
}
test_equal(hash_func(), "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520")
test_error(sha3sum(""), "file not found")
test_equal(hash_func(tempfile(), "secret base"), "a721d57570e7ce366adee2fccbe9770723c6e3622549c31c7cab9dbb4a795520")
test_error(hash_func("", ""), "file not found or accessible")

0 comments on commit ae5eea0

Please sign in to comment.