I’m trying to setup a VM testing how does Syncthing handles many devices added via the API. I tried initially to use a certain device hash multiple times, and replace the last character with 1
, 2
etc, as much as needed. This did not work, and only a single device with the original ID was added successfully. My suspicion is that this is due to a certain safety/check bit / digit, but I’m not sure.
I also read this paragraph from here:
The hashing results in a 256 bit hash which we encode using base32. Base32 encodes five bits per character so we need 256 / 5 = 51.2 characters to encode the device ID. This becomes 52 characters in practice, but 52 characters of base32 would decode to 260 bits which is not a whole number of bytes. The base32 encoding adds padding to 280 bits (the next multiple of both 5 and 8 bits) so the resulting ID looks something like:
MFZWI3DBONSGYYLTMRWGC43ENRQXGZDMMFZWI3DBONSGYYLTMRWA====
The padding (
====
) is stripped away, the device ID split into four groups, and check digits are added for each group. For presentation purposes the device ID is grouped with dashes, resulting in the final value:MFZWI3D-BONSGYC-YLTMRWG-C43ENR5-QXGZDMM-FZWI3DP-BONSGYY-LTMRWAD
I don’t understand the base32 math… I tried to use this online calculator with the output of the command echo bla | sha256sum
, and I got a weird result:
734OD6S3BPOCKK8MMDAG7R5C3HGUGDPTG1E1ISH0A020K3LHJV
And I didn’t experience what the docs mentions about the padding, and the non-integer number of bits. My simple mathematical sense also tells me that the number of bits I got for base32 sha256 hash, also makes sense, because:
$ echo bla | sha256sum | cut -d' ' -f1 | wc -c
65
While the number of characters in the result I got from the website is 51
. This satisfies the simple calculation of 65*4/5 = 51
, where 4 is the number of characters required for base16, and 5 is the number of bits in a base32 encoding.
Can anyone find the mistake in my math?