-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrolls.py
More file actions
60 lines (41 loc) · 1.51 KB
/
rolls.py
File metadata and controls
60 lines (41 loc) · 1.51 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
import base64
import json
from cryptography.hazmat.primitives.asymmetric import utils, rsa, padding
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.serialization import load_pem_private_key, load_pem_public_key
private_key = load_pem_private_key(open('key', 'rb').read(), password=None)
public_key = load_pem_public_key(open('key.pub', 'rb').read())
#print(private_key)
#private_key = rsa.generate_private_key(public_exponent=65537, key_size=2048)
#public_key = private_key.public_key()
pem = private_key.private_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PrivateFormat.TraditionalOpenSSL,
encryption_algorithm=serialization.NoEncryption()
)
#print(pem)
pem = public_key.public_bytes(
encoding=serialization.Encoding.PEM,
format=serialization.PublicFormat.SubjectPublicKeyInfo
)
#print(pem)
tok_header = {'alg': 'RS256', 'typ': 'JWT'}
tok_payload = {'name': 'garret'}
tok_header = base64.urlsafe_b64encode(json.dumps(tok_header, separators=(',', ':')).encode('utf-8')).replace(b'=', b'')
tok_payload = base64.urlsafe_b64encode(json.dumps(tok_payload, separators=(',', ':')).encode('utf-8')).replace(b'=', b'')
message = tok_header + b'.' + tok_payload
print(message)
print()
signature = private_key.sign(
message,
padding.PKCS1v15(),
hashes.SHA256()
)
print(base64.urlsafe_b64encode(signature).replace(b'=', b''))
ver = public_key.verify(
signature,
message,
padding.PKCS1v15(),
hashes.SHA256()
)
print(ver)