Of course my main calculation is only one line:
$hamming_dist = unpack("%160b*", sha1($attempt_phrase) ^ $challenge_sha);
:)