An object's identity in Git is the SHA-1 hash of its serialized bytes, written as 40 lowercase hex characters. This is the content-addressable idea at the heart of Git, and today your ids start matching real Git exactly.
Compute an object id as the SHA-1 of its loose object bytes.
This is the moment the whole design clicks: an object’s id is its content, run through SHA-1. Hash the serialized bytes (header, NUL, and content together) and render the 20-byte digest as 40 lowercase hex characters. Two files with identical content get the same id automatically, because the id is a pure function of the bytes. That is what “content-addressable” means.
The values are not ours to choose; they are whatever SHA-1 produces, and they are
identical to real Git because we hash the exact same wrapped bytes Git does. The
empty blob e69de29b... is a constant you will see in real repositories, and
hello\n always hashes to ce013625.... We lean on the standard library for
SHA-1 here - earlier projects built hashing from scratch; this one is about the
object model, not the hash function.
// hash the serialized bytes, not the raw contentfunc HashObject(typ string, content []byte) string {sum := sha1.Sum(Serialize(typ, content))return hex.EncodeToString(sum[:])}
You can confirm any of these ids with real Git, for example printf 'hello\n' | git hash-object --stdin