this post was submitted on 05 Oct 2026
88 points (92.3% liked)

Programming

28764 readers
333 users here now

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person's post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you're posting long videos try to add in some form of tldr for those who don't want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



founded 3 years ago
MODERATORS
you are viewing a single comment's thread
view the rest of the comments
[–] calcopiritus@lemmy.world 55 points 4 days ago (14 children)

I tried to implement my own git from scratch. And when I got to choose a hashing algorithm, I reached the exact same conclusion as the author pretty fast.

The hashing algorithm doesn't really matter as long as it's good enough to prevent accidental collisions. And if there is a collision, it is extremely easy to check.

If you are about to create a new object and an object with that hash already exists, you compare the 2 objects. If they are the same, nothing happened. If they are different, you just found a collision. Throw some obscure error that will only be witnessed once in the lifetime of the universe and be done with it. Tell the user to change a single bit of the input and be done with it.

The purpose of object hashes in git was never to provide security. Security is achieved through other means.

[–] Kajika@lemmy.ml 6 points 3 days ago* (last edited 3 days ago) (6 children)

Security is achieved through other means.

Let's explicit this: you can, and should, sign your commits if you want security (no commit tampering). You need to:

  • generated a gpg key: gpg --full-generate-key
  • set git to look for the public key to use: git config --global user.signingkey [public key ID]
  • set git to sign your commits: git config --global commit.gpgsign true

Note that I used --global here but you can do without to sign on a per-project basis.

Also gpg UX is terrible, to find the [public key ID] use the command gpg --list-keys, it should looks like:

pub   ed25519 2026-01-01 [SC] <- type algorithm date and capability (this key is only to Sign and Certify)
      662E3CDD6FE329002D0CA5BB40339DD82B12EF16 <- Public key ID
uid           [ultimate] my full name (Master Key) <my_email@domain.com> <- owner information (should be your name and email address)
sub   rsa4096 2026-01-01 [E] <- sub key used to encrypt
sub   ed25519 2026-01-01 [S] [expires: 2027-01-01] <- subkey used to sign

Last, if you want to save/backup you keys the default location of gpg files is ~/gnupg or you can export keys with gpg --export [key ID] > path/to/file.key for the public part and gpg --export-secret-keys [private key ID] for the private part. Both export can have a -a or --armor argument to output as base64 text instead of raw binary. [private key ID] can be found with gpg --list-secret-keys.

EDIT: after writing this I checked https://git-scm.com/book/en/v2/Git-Tools-Signing-Your-Work and TIL you can sign tags too.

EDIT 2: you can also use your ssh key to sign -> git config --global gpg.format ssh (I am less inclined to do this as you won't have subkeys and expiration date but this would be simplier indeed)

[–] dunj3@feddit.org 4 points 3 days ago (1 children)

Worth to keep in mind though that the gpg signature basically only signs the (git) hashes of the contained objects, so the choice of hash function is indeed important for security.

[–] Kajika@lemmy.ml 1 points 2 days ago* (last edited 2 days ago) (1 children)

~~Indeed it is still relevant~~. I am wondering what does it sign for tags.

EDIT: Are you sure about that? signing the hash felt logical but digging about it this seems a false assumption. git is signing the whole commit object.

[–] dunj3@feddit.org 3 points 2 days ago (1 children)

Yes, but the "commit object" is just a bunch of metadata that refers to the tree by its hash:

% git cat-file commit 6de20f6092
tree 5cb21f9acf8c31ce1a8f6bb0bbc3b9e4e8607ce8
parent c679abf7d2c0f3f4ee623e0882a50964e9b100cc
author Junio C Hamano <gitster@pobox.com> 1791306800 -0700
committer Junio C Hamano <gitster@pobox.com> 1791306812 -0700

The 4th batch

Signed-off-by: Junio C Hamano <gitster@pobox.com>

And the tree in turn refers to the files (or subfolders) by their hashes:

% git cat-file -p 5cb21f9acf8c31ce1a8f6bb0bbc3b9e4e8607ce8 | head
100644 blob fd4fb56b6d56789369d4824ad10999369127f5c7	.b4-config
100644 blob 8168d8a10b3a9e14f6c019e8ffbe5a71dc307d8b	.b4-cover-template
100644 blob fef04a38402fee6465a6a4225374d493b47421c0	.cirrus.yml
100644 blob 86b4fe33e5cd98e2a347944559c345419824c245	.clang-format
100644 blob 82e121a41754b536611c9ce9b2b4aba349e7ed9d	.editorconfig
100644 blob 26490ad60a74d0968eaf2f77abffcb21d78b18e3	.gitattributes
040000 tree 5f898fc5ec3429058fd21072db170fff9e49eab4	.github
100644 blob 0209bd16f209c232748734d911099b37be80fc12	.gitignore
100644 blob 3f2483550038f6aabb2f8b858b4ee7b29a66072a	.gitlab-ci.yml
100644 blob cbeebdab7a5e2c6afec338c3534930f569c90f63	.gitmodules

If you can produce a hash collision, you can therefore have two repositories with the same signed commit but different file content.

[–] Kajika@lemmy.ml 1 points 2 days ago (1 children)

Yes you are right, damned! Why git isn't signing the diff (patch)? This is so wrong on so many level, signing already hash the content, we can sign multiple GiB without any issue.

[–] dunj3@feddit.org 2 points 2 days ago

Hm, I wouldn't call it wrong on many levels, I think it's quite elegant. It's a form of a Merkle Tree, and knowing the hash of a commit does not just allow you to verify its content, but also the whole history up to that point (as the commit contains the hash of its parent, which in turn contains hashes of its content and parents). I guess that's not too unimportant if you consider scenarios where you pull code from distributed repositories (like forks), as you can ensure that the parts of the history you know are actually what they claim to be. And if you accept hash-then-sign as being secure, this is just as secure for signed commits (assuming the hash function is secure).

I think the only unfortunate part here is that git ended up using SHA-1 as a hash function, which 20 years ago might or might not have been a reasonable choice -- I don't know how "broken" it was regarded back then, or how widespread SHA-256 use was.

load more comments (4 replies)
load more comments (11 replies)