我对 Golang 的 crypto/sha256 包有疑问。我将相同的输入发送到 sha256.Sum256 两次,每次输出都不同。
我的代码如下:
//Run the HMAC-SHA256 on the given kmac and message and return the generated MAC tag
func PCSSHA256(kmac []byte, message []byte) [32]byte {
//NOTE: bitwise XOR ^ only works on integers.
kmac64 := append(kmac, []byte(strings.Repeat("0", 48))[0:]...) //Pad to obtain a 64 byte unit
tohashinner := make([]byte, 64)
for i := 0; i < 64; i++ {
tohashinner[i] = kmac64[i] ^ 0x36
}
tohashouter := make([]byte, 64)
for i := 0; i < 64; i++ {
tohashouter[i] = kmac64[i] ^ 0x5c
}
tohashinnerwmess := append(tohashinner, message[0:]...)
firsthashval := sha256.Sum256(tohashinnerwmess)
fmt.Printf("tohashinner in || bounds:\n||%s||\n", tohashinner)
fmt.Printf("tohashouter in || bounds:\n||%s||\n", tohashouter)
fmt.Printf("tohashinnerwmess in || bounds:\n||%s||\n", tohashinnerwmess)
fmt.Printf("firsthashval after Hex Encode in || bounds:\n||%s||\n", hex.EncodeToString(firsthashval[:]))
tag := sha256.Sum256(append(tohashouter, firsthashval[0:]...))
fmt.Printf("Input message in || bounds:\n||%s||\n", message)
fmt.Printf("Input kmac in || bounds:\n||%s||\n", kmac)
fmt.Printf("Tag generated by PCSSHA256 after Hex Encode in || bounds:\n||%s||\n", hex.EncodeToString(tag[:]))
return tag
}
下面是第一次运行的 fmt.Printf 输出:
tohashinner in || bounds:
||gdebc`anolwtursp||
tohashouter in || bounds:
||
llllllllllllllllllllllllllllllllllllllllllllllll||
tohashinnerwmess in || bounds:
||gdebc`anolwturspThis is a sample message. It is to be used for testing.
||
firsthashval after Hex Encode in || bounds:
||7b7bec6f1a9e8860a40730f76f3c5a5f3576a90008630e0dc3fbdc088430ce0f||
Input message in || bounds:
||This is a sample message. It is to be used for testing.
||
Input kmac in || bounds:
||QRSTUVWXYZABCDEF||
Tag generated by PCSSHA256 after Hex Encode in || bounds:
||fd54280dbbca722e41024100c8fa171fa640c814cb4c06380efced71d37504d5||
这是第二次运行的输出:
tohashinner in || bounds:
||gdebc`anolwtursp||
tohashouter in || bounds:
||
llllllllllllllllllllllllllllllllllllllllllllllll||
tohashinnerwmess in || bounds:
||gdebc`anolwturspThis is a sample message. It is to be used for testing.
||
firsthashval after Hex Encode in || bounds:
||632b978ee2b3498754b761e3e091832a6e8f8308ea89f55749a0754f481564d1||
Input message in || bounds:
||This is a sample message. It is to be used for testing.
||
Input kmac in || bounds:
||QRSTUVWXYZABCDEF||
Tag generated by PCSSHA256 after Hex Encode in || bounds:
||e81de52ce8c7d86ef9937011e2978b452d300b85051cf68f7bd99572558e3cee||
在这两个输出中,除了 firsthashvalue 和结果标签之外,一切都是一样的。由于 tohashinnerwmess(我仅使用一个变量来弄清楚出了什么问题)在这两种情况下都是等效的,并且它是唯一被输入 sha256.Sum256 的东西,我很困惑为什么两个输入的 firsthashvalue 不同。
我很确定 sha256.Sum256 是确定性的,预计会为给定的输入返回相同的输出 - 在 Go Playground 中对此进行了测试,实际上它返回了相同的东西 - 所以我认为问题出在我的代码或我对 Sum256 内部工作原理的理解。如果有人能解释我做错了什么,那将不胜感激。
谢谢。
编辑: - 在第一次运行时,使用 ioutil.ReadFile() 直接从文件中读取输入到 PCSSHA256 函数的消息,而在第二次运行时,加密消息首先通过 AES 逐 block 处理,然后传递给 PCSSHA256。
最佳答案
回答:这是我的代码的问题。在第二种情况下,当转换为十六进制时,我的消息被 8 个额外的 0 填充 - 此填充在打印为字符串时不可见。
关于Golang crypto/sha256 - 相同的输入产生不同的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46555379/
为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
这是一道面试题,我没有答对,但还是很好奇怎么解。你有N个人的大家庭,分别是1,2,3,...,N岁。你想给你的大家庭拍张照片。所有的家庭成员都排成一排。“我是家里的friend,建议家庭成员安排如下:”1岁的家庭成员坐在这一排的最左边。每两个坐在一起的家庭成员的年龄相差不得超过2岁。输入:整数N,1≤N≤55。输出:摄影师可以拍摄的照片数量。示例->输入:4,输出:4符合条件的数组:[1,2,3,4][1,2,4,3][1,3,2,4][1,3,4,2]另一个例子:输入:5输出:6符合条件的数组:[1,2,3,4,5][1,2,3,5,4][1,2,4,3,5][1,2,4,5,3][
我有一个这样的哈希数组:[{:foo=>2,:date=>Sat,01Sep2014},{:foo2=>2,:date=>Sat,02Sep2014},{:foo3=>3,:date=>Sat,01Sep2014},{:foo4=>4,:date=>Sat,03Sep2014},{:foo5=>5,:date=>Sat,02Sep2014}]如果:date相同,我想合并哈希值。我对上面数组的期望是:[{:foo=>2,:foo3=>3,:date=>Sat,01Sep2014},{:foo2=>2,:foo5=>5:date=>Sat,02Sep2014},{:foo4=>4,:dat
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha
我想使用spawn(针对多个并发子进程)在Ruby中执行一个外部进程,并将标准输出或标准错误收集到一个字符串中,其方式类似于使用Python的子进程Popen.communicate()可以完成的操作。我尝试将:out/:err重定向到一个新的StringIO对象,但这会生成一个ArgumentError,并且临时重新定义$stdxxx会混淆子进程的输出。 最佳答案 如果你不喜欢popen,这是我的方法:r,w=IO.pipepid=Process.spawn(command,:out=>w,:err=>[:child,:out])
我基本上来自Java背景并且努力理解Ruby中的模运算。(5%3)(-5%3)(5%-3)(-5%-3)Java中的上述操作产生,2个-22个-2但在Ruby中,相同的表达式会产生21个-1-2.Ruby在逻辑上有多擅长这个?模块操作在Ruby中是如何实现的?如果将同一个操作定义为一个web服务,两个服务如何匹配逻辑。 最佳答案 在Java中,模运算的结果与被除数的符号相同。在Ruby中,它与除数的符号相同。remainder()在Ruby中与被除数的符号相同。您可能还想引用modulooperation.
我想知道Ruby用来在命令行打印这些东西的输出流:irb(main):001:0>a="test"=>"test"irb(main):002:0>putsatest=>nilirb(main):003:0>a=>"test"$stdout是否用于irb(main):002:0>和irb(main):003:0>?而且,在这两次调用之间,$stdout的值是否有任何变化?另外,有人能告诉我打印/写入这些内容的Ruby源代码吗? 最佳答案 是的。而且很容易向自己测试/证明。在命令行试试这个:ruby-e'puts"foo"'>test.
让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来
我在使用自定义RailsFormBuilder时遇到了问题,从昨天晚上开始我就发疯了。基本上我想对我的构建器方法之一有一个可选block,以便我可以在我的主要content_tag中显示其他内容。:defform_field(method,&block)content_tag(:div,class:'field')doconcatlabel(method,"Label#{method}")concattext_field(method)capture(&block)ifblock_given?endend当我在我的一个Slim模板中调用该方法时,如下所示:=f.form_field:e