hash_u5.c (1079B)
1 #include "config.h" 2 #include "endian.h" 3 #include "hash_u5.h" 4 #include <string.h> 5 6 void hash_u5_init(struct hash_u5 *hu5, const char *hrp) 7 { 8 hu5->buf = 0; 9 hu5->num_bits = 0; 10 sha256_init(&hu5->hash); 11 sha256_update(&hu5->hash, hrp, strlen(hrp)); 12 } 13 14 void hash_u5(struct hash_u5 *hu5, const u8 *u5, size_t len) 15 { 16 size_t bits = len * 5; 17 18 while (bits) { 19 size_t n = 5; 20 21 if (bits < n) 22 n = bits; 23 24 hu5->buf <<= n; 25 hu5->buf |= (*u5 >> (5-n)); 26 bits -= n; 27 hu5->num_bits += n; 28 29 if (n == 5) 30 u5++; 31 32 if (hu5->num_bits >= 32) { 33 be32 be32 = cpu_to_be32((u32)(hu5->buf >> (hu5->num_bits-32))); 34 sha256_update(&hu5->hash, &be32, sizeof(be32)); 35 hu5->num_bits -= 32; 36 } 37 } 38 } 39 40 void hash_u5_done(struct hash_u5 *hu5, struct sha256 *res) 41 { 42 if (hu5->num_bits) { 43 be32 be32 = cpu_to_be32((u32)(hu5->buf << (32 - hu5->num_bits))); 44 45 sha256_update(&hu5->hash, &be32, (hu5->num_bits + 7) / 8); 46 } 47 sha256_done(&hu5->hash, res); 48 }