-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode_crypto.py
More file actions
84 lines (72 loc) · 2.36 KB
/
node_crypto.py
File metadata and controls
84 lines (72 loc) · 2.36 KB
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
75
76
77
78
79
80
81
from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
from Crypto.Signature import PKCS1_v1_5
from Crypto.Hash import SHA512, SHA384, SHA256, SHA, MD5
import hashlib
from Crypto import Random
from base64 import b64encode, b64decode
import hashlib
class Crypto:
hash = "SHA-256"
@staticmethod
def newkeys(keysize):
random_generator = Random.new().read
key = RSA.generate(keysize, random_generator)
private, public = key, key.publickey()
return public, private
@staticmethod
def importKey(externKey):
return RSA.importKey(externKey)
@staticmethod
def getpublickey(priv_key):
return priv_key.publickey()
@staticmethod
def encrypt(message, pub_key):
cipher = PKCS1_OAEP.new(Crypto.importKey(pub_key))
value = message.encode('utf-8')
return cipher.encrypt(value)
@staticmethod
def decrypt(ciphertext, priv_key):
cipher = PKCS1_OAEP.new(priv_key)
return cipher.decrypt(ciphertext)
@staticmethod
def sign(message, priv_key, hashAlg="SHA-256"):
#global hash
#hash = hashAlgs
signer = PKCS1_v1_5.new(Crypto.importKey(priv_key))
digest = SHA256.new()
"""if (hash == "SHA-512"):
digest = SHA512.new()
#elif (hash == "SHA-384"):
digest = SHA384.new()
#elif (hash == "SHA-256"):
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()"""
digest.update(message)
return b64encode(signer.sign(digest))
@staticmethod
def verify(message, signature, pub_key):
value = message.encode('utf-8')
signer = PKCS1_v1_5.new(Crypto.importKey(pub_key))
digest = SHA256.new()
"""if (hash == "SHA-512"):
digest = SHA512.new()
elif (hash == "SHA-384"):
digest = SHA384.new()
elif (hash == "SHA-256"):
elif (hash == "SHA-1"):
digest = SHA.new()
else:
digest = MD5.new()"""
digest.update(value)
return signer.verify(digest, b64decode(signature))
@staticmethod
def gen_hash(contents):
try:
value = contents.encode('utf-8')
except AttributeError:
value = contents
contents_hash = hashlib.sha512(value).hexdigest()
return contents_hash