This repository has been archived by the owner on Feb 6, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockCrypto.py
executable file
·54 lines (47 loc) · 1.98 KB
/
blockCrypto.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
46
47
48
49
50
51
52
53
54
#!/usr/bin/python3
# Developed by Nino Stephen Mathew ( ninosm12[at]gmail[dot]com )
# Crypto Module
from fastecdsa import keys
from fastecdsa import curve
from fastecdsa import ecdsa
from hashlib import sha256
from hashlib import sha384
from hashlib import new
from base58check import b58encode
def keyGen(username, type):
privateKeyFile = username + '_privkey.pem'
publicKeyFile = username + '_pubkey.pem'
privateKey = keys.gen_private_key(curve.P256)
publicKey = keys.get_public_key(privateKey, curve.P256)
keys.export_key(privateKey, curve = curve.P256, filepath = "keys/" + type + "Key/" + privateKeyFile)
keys.export_key(publicKey, curve = curve.P256, filepath = "keys/" +type + "Key/" + publicKeyFile)
return True
#return privateKey, publicKey
def getPublicKey(username, type):
privateKey, publicKey = keys.import_key("keys/" + type + "Key/" + username + '_pubkey.pem')
return str(publicKey).encode('ascii')
def signTransaction(username, transactionData, type='user'):
privateKey, publicKey = keys.import_key("keys/" + type + "Key/" + username + '_privkey.pem')
r, s = ecdsa.sign(transactionData, privateKey, hashfunc = sha384)
return r, s, transactionData
def verifyTransaction(r, s, transactionData, publicKey):
valid = ecdsa.verify((r,s), transactionData, publicKey, hashfunc = sha384)
return valid
def generateWalletAddr(username, type):
publicKey = getPublicKey(username,type)
h = sha256()
h.update(str(publicKey).encode('utf-8'))
firstHash = h.hexdigest()
h = new('ripemd160')
h.update(str(firstHash).encode('utf-8'))
ripemd160Hash = h.hexdigest()
ripemd160HashExtd = '00' + ripemd160Hash
h = sha256()
h.update(str(ripemd160HashExtd).encode('utf-8'))
hash2 = h.hexdigest()
h.update(str(hash2).encode('utf-8'))
hash3 = h.hexdigest()
fourBytes = hash3[:8]
binAddr = ripemd160HashExtd + fourBytes
walletAddr = b58encode(str(binAddr).encode('utf-8'))
return walletAddr.decode('ascii')