+
+ 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];
+ }