Comparing bits. Am I doing it right?

This code is supposed to count the number of bit that are different in 2 SHA256 hashes. I think it works but I don’t know how many bits should be different so I don’t know how to test it. If ppl could let me know if it is working, or how to test it myself, or if there is a better approach I would appreciate it.

// Exc4.1 counts the number of bits that are different in 2 SHA256 hashes.
package main

import (
	"crypto/sha256"
	"fmt"
)

func main() {
	c1 := sha256.Sum256([]byte("x"))
	c2 := sha256.Sum256([]byte("X"))
	i := s256dif(&c1, &c2)
	fmt.Printf("There are %d bits that are different between c1 and c2.\n",
		i)
}

func s256dif(c1, c2 *[32]byte) int {
	var t int
	for i := range c1 {
		for n := uint64(0); n < 8; n++ {
			if c1[i]>>n&1 != c2[i]>>n&1 {
				t++
			}
		}
	}
	return t
}
'''

At a glance it looks like it should work, but not be very efficient. It’s an interesting problem that has efficient solutions, though.

Parts you can use to find a better solution is to recognize that the problem of counting the number of bits that are set in a given word is called the “Hamming weight”, and that XORing two words will leave you with only the bits that differ.

2 Likes

maybe this will help too,

1 Like

Is this better ?

// Exc4.1 counts the number of bits that are different in 2 SHA256 hashes.
package main

import (
	"crypto/sha256"
	"fmt"
	"math/bits"
)

func main() {
	c1 := sha256.Sum256([]byte("x"))
	c2 := sha256.Sum256([]byte("X"))
	i := s256dif(&c1, &c2)
	fmt.Printf("There are %d bits that are different between c1 and c2.\n",
		i)
}

func s256dif(c1, c2 *[32]byte) int {
	var t int
	for i := range c1 {
		t += bits.OnesCount8(c1[i] ^ c2[i])
	}
	return t
}

It seems to come out with the same numbers.

For production yes, but depending on the purpose of the exercise you may have cheated by using a library. :wink:

1 Like

@calmh @luk4z7 Thanks for the help.

1 Like