4

How to do something like this:

  1. Create a with following content:

    $ cat testfile.txt
    This file will be committed with this id: 83b90a07620ef578450c40a6d38bacc42de7ad2d

  2. Commit testfile.txt
    $ git add testfile.txt
    $ git commit -m 'Thank you'

  3. Execute git log to verify predicted commit id:
    $ git log
    commit 83b90a07620ef578450c40a6d38bacc42de7ad2d
    Author: rohit01 <*@gmail.com>
    Date: Fri Feb 21 23:46:52 2014 +0530

Basically, predict the next git commit id and commit a file which contains that commit id.

Anshul Goyal
  • 73,278
  • 37
  • 149
  • 186
rohit01
  • 207
  • 2
  • 6
  • 2
    See also http://stackoverflow.com/q/3935717/34397 – SLaks Feb 21 '14 at 18:48
  • Related? http://stackoverflow.com/questions/3442874/in-git-how-can-i-write-the-current-commit-hash-to-a-file-in-the-same-commit – Felix Kling Feb 21 '14 at 18:51
  • 1
    I am curious what would be the use case for this. Why do you want to save the commit id when it is available from `git log`? – Anshul Goyal Feb 21 '14 at 18:57
  • Use case: I host my blog in github (github.com/rohit01/rohit01.github.io). I want to write a blog explaining how it works and share the github commit link of same blog. – rohit01 Feb 28 '14 at 17:03

2 Answers2

8

That is cryptographically impossible.

The git commit ID is an SHA1 hash of the contents of the commit.
Any change you make in the commit will also affect the ID.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 3
    If I'm not mistaken, it's also dependent on the SHA1 of the immediate parent (so the same commit in different branches) yields a different ID. http://stackoverflow.com/questions/5632520/ – Michael Stum Feb 21 '14 at 18:54
  • I don't think it's *absolutely* impossible. It's just supremely unlikely. ^_^ – TheBuzzSaw Feb 21 '14 at 18:57
  • Well, Moore's Law and some specialized FPGA will make that trivial to brute force. Eventually. – Michael Stum Feb 21 '14 at 18:57
  • 2
    @MichaelStum: Yes; and the author, timestamp, description, and I'm not sure what else. – SLaks Feb 21 '14 at 18:58
  • Google just announced a collision attack on sha1 - https://shattered.io/. So this is no longer cryptographically impossible. An unfortunate situation for stackoverflow as this answer was correct at the time of submission :) – user1976 Mar 02 '17 at 17:58
  • @user1976: No; I'm pretty sure a collision attack is not enough to do this. – SLaks Mar 02 '17 at 18:03
  • @SLaks - correct! I didn't pay enough attention to the actual question :( I still wonder what will happen if innovation catches up with answers like this one. Already on stack overflow I find myself having to read down to the non-accepted answers because tech has moved on and the accepted answer is no longer the best. In the case of an answer like this though it could actually become incorrect in the future! – user1976 Mar 03 '17 at 09:58
1

Are CPU cycles an issue? If you have infinite time this works:

This file will be committed with this id: 83b90a07620ef578450c40a6d38bacc42de7ad2d

Junk characters: VGhpcyBpcyBhbiBlYXN0ZXIgZWdnISBJIGNvdWxkbid0IHRoaW5rIG9mIGFueXRoaW5nIGdyZWF0IHRvIHB1dCBoZXJlLiBVc2luZyBiYXNlNjQgdG8gZ2VuZXJhdGUganVuay1sb29raW5nIGp1bmsgaXMga2luZCBvZiBzaWxseSwgYnV0IHRoaXMgc2hvdWxkIGJlIGVub3VnaCwgcmlnaHQ/ICBJZGsuIFVwdm90ZSBwbHo/

Then enumerate all possible junk characters until you get something like a fixed point under hashing. By pigeonhole principle since there can be many more junk characters and assuming hashes are "random" this will work eventually.

But this is impossible in any reasonable sense because you are trying to find a string S with the property that Hash(S) = 83b90a07620ef578450c40a6d38bacc42de7ad2d, which is by definition impossible for a secure hashing algorithm.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
  • `which is by definition impossible`; no; simply slow. http://en.wikipedia.org/wiki/Preimage_attack – SLaks Feb 21 '14 at 19:00
  • CPU cycles should not be an issue if it takes some predictable finite time. Did you ever tried this approach? – rohit01 Feb 28 '14 at 17:06
  • @rohit01 a "predictable, finite time" may be many orders of magnitude larger than the length of time the universe has existed. – djechlin Feb 28 '14 at 17:08
  • So I will be dead before I commit this file :p – rohit01 Feb 28 '14 at 17:34
  • 1
    @rohit01 barring major advancements in quantum engineering, I do not believe it is possible for you to commit a file with this property in your lifetime. – djechlin Feb 28 '14 at 18:59