-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
81 lines (63 loc) · 1.72 KB
/
Copy pathutils.py
File metadata and controls
81 lines (63 loc) · 1.72 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
import random
import json
def gcd(a, b):
while b:
a, b = b, a % b
return a
def extended_gcd_iterative(a, b):
x0, x1 = 1, 0
y0, y1 = 0, 1
while b != 0:
q = a // b
a, b = b, a % b
x0, x1 = x1, x0 - q * x1
y0, y1 = y1, y0 - q * y1
return (a, x0, y0)
def modinv(a, m):
g, x, y = extended_gcd_iterative(a, m)
if g != 1:
raise Exception('Modular inverse does not exist')
return x % m
def modexp(base, exp, mod):
result = 1
base = base % mod
while exp > 0:
if exp % 2 == 1:
result = (result * base) % mod
base = (base * base) % mod
exp = exp // 2
return result
def is_prime(n, k=5):
if n <= 1:
return False
if n <= 3:
return True
if n % 2 == 0:
return False
r, d = 0, n - 1
while d % 2 == 0:
d //= 2
r += 1
for _ in range(k):
a = random.randint(2, n - 2)
x = modexp(a, d, n)
if x == 1 or x == n - 1:
continue
for _ in range(r - 1):
x = (x * x) % n
if x == n - 1:
break
else:
return False
return True
def int_to_text(number):
byte_length = (number.bit_length() + 7) // 8
return number.to_bytes(byte_length, byteorder='big').decode('utf-8')
def text_to_int(text):
return int.from_bytes(text.encode('utf-8'), byteorder='big')
def save_key_to_file(key_dict, path):
with open(path, 'w') as f:
json.dump(key_dict, f, indent=2)
def load_key_from_file(path):
with open(path, 'r') as f:
return json.load(f)