Skip to content

Commit ffab907

Browse files
committed
Added files via upload
1 parent 322fc57 commit ffab907

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed

checksum.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from Crypto.Hash import SHA256, SHA, MD5
2+
import argparse
3+
4+
parse = argparse.ArgumentParser(prog='Checksum',description='Support SHA1, SHA256, MD5', add_help=False);
5+
parse.add_argument("-h", "--hash", help="Hash Althorigm")
6+
parse.add_argument("-c", "--checksum", help="Checksum Input")
7+
parse.add_argument("input", help="Input file")
8+
9+
args = parse.parse_args()
10+
args.hash = args.hash.lower()
11+
if (args.hash != 'sha1' and args.hash != 'sha256' and args.hash != 'md5'):
12+
print('The althorigm is not supported')
13+
else:
14+
15+
block_size=256
16+
data=''
17+
fi = open(args.input,"rb")
18+
while (1):
19+
tmp = fi.read(block_size)
20+
if len(tmp) == 0:
21+
break;
22+
else: data += tmp
23+
fi.close()
24+
25+
hashvalue = ''
26+
if (args.hash == 'sha1' ):
27+
tmp = SHA.new(data)
28+
hashvalue += tmp.hexdigest()
29+
elif (args.hash == 'sha256'):
30+
tmp = SHA256.new(data)
31+
hashvalue += tmp.hexdigest()
32+
elif args.hash == 'md5':
33+
tmp = MD5.new(data)
34+
hashvalue += tmp.hexdigest()
35+
36+
if args.checksum is not None:
37+
if (args.checksum == hashvalue):
38+
print('True')
39+
else: print('False')
40+
print("Checksum is: ")
41+
print(hashvalue)
42+
43+

sign.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
from Crypto.Hash import SHA256, SHA, MD5
2+
from Crypto.PublicKey import RSA
3+
from Crypto.Signature import PKCS1_v1_5
4+
from Crypto import Random
5+
import argparse
6+
import os.path
7+
8+
parse = argparse.ArgumentParser(prog='Make Digital Signature', description='Support SHA1, SHA256, MD5', add_help=False);
9+
parse.add_argument("-h", "--hash", help="Hash Althorigm")
10+
parse.add_argument("input", help="Input data file.")
11+
parse.add_argument("output", help="Output signature file")
12+
args = parse.parse_args()
13+
args.hash = args.hash.lower()
14+
if (args.hash != 'sha1' and args.hash != 'sha256' and args.hash != 'md5'):
15+
print('The althorigm is not supported')
16+
else:
17+
#Create Hash value from data.
18+
print("Creating digital signature")
19+
block_size=256
20+
data=''
21+
fi = open(args.input,"rb")
22+
while (1):
23+
tmp = fi.read(block_size)
24+
if len(tmp) == 0:
25+
break;
26+
else: data += tmp
27+
fi.close()
28+
29+
hash
30+
if (args.hash == 'sha1' ):
31+
hash = SHA.new(data)
32+
elif (args.hash == 'sha256'):
33+
hash = SHA256.new(data)
34+
elif args.hash == 'md5':
35+
hash = MD5.new(data)
36+
37+
if os.path.isfile("prvkey.prv"):
38+
prvkey_file = open("prvkey.prv","r")
39+
key = RSA.importKey(prvkey_file.read())
40+
prvkey_file.close()
41+
else:
42+
# create random key
43+
random_generator = Random.new().read
44+
key = RSA.generate(1024, random_generator)
45+
46+
# save private key
47+
prvkey_file = open("prvkey.prv","w")
48+
prvkey_file.write(key.exportKey())
49+
prvkey_file.close()
50+
51+
# sign by private key
52+
signer = PKCS1_v1_5.new(key)
53+
signature = signer.sign(hash)
54+
print("The digital signature for your file is: ")
55+
print(signature)
56+
57+
# export public key
58+
pubkey_file = open("pubkey.pub","w")
59+
pubkey_file.write(key.publickey().exportKey())
60+
pubkey_file.close()
61+
print("Public key is: ")
62+
print(key.publickey().exportKey())
63+
print("Public key has been exported into <pubkey.pub>")
64+
65+
#export signature
66+
fo = open(args.output,"wb")
67+
fo.write(signature)
68+
fo.close()
69+
print("Digital signature has been saved into file " + args.output)
70+
print("Finished")

very_sign.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
from Crypto.Hash import SHA256, SHA, MD5
2+
from Crypto.PublicKey import RSA
3+
from Crypto.Signature import PKCS1_v1_5
4+
import argparse
5+
import os
6+
parse = argparse.ArgumentParser(prog='Verify Digital Signature', description='Support SHA1, SHA256, MD5', add_help=False);
7+
parse.add_argument("-h", "--hash", help="Hash Althorigm")
8+
parse.add_argument("data", help="Input data file.")
9+
parse.add_argument("signature", help="Input signature file")
10+
args = parse.parse_args()
11+
args.hash = args.hash.lower()
12+
if (args.hash != 'sha1' and args.hash != 'sha256' and args.hash != 'md5'):
13+
print('The althorigm is not supported')
14+
else:
15+
#Create Hash value from data.
16+
print("Verifying this digital signature")
17+
block_size=256
18+
data=''
19+
fi = open(args.data,"rb")
20+
while (1):
21+
tmp = fi.read(block_size)
22+
if len(tmp) == 0:
23+
break;
24+
else: data += tmp
25+
fi.close()
26+
27+
hash
28+
if (args.hash == 'sha1' ):
29+
hash = SHA.new(data)
30+
elif (args.hash == 'sha256'):
31+
hash = SHA256.new(data)
32+
elif args.hash == 'md5':
33+
hash = MD5.new(data)
34+
print(hash.digest())
35+
36+
#Read signature from signature
37+
fi = open(args.signature,"r")
38+
signature = fi.read()
39+
fi.close()
40+
41+
42+
if os.path.isfile("pubkey.pub"):
43+
#Read public key
44+
pubkey_file = open("pubkey.pub", "r")
45+
pubkey = RSA.importKey( pubkey_file.read() )
46+
pubkey_file.close()
47+
48+
#verify
49+
verifier = PKCS1_v1_5.new(pubkey)
50+
if verifier.verify(hash, signature):
51+
print("It's true")
52+
else: print("It's false")
53+
else: print("Can not find public key file <pubkey.pub>...Can not complete verifying")
54+
55+
print("Finished")

0 commit comments

Comments
 (0)