Skip to content

Commit a384421

Browse files
committed
Feat #157: Support --blake3 option for Blake3 algorithm
1 parent 1df9e4d commit a384421

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

hash_check.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,6 @@ static unsigned bsd_hash_name_to_id(const char* name, unsigned length, enum Hash
303303
#define code2mask_size (19 * 2)
304304
static unsigned code2mask[code2mask_size] = {
305305
FOURC2U('A', 'I', 'C', 'H'), RHASH_AICH,
306-
FOURC2U('B', 'L', 'A', 'K'), (RHASH_BLAKE2S | RHASH_BLAKE2B),
307306
FOURC2U('B', 'T', 'I', 'H'), RHASH_BTIH,
308307
FOURC2U('C', 'R', 'C', '3'), (RHASH_CRC32 | RHASH_CRC32C),
309308
FOURC2U('E', 'D', '2', 'K'), RHASH_ED2K,
@@ -332,9 +331,15 @@ static unsigned bsd_hash_name_to_id(const char* name, unsigned length, enum Hash
332331
/* quick fix to detect "RMD160" as RIPEMD160 */
333332
if (code == FOURC2U('R', 'M', 'D', '1'))
334333
return (length == 6 && name[4] == '6' && name[5] == '0' ? RHASH_RIPEMD160 : 0);
335-
for (i = 0; code2mask[i] != code; i += 2)
336-
if (i >= (code2mask_size - 2)) return 0;
337-
hash_mask = code2mask[i + 1];
334+
if (code == FOURC2U('B', 'L', 'A', 'K')) {
335+
if (length == 6 && name[4] == 'E' && name[5] == '3')
336+
return RHASH_BLAKE3;
337+
hash_mask = RHASH_BLAKE2S | RHASH_BLAKE2B;
338+
} else {
339+
for (i = 0; code2mask[i] != code; i += 2)
340+
if (i >= (code2mask_size - 2)) return 0;
341+
hash_mask = code2mask[i + 1];
342+
}
338343
i = get_ctz(hash_mask);
339344
if (length <= 4)
340345
{

parse_cmdline.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ static void print_help(void)
8181
print_help_line(" -H, --sha1 ", digest_format, "SHA1");
8282
print_help_line(" --sha224, --sha256, --sha384, --sha512 ", digest_format, "SHA2");
8383
print_help_line(" --sha3-224, --sha3-256, --sha3-384, --sha3-512 ", digest_format, "SHA3");
84+
print_help_line(" --blake2s, --blake2b ", digest_format, "BLAKE2S/BLAKE2B");
85+
print_help_line(" --blake3 ", digest_format, "BLAKE3");
8486
print_help_line(" -T, --tth ", digest_format, "TTH");
8587
print_help_line(" --btih ", digest_format, "BitTorrent InfoHash");
8688
print_help_line(" -A, --aich ", digest_format, "AICH");
@@ -95,7 +97,6 @@ static void print_help(void)
9597
print_help_line(" --gost94-cryptopro ", digest_format, _("GOST R 34.11-94 CryptoPro"));
9698
print_help_line(" --ripemd160 ", digest_format, "RIPEMD-160");
9799
print_help_line(" --has160 ", digest_format, "HAS-160");
98-
print_help_line(" --blake2s, --blake2b ", digest_format, "BLAKE2S/BLAKE2B");
99100
print_help_line(" --edonr256, --edonr512 ", digest_format, "EDON-R 256/512");
100101
print_help_line(" --snefru128, --snefru256 ", digest_format, "SNEFRU-128/256");
101102
print_help_line(" -a, --all ", _("Calculate all supported hash functions.\n"));
@@ -169,7 +170,10 @@ static void list_hashes(void)
169170
*/
170171
static void add_hash_id(options_t* o, unsigned hash_id)
171172
{
172-
o->hash_mask |= hash_id_to_bit64(hash_id);
173+
if (hash_id == RHASH_ALL_HASHES)
174+
o->hash_mask = get_all_supported_hash_mask();
175+
else
176+
o->hash_mask |= hash_id_to_bit64(hash_id);
173177
}
174178

175179
/**
@@ -451,6 +455,7 @@ cmdline_opt_t cmdline_opt[] =
451455
{ F_VFNC, 0, 0, "edonr512", (opt_handler_t)add_hash_id, 0, RHASH_EDONR512 },
452456
{ F_VFNC, 0, 0, "blake2s", (opt_handler_t)add_hash_id, 0, RHASH_BLAKE2S },
453457
{ F_VFNC, 0, 0, "blake2b", (opt_handler_t)add_hash_id, 0, RHASH_BLAKE2B },
458+
{ F_VFNC, 0, 0, "blake3", (opt_handler_t)add_hash_id, 0, RHASH_BLAKE3 },
454459

455460
/* output formats */
456461
{ F_UFLG, 0, 0, "sfv", 0, &opt.fmt, FMT_SFV },
@@ -1114,6 +1119,7 @@ static void set_default_hash_mask(const char* progName)
11141119
if (strstr(buf, "edonr512")) add_hash_id(&opt, RHASH_EDONR512);
11151120
if (strstr(buf, "blake2s")) add_hash_id(&opt, RHASH_BLAKE2S);
11161121
if (strstr(buf, "blake2b")) add_hash_id(&opt, RHASH_BLAKE2B);
1122+
if (strstr(buf, "blake3")) add_hash_id(&opt, RHASH_BLAKE3);
11171123
if (strstr(buf, "snefru256")) add_hash_id(&opt, RHASH_SNEFRU128);
11181124
if (strstr(buf, "snefru128")) add_hash_id(&opt, RHASH_SNEFRU256);
11191125
else if (strstr(buf, "ed2k")) add_hash_id(&opt, RHASH_ED2K);

0 commit comments

Comments
 (0)