Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby SHA-1A字符串(git哈希对象字符串)_Ruby_Git_Sha1_Sha_Digest - Fatal编程技术网

Ruby SHA-1A字符串(git哈希对象字符串)

Ruby SHA-1A字符串(git哈希对象字符串),ruby,git,sha1,sha,digest,Ruby,Git,Sha1,Sha,Digest,我正在尝试加密SHA-1以下字符串: commit 218\0tree 64a7513fad0b86d34b6feedbf9c2e99135819861 parent 233634213baf3f40236233f28c6646f20786a80a author CTF user <me@example.com> 1390678027 +0000 committer CTF user <me@example.com> 1390678027 +0000 Give me a

我正在尝试加密SHA-1以下字符串:

commit 218\0tree 64a7513fad0b86d34b6feedbf9c2e99135819861 parent 233634213baf3f40236233f28c6646f20786a80a author CTF user <me@example.com> 1390678027 +0000 committer CTF user <me@example.com> 1390678027 +0000 Give me a Gitcoin 200
commit 218\0tree 64a7513fad0b86d34b6feedbf9c2e99135819861父项233634213baf3f40236233f28c6646f20786a80a作者CTF用户1390678027+0000提交者CTF用户1390678027+0000给我一个Gitcoin 200
它是一个git
hash对象
input,头部与提交消息一起。我得到的结果在不同的方法中是非常不同的

使用git散列对象,我得到了bed6b1001619ad84548d05db65a75ac80bf79f31

使用Ruby的
摘要/sha1
,我得到了
e729e36abf0fa4da392b8f2acc1561ec5d298af9

使用,我得到了
d543ddbfb7607464f5f964b9a3536eccedd1e1a4

这是非常令人困惑的。它是哪一个?为什么我不能模拟git hash对象的功能?我阅读并遵循了那里的说明,但似乎无法获得正确的哈希摘要

以下是我的ruby代码片段:

Digest::SHA1.hexdigest“提交#{body.length}\0#{body}”


在git中是否有方法查看哈希对象头,或者它用来哈希SHA-1的字符串?我上面的方法有什么问题吗?

你好,请捕获旗手:)

这就是你要做的(确保有新行!)

需要“摘要/sha1”
content=“tree#{tree}
父#{parent}
作者CTF用户#{timestamp}+0000
提交者CTF用户#{timestamp}+0000
给我一枚金币
1.
"
摘要::SHA1.hexdigest“提交#{content.length}\0#{content}”

您试图解决的问题并不完全清楚。您可能想看看这个问题的答案-@D.Shawley我正在尝试手动获取git hash对象SHA-1值,而不是使用git hash对象。我看了那个问题,我正是这么做的。你从哪里得到以
树64a751…
开头的字符串的?如果要与实际的git提交进行比较,请注意,提交数据包含嵌入的换行符,而不是全部在一行中。@torek From git write tree<代码>树在一行上,
在另一行上,
作者
在另一行上,
提交者
在另一行上。然后是两行新行,然后是提交消息。有了这个,我得到了一个SHA-1的
56e4a594dc02c21dfa82c237e3b8c53ed9c8f4c5
。仍然不是
bed6b..
。知道我还遗漏了什么吗?啊,我的意思更像是,你有没有
git cat file-p
一个ID已知的实际提交。如果我把你的树、父对象等放在一个文件中,我会得到一个至少220字节长的文件,包括所有的换行符,它比所指示的218长,所以我很确定这里有一些数据不同步。换行符是问题:)我在bash中犯了同样的错误。我明白了。谢谢
require 'digest/sha1'

content = "tree #{tree}
parent #{parent}
author CTF user <me@example.com> #{timestamp} +0000
committer CTF user <me@example.com> #{timestamp} +0000

Give me a Gitcoin

1
"

Digest::SHA1.hexdigest "commit #{content.length}\0#{content}"