hare

The Hare programming language
git clone https://git.torresjrjr.com/hare.git
Log | Files | Refs | README | LICENSE

commit bf9678fa3d7e30201ce1ced4a1cc0507cb72f1a8
parent 1ab4dc3f284daf20f890fc10e5ce06045b7bfa84
Author: Thomas Bracht Laumann Jespersen <t@laumann.xyz>
Date:   Thu,  4 Nov 2021 14:57:32 +0100

crypto: testing: use encoding::hex::encodestr()

Replace the manual construction of the hash strings as hex with single
call to hex::encodestr().

As an added bonus, fix the commented out 1gb tests for sha1 and sha256.

Signed-off-by: Thomas Bracht Laumann Jespersen <t@laumann.xyz>

Diffstat:
Mcrypto/md5/+test.ha | 14++++----------
Mcrypto/sha1/+test.ha | 28+++++++++-------------------
Mcrypto/sha256/+test.ha | 31+++++++++----------------------
Mcrypto/sha512/+test.ha | 54+++++++++++++-----------------------------------------
Mscripts/gen-stdlib | 8++++----
Mstdlib.mk | 8++++----
6 files changed, 43 insertions(+), 100 deletions(-)

diff --git a/crypto/md5/+test.ha b/crypto/md5/+test.ha @@ -1,8 +1,7 @@ use fmt; use hash; use strings; -use strio; -use io; +use encoding::hex; @test fn md5() void = { let md5 = md5(); @@ -26,14 +25,9 @@ use io; let sum: [SIZE]u8 = [0...]; hash::sum(&md5, sum); - let hex = strio::dynamic(); - defer io::close(hex); - for (let j = 0z; j < SIZE; j += 1) { - fmt::fprintf(hex, "{:02x}", sum[j])!; - }; - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let sumhex = hex::encodestr(sum); + if (sumhex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, sumhex, vector.1)!; abort(); }; }; diff --git a/crypto/sha1/+test.ha b/crypto/sha1/+test.ha @@ -1,8 +1,7 @@ use fmt; use hash; use strings; -use strio; -use io; +use encoding::hex; @test fn sha1() void = { let sha = sha1(); @@ -30,14 +29,9 @@ use io; let sum: [SIZE]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - for (let j = 0z; j < SIZE; j += 1) { - fmt::fprintf(hex, "{:02x}", sum[j])!; - }; - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; @@ -54,17 +48,13 @@ use io; // for (let i = 0z; i < 16777216; i += 1) // hash::write(&sha, strings::toutf8(input)); // -// let sum = hash::sum(&sha); -// defer free(sum); +// let sum: [SIZE]u8 = [0...]; +// hash::sum(&sha, sum); // -// let hex = strio::dynamic(); -// defer io::close(hex); -// for (let i = 0z; i < SIZE; i += 1) -// fmt::fprintf(hex, "{:02x}", sum[i]); +// let shahex = hex::encodestr(sum); // -// if (strio::string(hex) != expected) { -// fmt::errorfln("1GB vector: {} != {}", -// strio::string(hex), expected); +// if (shahex != expected) { +// fmt::errorfln("1GB vector: {} != {}", shahex, expected)!; // abort(); // }; //}; diff --git a/crypto/sha256/+test.ha b/crypto/sha256/+test.ha @@ -1,8 +1,7 @@ use fmt; use hash; -use io; use strings; -use strio; +use encoding::hex; @test fn sha256() void = { let sha = sha256(); @@ -27,15 +26,9 @@ use strio; let sum: [SIZE]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - for (let i = 0z; i < SIZE; i += 1) { - fmt::fprintf(hex, "{:02x}", sum[i])!; - }; - - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; @@ -48,18 +41,12 @@ use strio; //for (let i = 0z; i < 16777216; i += 1) { // hash::write(&sha, strings::toutf8(input)); //}; - //let sum = hash::sum(&sha); - //defer free(sum); - - //let hex = strio::dynamic(); - //defer io::close(hex); - //for (let i = 0z; i < SIZE; i += 1) { - // fmt::fprintf(hex, "{:02x}", sum[i]); - //}; + //let sum: [SIZE]u8 = [0...]; + //hash::sum(&sha, sum); - //if (strio::string(hex) != expected) { - // fmt::errorfln("Biggo vector: {} != {}", - // strio::string(hex), expected); + //let shahex = hex::encodestr(sum); + //if (shahex != expected) { + // fmt::errorfln("Biggo vector: {} != {}", shahex, expected)!; // abort(); //}; }; diff --git a/crypto/sha512/+test.ha b/crypto/sha512/+test.ha @@ -1,8 +1,7 @@ use fmt; use hash; use strings; -use strio; -use io; +use encoding::hex; @test fn sha512() void = { let sha = sha512(); @@ -26,16 +25,9 @@ use io; let sum: [SIZE]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - - for (let j = 0z; j < len(sum); j += 1) { - fmt::fprintf(hex, "{:02x}", sum[j])!; - }; - - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; @@ -57,15 +49,9 @@ use io; let sum: [SIZE224]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - - for (let j = 0z; j < len(sum); j += 1) - fmt::fprintf(hex, "{:02x}", sum[j])!; - - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; @@ -87,16 +73,9 @@ use io; let sum: [SIZE256]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - - for (let j = 0z; j < len(sum); j += 1) { - fmt::fprintf(hex, "{:02x}", sum[j])!; - }; - - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; @@ -119,16 +98,9 @@ use io; let sum: [SIZE384]u8 = [0...]; hash::sum(&sha, sum); - let hex = strio::dynamic(); - defer io::close(hex); - - for (let j = 0z; j < len(sum); j += 1) { - fmt::fprintf(hex, "{:02x}", sum[j])!; - }; - - if (strio::string(hex) != vector.1) { - fmt::errorfln("Vector {}: {} != {}", - i, strio::string(hex), vector.1)!; + let shahex = hex::encodestr(sum); + if (shahex != vector.1) { + fmt::errorfln("Vector {}: {} != {}", i, shahex, vector.1)!; abort(); }; }; diff --git a/scripts/gen-stdlib b/scripts/gen-stdlib @@ -242,7 +242,7 @@ crypto_sha256() { else gensrcs_crypto_sha256 \ +test.ha - genssa_crypto_sha256 fmt strio strings + genssa_crypto_sha256 hash fmt strings encoding::hex fi } @@ -253,7 +253,7 @@ crypto_sha1() { gen_ssa crypto::sha1 hash io endian else gen_srcs crypto::sha1 sha1.ha +test.ha - gen_ssa crypto::sha1 hash io endian fmt strio strings + gen_ssa crypto::sha1 hash endian fmt strings encoding::hex fi } @@ -264,7 +264,7 @@ crypto_sha512() { gen_ssa crypto::sha512 hash io endian else gen_srcs crypto::sha512 sha512.ha +test.ha - gen_ssa crypto::sha512 hash io endian fmt strio strings + gen_ssa crypto::sha512 hash endian fmt strings encoding::hex fi } @@ -275,7 +275,7 @@ crypto_md5() { gen_ssa crypto::md5 hash io endian else gen_srcs crypto::md5 md5.ha +test.ha - gen_ssa crypto::md5 hash io endian fmt strio strings + gen_ssa crypto::md5 hash endian fmt strings encoding::hex fi } diff --git a/stdlib.mk b/stdlib.mk @@ -2328,7 +2328,7 @@ testlib_crypto_md5_any_srcs= \ $(STDLIB)/crypto/md5/md5.ha \ $(STDLIB)/crypto/md5/+test.ha -$(TESTCACHE)/crypto/md5/crypto_md5-any.ssa: $(testlib_crypto_md5_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) +$(TESTCACHE)/crypto/md5/crypto_md5-any.ssa: $(testlib_crypto_md5_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_hex_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/crypto/md5 @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::md5 \ @@ -2339,7 +2339,7 @@ testlib_crypto_sha1_any_srcs= \ $(STDLIB)/crypto/sha1/sha1.ha \ $(STDLIB)/crypto/sha1/+test.ha -$(TESTCACHE)/crypto/sha1/crypto_sha1-any.ssa: $(testlib_crypto_sha1_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) +$(TESTCACHE)/crypto/sha1/crypto_sha1-any.ssa: $(testlib_crypto_sha1_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_hex_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/crypto/sha1 @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::sha1 \ @@ -2350,7 +2350,7 @@ testlib_crypto_sha256_any_srcs= \ $(STDLIB)/crypto/sha256/sha256.ha \ $(STDLIB)/crypto/sha256/+test.ha -$(TESTCACHE)/crypto/sha256/crypto_sha256-any.ssa: $(testlib_crypto_sha256_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) +$(TESTCACHE)/crypto/sha256/crypto_sha256-any.ssa: $(testlib_crypto_sha256_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_hash_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_hex_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/crypto/sha256 @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::sha256 \ @@ -2361,7 +2361,7 @@ testlib_crypto_sha512_any_srcs= \ $(STDLIB)/crypto/sha512/sha512.ha \ $(STDLIB)/crypto/sha512/+test.ha -$(TESTCACHE)/crypto/sha512/crypto_sha512-any.ssa: $(testlib_crypto_sha512_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_io_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strio_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) +$(TESTCACHE)/crypto/sha512/crypto_sha512-any.ssa: $(testlib_crypto_sha512_any_srcs) $(testlib_rt) $(testlib_hash_$(PLATFORM)) $(testlib_endian_$(PLATFORM)) $(testlib_fmt_$(PLATFORM)) $(testlib_strings_$(PLATFORM)) $(testlib_encoding_hex_$(PLATFORM)) @printf 'HAREC \t$@\n' @mkdir -p $(TESTCACHE)/crypto/sha512 @HARECACHE=$(TESTCACHE) $(HAREC) $(TESTHAREFLAGS) -o $@ -Ncrypto::sha512 \