usr bin env python3 from binascii import hexlify unhexlify from pprint

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#!/usr/bin/env python3
from binascii import hexlify, unhexlify
from pprint import pprint
key_pem = (
b"-----BEGIN PRIVATE KEY-----\n"
b"MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQDRgJ0gr3uHa/UB\n"
b"0CQiPu2/YQByMLYvko3SbI/seMNvkq+iawYTVl62Q1SoZ9rZldNFlEvqaePiKYaJ\n"
b"PB4ZXMcEqnonD5YATq92SpxkvPngu4t2FvPPS86C6FvKbnIxfgHbR64vWQTrEOj4\n"
b"s48GFFJ+5QSzl6J7/HB0WpEHdGjTaMM5kp54DvO1VX3mbOEQyoq4itm2m+eyREic\n"
b"Ye3KHGykGZJJTcrTCf+qJXOWecvNpuVIny9IGyWy1CDnGNX5Qj7ckA91/WVPm7d0\n"
b"qsbrKWuDKxwp0dV8p64+wWGkDJcj87HQcroU3tCsDJN4iF8KpCGQ097UyA9xdXTw\n"
b"kk5riQZdAgMBAAECggEABiLVt1Dcdd1wGiL+A/DC5uGQ8UdC9sa8l6atWng5BSoP\n"
b"MdsfnO5hLMZxEtOj4c6VFwseZBnA3m1n7toPaZ/Bhn31wPIgaxbi5byOsxaj3PSx\n"
b"Q36tmms2e7gRhC7S8mcl58XEMMfTMI1YvXwI2t06g1Py3M26qRX+NzI14DmFHnf7\n"
b"+EdiI5vfms/Whu6r3ekCkjIcPo67icP3RdaKaXzW7r9TwBPcyvriREjxCq+/sav8\n"
b"ZwCUc9Zo91eh11G7tT8QrTXdxZHuVRTOxWEdgSAo5ZZ77nPHIWvNOvnI5ZEkMNG3\n"
b"IFEuqtvwEHoTSQCsgaIQXhe/zOG65Cs5mQZW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ge) == 20
import OpenSSL.crypto as openssl
k = openssl.load_privatekey(openssl.FILETYPE_PEM, key_pem)
signature = openssl.sign(k, challenge, 'sha1')
pprint(hexlify(signature))
# --->
# (b'308a810a90a30513c93167ed2674f97cef1c4bc6df8160a1ea5480763d34ac3043c576fa2fa1'
# b'71e48bdd0bbb13bcf38bbd4483b3cba215f347439e3e169bb02e49b5b47679ec2dad328174cd'
# b'f893b2c71465b3eba858b00cc92aa536af2f5c85307cc331a19c4acd54923f23e0b9bf5009b2'
# b'6d2a4469378e352eaf29f7ce333b8cabca39d9b8858b73e93b745b30ee74264623ef790e6a61'
# b'a1e7ffb360aa9505f7fc868881d8440ff6765f233dac259a11d49c221ab7549e16df07bdc99e'
# b'dbbe953ca7e9b1164a115932a9e4c4e3c3509008127298f9d5baae405d97e179c949b013b983'
# b'76c04d92b7fdf0056f60c9c6df2932c122a7bce0afcf334c13e982b3')
#-- wrong! should be:
# 21 B3 AD 47 7A 3F 83 A6 CA 00 E7 D9 89 DB 11 C0
# 90 6F 5D 27 9C 43 BB BD 5D 4A 02 4E 1C 11 F3 6C
# 3A DC D8 25 B1 8D 16 AC 64 8D 34 7B 9F 1F 77 AC
# 29 F6 4F B3 C8 A7 42 78 D3 1E 2C 9E E8 09 9F 58
# A4 65 0F 45 9F 33 CC B5 01 38 7E A7 D7 31 B9 C7
# 46 D4 82 8C 47 68 B1 F5 86 BD 1E 01 7D 03 3B 88
# 57 0C F6 80 FC 7D 47 88 24 D9 EF F8 19 2D B3 73
# 31 B0 9D 5E 8F 9F 77 9E 33 2B E7 EE AA 51 90 05
# 29 75 A2 88 08 25 7A 9E 31 9E 5B ED 28 14 3E 54
# EC 63 AB 08 3B 61 8C 60 93 83 74 63 1F CD E7 10
# E2 B5 1D EC 61 15 40 83 A2 1E E2 80 B8 90 B3 A2
# 7D 10 BB 8E D8 1D 42 DD F2 52 E2 08 C1 7C 27 FB
# 97 C0 DF BD 28 20 C7 B9 94 D3 71 85 2D 1E 3F 75
# FF 33 EE 8F 44 D7 1A A3 A7 37 2F BD 84 4B D4 2D
# C6 72 75 C7 F4 CE 56 3C 98 6F C4 F8 1B DD 37 13
# B0 E2 BA AA 69 75 25 4D 2B A4 3C A1 D2 C3 88 69
#