-
Notifications
You must be signed in to change notification settings - Fork 0
/
enc.py
46 lines (32 loc) · 930 Bytes
/
enc.py
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
from sys import argv
from rsa import PrivateKey,sign
from qrcode import QRCode
from qrcode.constants import ERROR_CORRECT_L
def padding( string , size ):
return ( size - len(string) ) * '*' + string
def main():
if len(argv) < 3:
print 'Format: enc.py <identifier> <message>'
return
ID = argv[1] # Identifier of origin
MESSAGE = argv[2]
# Read private key of origin
with open(ID+'.pr','r') as f:
privatekey = PrivateKey.load_pkcs1( f.read() )
# Sign message with private key
SIGN = sign( MESSAGE , privatekey , 'SHA-256' ).encode('hex')
# Concatenate message, signature and identifier
TEXT = padding( MESSAGE , 100 ) + padding( SIGN , 400 ) + padding( ID ,100 )
# Create QR code
qr = QRCode(
version=1,
error_correction=ERROR_CORRECT_L,
box_size=10,
border=4,
)
qr.add_data( TEXT )
qr.make(fit=True)
img = qr.make_image()
img.save(ID + '.png')
if __name__ == '__main__':
main()