base32: Add encoding of hashes, print base32-encoded hashes

laumann
Sep 16, 2022, 8:40 PM
DKX3RO3LHLWUVHRUI23CN4A4IP3EPIUIVB6I5PPHMBU4PN2GOM2QC

Dependencies

  • [2] FD4A7HCM base32: describe known limitations
  • [3] YG4DZB3A add representation of hash, decode dependencies
  • [*] VKLGQREY change: add base32 decode, initial deconstruction of hashed

Change contents

  • edit in hash.c at line 4
    [3.420]
    [3.420]
    #include <string.h>
  • edit in hash.c at line 7
    [3.439]
    [3.439]
    #include "base32.h"
  • edit in hash.c at line 13
    [3.485]
    [3.485]
    char b32hash[53];
    memset(b32hash, 0, 53);
  • replacement in hash.c at line 21
    [3.578][3.578:666]()
    for (i = 0; i < BLAKE3_BYTES; i++)
    printf("%02x", hash->bytes[i]);
    printf("\n");
    [3.578]
    [3.666]
    base32_encode_hash(b32hash, hash->bytes);
    printf("%s\n", b32hash);
    /* for (i = 0; i < BLAKE3_BYTES; i++) */
    /* printf("%02x", hash->bytes[i]); */
    /* printf("\n"); */
  • edit in base32.h at line 11
    [5.1886]
    [5.1886]
    void base32_encode_hash(char *dst, uint8_t const *in);
  • edit in base32.c at line 2
    [5.1949]
    [5.1949]
    /**
    * Base32 encoding/decoding - for now only supports presenting blake3 hashes (32
    * bytes).
    */
  • edit in base32.c at line 42
    [2.249]
    [2.249]
    * (4) does not validate the 33rd byte to be a "1" meaning blake3.
  • edit in base32.c at line 71
    [5.3724]
    static const char BASE32_ENC[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567=";
    /**
    * Base32 encoding for hashes. This routine only handles (and assumes) inputs of
    * length 32. Additionally, it doesn't do any padding.
    *
    * The output should large enough to hold 53 character. One interesting feature
    * of Pijul's use of base32 is that the hash type is appended to the output as
    * an extra byte where 1 = BLAKE3. This is the reason that all blake3-hashed
    * change files end with "C".
    */
    void
    base32_encode_hash(char *dst, uint8_t const *hash)
    {
    uint8_t b[5];
    int i, j;
    for (i = 0; i < 6; i++) {
    for (j = 0; j < 5; j++)
    b[j] = hash[i * 5 + j];
    *dst++ = BASE32_ENC[b[0] >> 3];
    *dst++ = BASE32_ENC[(b[0] & 0x7) << 2 | (b[1] & 0xc0) >> 6];
    *dst++ = BASE32_ENC[(b[1] & 0x3e) >> 1];
    *dst++ = BASE32_ENC[(b[1] & 0x1) << 4 | (b[2] & 0xf0) >> 4];
    *dst++ = BASE32_ENC[(b[2] & 0x0f) << 1 | (b[3] & 0x80) >> 7];
    *dst++ = BASE32_ENC[(b[3] & 0x7c) >> 2];
    *dst++ = BASE32_ENC[(b[3] & 0x3) << 3 | (b[4] & 0xe0) >> 5];
    *dst++ = BASE32_ENC[b[4] & 0x1f];
    }
    b[0] = hash[30];
    b[1] = hash[31];
    b[2] = 1; /* Blake3 */
    b[3] = b[4] = 0;
    *dst++ = BASE32_ENC[b[0] >> 3];
    *dst++ = BASE32_ENC[(b[0] & 0x7) << 2 | (b[1] & 0xc0) >> 6];
    *dst++ = BASE32_ENC[(b[1] & 0x3e) >> 1];
    *dst++ = BASE32_ENC[(b[1] & 0x1) << 4 | (b[2] & 0xf0) >> 4];
    *dst++ = BASE32_ENC[(b[2] & 0x0f) << 1 | (b[3] & 0x80) >> 7];
    }