Skip to main content
Bounty Ended with 50 reputation awarded by Have a nice day
added 125 characters in body
Source Link
  • It's kinda weird that you use i for some loops and j for others when there are no name collisions. Weird, but not horrible.
  • I would probably toss 128 into a constant called block size and put that at the top of this file. BLOCK_SIZE = 128. Then replace all instances of 128 with that constant That way if the block size changes you only need to update it in one place.
  • You could probably wrap this in a class and it would be easier to handle namespacing across files if you end up using this in another python file. Going this route you could just expose encrypt/decrypt functions and make all of the others "private" by prefixing them with _.
    • If you go this way blocksize could be set on the object and doesn't need to be a constant
  • Right now you are only using numpy for the randomness functions. I would suggest using pythons built in random functions for that.
  • It's kinda weird that you use i for some loops and j for others when there are no name collisions. Weird, but not horrible.
  • I would probably toss 128 into a constant called block size and put that at the top of this file. BLOCK_SIZE = 128. Then replace all instances of 128 with that constant That way if the block size changes you only need to update it in one place.
  • You could probably wrap this in a class and it would be easier to handle namespacing across files if you end up using this in another python file. Going this route you could just expose encrypt/decrypt functions and make all of the others "private" by prefixing them with _.
    • If you go this way blocksize could be set on the object and doesn't need to be a constant
  • Right now you are only using numpy for the randomness functions. I would suggest using pythons built in random functions for that.
  • It's kinda weird that you use i for some loops and j for others when there are no name collisions. Weird, but not horrible.
  • I would probably toss 128 into a constant called block size and put that at the top of this file. BLOCK_SIZE = 128. Then replace all instances of 128 with that constant That way if the block size changes you only need to update it in one place.
  • You could probably wrap this in a class and it would be easier to handle namespacing across files if you end up using this in another python file. Going this route you could just expose encrypt/decrypt functions and make all of the others "private" by prefixing them with _.
    • If you go this way blocksize could be set on the object and doesn't need to be a constant
  • Right now you are only using numpy for the randomness functions. I would suggest using pythons built in random functions for that.
Source Link

Is that just for type hints?

Correct, everything in the typing module is used for type hints. These are useful when using something like mypy, but the python interpreter will just ignore all of these.

Would it be faster to use numpy arrays instead of python lists of lists?

Theoretically, yes but it depends on the size of the data. numpy is really meant for processing large data sets, and I imagine most things you'd use this for is pretty small where the overhead would wipe away any speedup.

Aside from those follow up questions, if you see anything needing to be changed, let me now.

def generate_key(key: str) -> bytes:
    if len(key) >= 128:
        key = key.encode()
        return key[:128]
    elif len(key) < 128:
        
        key = key.encode()
        
        for i in range(128 - len(key)):
            key = key + bytes([(sum(key) // len(key)) ^ sum(1 << (8 - 1 - j) for j in range(8) if key[i] >> j & 1)])
        
        decimal = ''.join(str(i) for i in key)
        
        binary = f'{bin(int(decimal[len(decimal) // 2:] + decimal[:len(decimal) // 2]))[2:]:<01024s}'
        
        key = bin_to_bytes(binary[:1024])
        half1 = key[:len(key) // 2]
        half2 = key[len(key) // 2:]
        key = half2 + half1
        return key[:128]

The elif here is unnecessary. You could remove some of the indentation by just omitting the elif statement here.

def generate_key(key: str) -> bytes:
    if len(key) >= 128:
        key = key.encode()
        return key[:128]
        
    key = key.encode()
    
    for i in range(128 - len(key)):
        key = key + bytes([(sum(key) // len(key)) ^ sum(1 << (8 - 1 - j) for j in range(8) if key[i] >> j & 1)])
    
    decimal = ''.join(str(i) for i in key)
    
    binary = f'{bin(int(decimal[len(decimal) // 2:] + decimal[:len(decimal) // 2]))[2:]:<01024s}'
    
    key = bin_to_bytes(binary[:1024])
    half1 = key[:len(key) // 2]
    half2 = key[len(key) // 2:]
    key = half2 + half1
    return key[:128]
  • It's kinda weird that you use i for some loops and j for others when there are no name collisions. Weird, but not horrible.
  • I would probably toss 128 into a constant called block size and put that at the top of this file. BLOCK_SIZE = 128. Then replace all instances of 128 with that constant That way if the block size changes you only need to update it in one place.
  • You could probably wrap this in a class and it would be easier to handle namespacing across files if you end up using this in another python file. Going this route you could just expose encrypt/decrypt functions and make all of the others "private" by prefixing them with _.
    • If you go this way blocksize could be set on the object and doesn't need to be a constant
  • Right now you are only using numpy for the randomness functions. I would suggest using pythons built in random functions for that.