openssl dgst -sha256 -sign

时间:2015-12-28 16:04:27

标签: php openssl sign

我们如何在PHP中实现此命令?

openssl dgst -sha256 -sign private.key -out data.signed data

我尝试了openssl_sign() PHP函数,但它生成了不同的输出......

让我们来看看这个私钥:

-----BEGIN PRIVATE KEY-----
MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCrYL5Zq4rvXwEn
acjPDXjrl0UnAKfAchtNQIyEdf/a41tFgZkw1DXS2s+9eWNBarWYEwGhFoiOeeop
ShU7qa5IKL7pwLaO8vPbTPNWZZ9sy3Vx18lB54IyRPBX5lrLU+n0XuYyFcZa1+Gd
Dtgu+AQQqjDGGuSJecuABWH+rzzfumMi9K4gP9S0J8vq60mRna6R47Ati7t/jBJm
b2vSoSaWjtKf0LJoRPnbc7SgYF8D4JWJ3xQnsa8LrrBudV5j+N3uRDMimUeHL6iW
1KoAb8g1Iql/b3/2C0Z0M/O/13sECytFxDBVP9W+lvHwNF7pZR+C/BvhFH6FnrFI
fRismw8BAgMBAAECggEBAIsHKqS4Azf5TIhayusdtND6oMDpSS1X5EohaV00FOHP
u4WBg3MXVKq/k/PT++9fz/2UvaefDhz3Tj08ukjyeE5Vr7sV+YOyGJ35qRaXzmOG
ErrOIZjzZK2/O3MzjsiQRKVYL0rGW2nq5D5zgnFoBnQ2fObZfjkAs1QiCcOBCdM8
nttTO31JCwyBuTKGK3KaJdoba6dVze5Xhm1b/haCmq2EJOML5G73jK7m8aYkVDFb
FJOShYATyz5Xju33QEPz+HWcMrk/TKZFRQYSjWFAzy0/ENrMptIVff3UN0KL0YSl
0hNHlJwHStDEZAiMw4A20ojtKqfUkyR1kPzxpWdi/BkCgYEA4VVFhv5RNSJUGm1I
zhJT54UlfIW57c/NWx/Tqbhe0QYg//sM4c/s8lw45lvmRc7vKs6C/ZGo18p+XNT+
BaZdNFlxbtKJisM61Cegc8pIwzv4mbTyo9SjoWswxt20zVp8sIGeF4w7X94gPVZ0
f8D3OELujXqk2i34blL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QcDlGN21NaFhHV3RmaG5RN1lMdmdFRUtvd3hocmtpWG5MZ0FWaF9xODgzN3BqSXZTdUlEX1V0Q2ZMNnV0SmtaMnVrZU93TFl1N2Y0d1NabTlyMHFFbWxvN1NuOUN5YUVUNTIzTzBvR0JmQS1DVmlkOFVKN0d2QzY2d2JuVmVZX2pkN2tReklwbEhoeS1vbHRTcUFHX0lOU0twZjI5XzlndEdkRFB6djlkN0JBc3JSY1F3VlRfVnZwYng4RFJlNldVZmd2d2I0UlItaFo2eFNIMFlySnNQQVEifX0.eyJjb250YWN0IjpbIm1haWx0bzptbG9jYXRpQGdtYWlsLmNvbSJdLCJhZ3JlZW1lbnQiOiJodHRwOi8vMTI3LjAuMC4xOjQwMDEvdGVybXMvdjEiLCJyZXNvdXJjZSI6Im5ldy1yZWcifQ

使用openssl dgst -sha256 -sign private.key -out data.signed data shell命令,我有这个输出:

00000000   18 6C 67 3F  2F 26 41 64  E1 FA 58 B0  1B EA 65 3B  4A 22 2D 6B  03 82 AB DB  .lg?/&Ad..X...e;J"-k....
00000018   18 B5 D7 5B  31 F1 D6 96  90 8E 5D 77  47 C9 20 2C  A7 42 08 FD  09 58 97 FB  ...[1.....]wG. ,.B...X..
00000030   36 B6 BB B5  18 37 2C 50  C8 F6 07 4E  E5 4E 1F 86  60 0B BF A4  A8 6B AB DF  6....7,P...N.N..`....k..
00000048   B4 14 9D 18  44 03 2C 36  51 09 80 FE  2F 54 05 D1  62 8C 9D A7  9D D7 FE 7A  ....D.,6Q.../T..b......z
00000060   DE CD 19 D1  26 C4 CB E0  9E 21 0E 1B  4C B2 B0 C4  E3 0E FB 6D  E1 D0 CA 8F  ....&....!..L......m....
00000078   A3 CB E6 61  A0 E2 8F 95  65 FE 98 34  32 FE 97 52  5A 83 FB C9  21 98 68 D6  ...a....e..42..RZ...!.h.
00000090   CA 03 DA 7E  F3 6F 1C 83  89 CB D5 7A  9E B3 14 B3  8F 50 B0 96  B6 C7 97 96  ...~.o.....z.....P......
000000A8   0D 84 BE 7E  37 7F BE D2  9D 1C AB F2  5C 4C CD B7  E6 66 87 4C  C8 7A 9B 59  ...~7.......\L...f.L.z.Y
000000C0   50 65 1D FE  CD D5 CB AC  CD CE 91 55  E0 E8 BC BF  10 B9 21 EC  E9 2C 57 8A  Pe.........U......!..,W.
000000D8   CC 9F F1 30  49 B9 A2 B3  44 27 5B 61  D2 3F D9 60  3B AD B7 07  CE F6 DF AE  ...0I...D'[a.?.`;.......
000000F0   94 B3 62 40  26 0E 57 22  F3 F1 4E 02  2F 1C 00 D1                            ..b@&.W"..N./...

使用PHP代码openssl_sign($data, $signature, $privateKey, OPENSSL_ALGO_SHA256)我有:

00000000   5C 8A DC B6  36 83 B0 E1  82 2D 3E B5  23 E2 14 D4  A4 4B A9 9D  DA 4B 3C EB  \...6....->.#....K...K<.
00000018   AF EE 9F A2  F8 0E DA 40  68 25 DB 6D  44 A1 08 B5  92 56 9E D7  E3 5E 3D 59  .......@h%.mD....V...^=Y
00000030   1F 53 D2 21  A2 E7 5E 58  74 42 FE 5D  5B 36 1F 30  C4 DF 45 DE  A4 70 2D D7  .S.!..^XtB.][6.0..E..p-.
00000048   9B 2E 61 D5  2C 29 AA C1  93 4D 42 86  F4 35 62 52  DE B1 1F 1B  0C F4 A2 53  ..a.,)...MB..5bR.......S
00000060   95 35 BC F8  65 AB 28 A1  2C DC 66 2B  B1 10 34 13  CC 4D 5A F1  13 8C 47 80  .5..e.(.,.f+..4..MZ...G.
00000078   DC 11 19 35  F5 24 78 1A  C8 E6 AE EE  83 21 1B 12  7F AA 35 74  7F 23 DE 54  ...5.$x......!...5t#.T
00000090   EB 76 70 FA  6B 39 79 CC  35 F4 57 D8  95 B3 07 86  30 12 AF 5C  83 7A 28 25  .vp.k9y.5.W.....0..\.z(%
000000A8   93 AE B9 A3  37 86 ED 78  AD 59 10 25  90 2F 64 53  7C DE DE 9C  D6 B5 16 A3  ....7..x.Y.%./dS|.......
000000C0   DE B1 CE BC  B5 5A A8 8A  BC 8B 2D CE  18 A1 B4 97  BF 92 F5 0C  8B B8 38 6D  .....Z....-...........8m
000000D8   E6 DC E7 20  43 B9 82 6B  41 46 83 F1  89 40 BA 28  3D 8C 3D 1F  22 C7 ED C3  ... C..kAF...@.(=.=."...
000000F0   0C 24 0A 91  ED CC 27 B7  1C B4 5C 67  1D C0 1D 8F                            .$....'...\g....

如您所见,我有两种不同的输出......

PS:你可以在这里找到完整的例子/日志:

1 个答案:

答案 0 :(得分:0)

我使用了你的数据,我得到了相同的输出。

我的步骤:

控制台命令

openssl dgst -sha256 -sign private.key -out data.signed data.txt

PHP代码

$data = file_get_contents("data.txt");
$pkeyid = openssl_pkey_get_private("file://private.key");
openssl_sign($data, $signature, $pkeyid, OPENSSL_ALGO_SHA256);
openssl_free_key($pkeyid);
var_dump(file_put_contents("datasigned-php",$signature));

我的版本:

  

OpenSSL LibreSSL 2.2.7

     

Php版本字符串5.6.30

     

OS MacOs Sierra 10.12.6

我认为您的数据之间存在差异。因为您给出的数据方向的结果是相等的