mirror of
https://github.com/ansible-collections/community.crypto.git
synced 2026-05-07 05:43:06 +00:00
Reformat everything with black.
I had to undo the u string prefix removals to not drop Python 2 compatibility. That's why black isn't enabled in antsibull-nox.toml yet.
This commit is contained in:
@@ -14,7 +14,7 @@ import sys
|
||||
|
||||
|
||||
def binary_exp_mod(f, e, m):
|
||||
'''Computes f^e mod m in O(log e) multiplications modulo m.'''
|
||||
"""Computes f^e mod m in O(log e) multiplications modulo m."""
|
||||
# Compute len_e = floor(log_2(e))
|
||||
len_e = -1
|
||||
x = e
|
||||
@@ -31,18 +31,18 @@ def binary_exp_mod(f, e, m):
|
||||
|
||||
|
||||
def simple_gcd(a, b):
|
||||
'''Compute GCD of its two inputs.'''
|
||||
"""Compute GCD of its two inputs."""
|
||||
while b != 0:
|
||||
a, b = b, a % b
|
||||
return a
|
||||
|
||||
|
||||
def quick_is_not_prime(n):
|
||||
'''Does some quick checks to see if we can poke a hole into the primality of n.
|
||||
"""Does some quick checks to see if we can poke a hole into the primality of n.
|
||||
|
||||
A result of `False` does **not** mean that the number is prime; it just means
|
||||
that we could not detect quickly whether it is not prime.
|
||||
'''
|
||||
"""
|
||||
if n <= 2:
|
||||
return n < 2
|
||||
# The constant in the next line is the product of all primes < 200
|
||||
@@ -52,9 +52,52 @@ def quick_is_not_prime(n):
|
||||
if n < 200 and gcd == n:
|
||||
# Explicitly check for all primes < 200
|
||||
return n not in (
|
||||
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83,
|
||||
89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179,
|
||||
181, 191, 193, 197, 199,
|
||||
2,
|
||||
3,
|
||||
5,
|
||||
7,
|
||||
11,
|
||||
13,
|
||||
17,
|
||||
19,
|
||||
23,
|
||||
29,
|
||||
31,
|
||||
37,
|
||||
41,
|
||||
43,
|
||||
47,
|
||||
53,
|
||||
59,
|
||||
61,
|
||||
67,
|
||||
71,
|
||||
73,
|
||||
79,
|
||||
83,
|
||||
89,
|
||||
97,
|
||||
101,
|
||||
103,
|
||||
107,
|
||||
109,
|
||||
113,
|
||||
127,
|
||||
131,
|
||||
137,
|
||||
139,
|
||||
149,
|
||||
151,
|
||||
157,
|
||||
163,
|
||||
167,
|
||||
173,
|
||||
179,
|
||||
181,
|
||||
191,
|
||||
193,
|
||||
197,
|
||||
199,
|
||||
)
|
||||
return True
|
||||
# TODO: maybe do some iterations of Miller-Rabin to increase confidence
|
||||
@@ -83,6 +126,7 @@ if python_version >= (2, 7) or python_version >= (3, 1):
|
||||
if no == 0:
|
||||
return 0
|
||||
return no.bit_length()
|
||||
|
||||
else:
|
||||
# Slow, but works
|
||||
def count_bytes(no):
|
||||
@@ -107,25 +151,27 @@ else:
|
||||
count += 1
|
||||
return count
|
||||
|
||||
|
||||
if sys.version_info[0] >= 3:
|
||||
# Python 3 (and newer)
|
||||
def _convert_int_to_bytes(count, no):
|
||||
return no.to_bytes(count, byteorder='big')
|
||||
return no.to_bytes(count, byteorder="big")
|
||||
|
||||
def _convert_bytes_to_int(data):
|
||||
return int.from_bytes(data, byteorder='big', signed=False)
|
||||
return int.from_bytes(data, byteorder="big", signed=False)
|
||||
|
||||
def _to_hex(no):
|
||||
return hex(no)[2:]
|
||||
|
||||
else:
|
||||
# Python 2
|
||||
def _convert_int_to_bytes(count, n):
|
||||
if n == 0 and count == 0:
|
||||
return ''
|
||||
h = '%x' % n
|
||||
return ""
|
||||
h = "%x" % n
|
||||
if len(h) > 2 * count:
|
||||
raise Exception('Number {1} needs more than {0} bytes!'.format(count, n))
|
||||
return ('0' * (2 * count - len(h)) + h).decode('hex')
|
||||
raise Exception("Number {1} needs more than {0} bytes!".format(count, n))
|
||||
return ("0" * (2 * count - len(h)) + h).decode("hex")
|
||||
|
||||
def _convert_bytes_to_int(data):
|
||||
v = 0
|
||||
@@ -134,7 +180,7 @@ else:
|
||||
return v
|
||||
|
||||
def _to_hex(no):
|
||||
return '%x' % no
|
||||
return "%x" % no
|
||||
|
||||
|
||||
def convert_int_to_bytes(no, count=None):
|
||||
@@ -164,7 +210,7 @@ def convert_int_to_hex(no, digits=None):
|
||||
no = abs(no)
|
||||
value = _to_hex(no)
|
||||
if digits is not None and len(value) < digits:
|
||||
value = '0' * (digits - len(value)) + value
|
||||
value = "0" * (digits - len(value)) + value
|
||||
return value
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user