-
Notifications
You must be signed in to change notification settings - Fork 0
/
keys.py
executable file
·148 lines (134 loc) · 4.42 KB
/
keys.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
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
from cryptography.hazmat.primitives.asymmetric import ed25519
from cryptography.hazmat.primitives import serialization
import rsa
import os
# LINK https://cryptography.io/en/latest/hazmat/primitives/asymmetric/ed25519/
# NOTE Identity keys
privateKey = None
privateBytes = None
publicKey = None
publicBytes = None
# NOTE Encryption keys
privateRSAKey = None
privateRSAPEM = None
publicRSAKey = None
publicRSAPEM = None
# INFO Common entry point to authentication
def ensure():
if(
os.path.exists("private.key")
):
print("[ED25519] Loading ed25519 private key...")
load()
else:
print("[ED25519] Creating ed25519 private key...")
create()
def create():
global privateKey
global privateBytes
# ED25519 Creation
print("[ED25519] Generating ed25519 identity...")
privateKey = ed25519.Ed25519PrivateKey.generate()
print(privateKey)
privateBytes = privateKey.private_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PrivateFormat.Raw,
encryption_algorithm=serialization.NoEncryption()
)
print(privateBytes.hex())
# Public Key Creation
publicDerivation()
# RSA Creation
derive()
# Writing file
save()
def load(filepath="./"):
global privateBytes
# Reading file
with open(filepath + "private.key", "rb") as keyFile:
privateBytes = keyFile.read()
# Loading key
try:
loadBytes(privateBytes)
print("[ED25519] Loaded ed25519 private key from file [+]")
except Exception as e:
print("[ED25519] Could not load ed25519 private key: [X]")
print(e)
exit()
# INFO privateBytesProvided must be the same kind of data as the privateBytes (aka bytes)
def loadBytes(privateBytesProvided: bytes):
global privateKey
print("[ED25519] Loading ed25519 private key from bytes... [*]")
privateKey = ed25519.Ed25519PrivateKey.from_private_bytes(privateBytesProvided)
print("[ED25519] Loaded ed25519 private key from bytes [+]")
#print(privateKey)
# Public Key Creation
publicDerivation()
# RSA Creation
derive()
# INFO Deriving a public key from the private key
def publicDerivation():
global publicKey
global publicBytes
print("[ED25519] Generating ed25519 public key...[*]")
publicKey = privateKey.public_key()
#print(publicKey)
publicBytes = publicKey.public_bytes(
encoding=serialization.Encoding.Raw,
format=serialization.PublicFormat.Raw,
)
print("[ED25519] We are: " + publicBytes.hex())
print("[ED25519] Generated ed25519 public key [+]")
#print(publicBytes.hex())
# INFO RSA Derivation
def derive():
global privateRSAKey
global privateRSAPEM
global publicRSAKey
global publicRSAPEM
# RSA Creation
print("[RSA] Generating RSA keys from ed25519 identity... [*]")
privateRSAKey = rsa.generate_key(privateBytes.hex()) # So that the two are linked
privateRSAPEM = privateRSAKey.exportKey("PEM")
publicRSAKey = privateRSAKey.public_key()
publicRSAPEM = publicRSAKey.exportKey("PEM")
print("[RSA] Generated RSA keys from ed25519 identity [+]")
#print(privateRSAPEM)
#print(publicRSAPEM)
# INFO Encrypting a message (returning bytes)
def encrypt(message, publicKey=None):
global publicRSAKey
# Supporting self encryption
if not publicKey:
publicKey = publicRSAKey
# Generating the encrypted message
encrypted = rsa.encrypt(message, publicKey)
return encrypted
# INFO Decrypting a message (returning bytes)
def decrypt(message, privateKey=None):
global privateRSAKey
# Supporting self decryption by default
if not privateKey:
privateKey = privateRSAKey
# Generating the decrypted message
decrypted = rsa.decrypt(message, privateKey)
return decrypted
# INFO Sign a message after encoding it (returning bytes)
def sign(message):
global privateKey
signature = privateKey.sign(message.encode('utf-8'))
return signature
# INFO Verify a message (returning boolean)
def verify(message, signature, publicKeyProvided=None):
global publicKey
# Supporting self verification
if not publicKeyProvided:
publicKeyProvided = publicKey
# Generating the verified result
return publicKey.verify(signature, message.encode('utf-8'))
# ANCHOR Utilities
def save():
global privateBytes
print("[ED25519] Saving ed25519 key...")
with open("private.key", "wb") as f:
f.write(privateBytes)