mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-07-05 01:09:40 +08:00
fix(mypy): Fix annotations for 13 cipher algorithms (#4278)
* Initial fix for mypy errors in some cipher algorithms * fix(mypy): Update type hints * fix(mypy): Update type hints for enigma_machine2.py * Update as per the suggestion Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: Christian Clauss <cclauss@me.com>
This commit is contained in:
@ -2,24 +2,18 @@ import os
|
||||
import random
|
||||
import sys
|
||||
|
||||
from . import cryptomath_module as cryptoMath
|
||||
from . import rabin_miller as rabinMiller
|
||||
from . import cryptomath_module as cryptomath
|
||||
from . import rabin_miller
|
||||
|
||||
min_primitive_root = 3
|
||||
|
||||
|
||||
def main():
|
||||
print("Making key files...")
|
||||
makeKeyFiles("elgamal", 2048)
|
||||
print("Key files generation successful")
|
||||
|
||||
|
||||
# I have written my code naively same as definition of primitive root
|
||||
# however every time I run this program, memory exceeded...
|
||||
# so I used 4.80 Algorithm in
|
||||
# Handbook of Applied Cryptography(CRC Press, ISBN : 0-8493-8523-7, October 1996)
|
||||
# and it seems to run nicely!
|
||||
def primitiveRoot(p_val: int) -> int:
|
||||
def primitive_root(p_val: int) -> int:
|
||||
print("Generating primitive root of p")
|
||||
while True:
|
||||
g = random.randrange(3, p_val)
|
||||
@ -30,20 +24,20 @@ def primitiveRoot(p_val: int) -> int:
|
||||
return g
|
||||
|
||||
|
||||
def generateKey(keySize: int) -> ((int, int, int, int), (int, int)):
|
||||
def generate_key(key_size: int) -> tuple[tuple[int, int, int, int], tuple[int, int]]:
|
||||
print("Generating prime p...")
|
||||
p = rabinMiller.generateLargePrime(keySize) # select large prime number.
|
||||
e_1 = primitiveRoot(p) # one primitive root on modulo p.
|
||||
p = rabin_miller.generateLargePrime(key_size) # select large prime number.
|
||||
e_1 = primitive_root(p) # one primitive root on modulo p.
|
||||
d = random.randrange(3, p) # private_key -> have to be greater than 2 for safety.
|
||||
e_2 = cryptoMath.findModInverse(pow(e_1, d, p), p)
|
||||
e_2 = cryptomath.find_mod_inverse(pow(e_1, d, p), p)
|
||||
|
||||
publicKey = (keySize, e_1, e_2, p)
|
||||
privateKey = (keySize, d)
|
||||
public_key = (key_size, e_1, e_2, p)
|
||||
private_key = (key_size, d)
|
||||
|
||||
return publicKey, privateKey
|
||||
return public_key, private_key
|
||||
|
||||
|
||||
def makeKeyFiles(name: str, keySize: int):
|
||||
def make_key_files(name: str, keySize: int) -> None:
|
||||
if os.path.exists("%s_pubkey.txt" % name) or os.path.exists(
|
||||
"%s_privkey.txt" % name
|
||||
):
|
||||
@ -55,7 +49,7 @@ def makeKeyFiles(name: str, keySize: int):
|
||||
)
|
||||
sys.exit()
|
||||
|
||||
publicKey, privateKey = generateKey(keySize)
|
||||
publicKey, privateKey = generate_key(keySize)
|
||||
print("\nWriting public key to file %s_pubkey.txt..." % name)
|
||||
with open("%s_pubkey.txt" % name, "w") as fo:
|
||||
fo.write(
|
||||
@ -67,5 +61,11 @@ def makeKeyFiles(name: str, keySize: int):
|
||||
fo.write("%d,%d" % (privateKey[0], privateKey[1]))
|
||||
|
||||
|
||||
def main() -> None:
|
||||
print("Making key files...")
|
||||
make_key_files("elgamal", 2048)
|
||||
print("Key files generation successful")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
Reference in New Issue
Block a user