I have a table in my DB where I store SHA256 hashes in a BINARY(32) column. I\'m looking for a way to compute the Hamming distance of the entries in the column to a supplied
Interesting question, I've found a way to do this for a binary(3) that might work as well for a binary(32):
drop table if exists BinaryTest;
create table BinaryTest (hash binary(3));
insert BinaryTest values (0xAAAAAA);
set @supplied = cast(0x888888 as binary);
select length(replace(concat(
bin(ascii(substr(hash,1,1)) ^ ascii(substr(@supplied,1,1))),
bin(ascii(substr(hash,2,1)) ^ ascii(substr(@supplied,2,1))),
bin(ascii(substr(hash,3,1)) ^ ascii(substr(@supplied,3,1)))
),'0',''))
from BinaryTest;
The replace removes any all zeroes, and the length of remainder is the number of ones. (The conversion to binary omits leading zeroes, so counting the zeroes would not work.)
This prints 6, which matches the number of ones in
0xAAAAAA ^ 0x888888 = 0x222222 = 0b1000100010001000100010