forked from vast-ai/vast-pyworker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.py
33 lines (30 loc) · 920 Bytes
/
auth.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
from Crypto.PublicKey import RSA
from Crypto.Signature import pkcs1_15
from Crypto.Hash import SHA256
import base64
import subprocess
import time
def fetch_public_key():
command = ["curl", "-X", "GET", "https://run.vast.ai/pubkey/"]
result = subprocess.check_output(command, universal_newlines=True)
print("public key:")
print(result)
key = None
for _ in range(5):
try:
key = RSA.import_key(result)
break
except ValueError as e:
print(f"Error downloading key: {e}")
time.sleep(15)
return key
def verify_signature(public_key, message, signature):
if public_key is None:
print(f"No Public Key!")
return False
h = SHA256.new(message.encode())
try:
pkcs1_15.new(public_key).verify(h, base64.b64decode(signature))
return True
except (ValueError, TypeError):
return False