关于RSA签名验签官网示例用openssl无法验证的问题
我想用openssl签名,用Go验签,在网上搜到了源码页,但源码页的结果和我的无法对应,如下
源码页
https://golang.org/src/crypto/rsa/pkcs1v15_test.go
其中的签名和验证代码:
```
// These vectors have been tested with
// `openssl rsautl -verify -inkey pk -in signature | hexdump -C`
var signPKCS1v15Tests = []signPKCS1v15Test{
{"Test.\n", "a4f3fa6ea93bcdd0c57be020c1193ecbfd6f200a3d95c409769b029578fa0e336ad9a347600e40d3ae823b8c7e6bad88cc07c1d54c3a1523cbbb6d58efc362ae"},
}
func TestSignPKCS1v15(t *testing.T) {
for i, test := range signPKCS1v15Tests {
h := sha1.New()
h.Write([]byte(test.in))
digest := h.Sum(nil)
s, err := SignPKCS1v15(nil, rsaPrivateKey, crypto.SHA1, digest)
if err != nil {
t.Errorf("#%d %s", i, err)
}
expected, _ := hex.DecodeString(test.out)
if !bytes.Equal(s, expected) {
t.Errorf("#%d got: %x want: %x", i, s, expected)
}
}
}
func TestVerifyPKCS1v15(t *testing.T) {
for i, test := range signPKCS1v15Tests {
h := sha1.New()
h.Write([]byte(test.in))
digest := h.Sum(nil)
sig, _ := hex.DecodeString(test.out)
err := VerifyPKCS1v15(&rsaPrivateKey.PublicKey, crypto.SHA1, digest, sig)
if err != nil {
t.Errorf("#%d %s", i, err)
}
}
}
// 私钥在页面最底部
// In order to generate new test vectors you'll need the PEM form of this key:
// -----BEGIN RSA PRIVATE KEY-----
// MIIBOgIBAAJBALKZD0nEffqM1ACuak0bijtqE2QrI/KLADv7l3kK3ppMyCuLKoF0
// fd7Ai2KW5ToIwzFofvJcS/STa6HA5gQenRUCAwEAAQJBAIq9amn00aS0h/CrjXqu
// /ThglAXJmZhOMPVn4eiu7/ROixi9sex436MaVeMqSNf7Ex9a8fRNfWss7Sqd9eWu
// RTUCIQDasvGASLqmjeffBNLTXV2A5g4t+kLVCpsEIZAycV5GswIhANEPLmax0ME/
// EO+ZJ79TJKN5yiGBRsv5yvx5UiHxajEXAiAhAol5N4EUyq6I9w1rYdhPMGpLfk7A
// IU2snfRJ6Nq2CQIgFrPsWRCkV+gOYcajD17rEqmuLrdIRexpg8N1DOSXoJ8CIGlS
// tAboUGBxTDq3ZroNism3DaMIbKPyYrAqhKov1h5V
// -----END RSA PRIVATE KEY-----
var rsaPrivateKey = &PrivateKey{
PublicKey: PublicKey{
N: fromBase10("9353930466774385905609975137998169297361893554149986716853295022578535724979677252958524466350471210367835187480748268864277464700638583474144061408845077"),
E: 65537,
},
D: fromBase10("7266398431328116344057699379749222532279343923819063639497049039389899328538543087657733766554155839834519529439851673014800261285757759040931985506583861"),
Primes: []*big.Int{
fromBase10("98920366548084643601728869055592650835572950932266967461790948584315647051443"),
fromBase10("94560208308847015747498523884063394671606671904944666360068158221458669711639"),
},
}
```
将上面的私钥写到文件key
用命令
echo -e "Test.\n" | openssl rsautl -sign -inkey key | hexdump -C
输出的是签名后的结果,如下:
```
00000000 89 e3 18 40 74 8b b0 cf 92 eb f4 65 57 ef 54 f5 |...@t......eW.T.|
00000010 f3 a0 2e 7f 7a f4 66 02 4f a6 18 62 2e b8 37 56 |....z.f.O..b..7V|
00000020 46 a1 66 cd 43 4b 06 73 23 87 05 0a 1d 55 82 6b |F.f.CK.s#....U.k|
00000030 79 dc 46 86 19 fd 5f f5 41 de 64 b6 5f 74 f7 d8 |y.F..._.A.d._t..|
00000040
```
其中有效部分为 89e318...........
而官网示例中为 a4f3fa6ea93b........
openssl与上面源码中,签名后的结果对应不上,请问社区有无做过RSA的,帮忙看下.......