在 helmbert的答案中描述,OpenSSL (注意,我已经将 -n 选项包含到 -sign echo ): $ echo -n测试。 | openssl dgst -sha256 -sign private.key -hex 52e1cce3810c1a89693cf6965d1035618820a9e3a7b95203d885c4153dc3f7424b98e3ba628a186f1074d672bb59a1c0788a9c2064951ca2326eb1bf8e3e49e9 以相反方式并在Go代码中对散列消息进行签名,您可以将 hash 参数的值作为 0 传递给 rsa.SignPKCS1v15 : indata:= [] byte(Test。) s ,err:= rsa.SignPKCS1v15(nil,privKey,0,indata) I use openssl command to sign the message "Test.", output with hexdump# echo "Test." | openssl rsautl -inkey privite.key -sign -hexdump0000 - 09 1b ce e2 4b 69 86 be-d7 b1 fb f0 ec e4 53 0e ....Ki........S.0010 - ef 9c a4 7b db d3 21 d5-3e 78 23 61 89 34 7e bc ...{..!.>x#a.4~.0020 - e9 1e 5a e9 f4 40 e6 53-07 e4 dd 1a fe 31 ec 42 [email protected] - 98 a5 07 d4 7e d9 f4 01-2f ba a3 65 18 b7 69 a4 ....~.../..e..i.The hex string is 091bcee24b69...My private.Key# cat private.Key-----BEGIN RSA PRIVATE KEY-----MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu/ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWuRTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7AIU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlStAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V-----END RSA PRIVATE KEY-----Generate signature with Golangvar prvKeyPem = `-----BEGIN RSA PRIVATE KEY-----MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu/ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWuRTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7AIU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlStAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V-----END RSA PRIVATE KEY-----`func GenerateSignature() { block, _ := pem.Decode([]byte(prvKeyPem)) if block == nil { panic("failed to parse root certificate PEM") } privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) //x509.ParseCertificate(block.Bytes) if err != nil { panic("failed to parse certificate: " + err.Error()) } indata := "Test." h := sha256.New() h.Write([]byte(indata)) digest := h.Sum(nil) s, err := rsa.SignPKCS1v15(rand.Reader, privKey, crypto.SHA256, digest) if err != nil { panic("failed to sign:" + err.Error()) } fmt.Printf("%x\n", s)}func main() { GenerateSignature()}go run this code, following is output:52e1cce3810c1a89693cf6965d1035618820a9e3a7b95203d885c4153dc3f7424b98e3ba628a186f1074d672bb59a1c0788a9c2064951ca2326eb1bf8e3e49e9But I think it should be:091bcee24b69...Where is my wrong? Thanks 解决方案 In addition to the newline added by echo described in helmbert’s answer, the OpenSSL rsautl command operates directly on the supplied data, while the Go code first hashes the data with SHA256 and then signs the resulting digest.To perform the same as the Go code with OpenSSL, you can use the dgst command with the -sign option (note I’ve included the -n option to echo here too):$ echo -n "Test." | openssl dgst -sha256 -sign private.key -hex52e1cce3810c1a89693cf6965d1035618820a9e3a7b95203d885c4153dc3f7424b98e3ba628a186f1074d672bb59a1c0788a9c2064951ca2326eb1bf8e3e49e9To go the other way and sign the raw message without hashing in Go code, you can pass 0 as the value of the hash argument to rsa.SignPKCS1v15:indata := []byte("Test.")s, err := rsa.SignPKCS1v15(nil, privKey, 0, indata) 这篇关于为什么我使用openssl和golang生成的RSA签名有所不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-27 22:39