Given you hash some string to produce a 256bit output, why is the binary representation not of length 256
package main
import (
"fmt"
"crypto/sha256"
)
func main() {
s := "1"
m := sha256.Sum256([]byte(s))
fmt.Println(len(m))
b := fmt.Sprintf("%b\n", m[:])
fmt.Println(len(b))
}
Output:
32
267
If you'd print it, you'd see it immediately:
Which outputs:
You're printing an array, and the
fmtpackage adds square brackets[]and spaces between the binary representations of the individual bytes. Plus you also add a newline character to it in the format string.To get only the bits, you may use a loop like this:
Note the
%08bformat string specifies the width to be 8 and use0as padding (if the byte value would be less than 8 bits).Which outputs:
Try these on the Go Playground.