diff --git a/src/authur1.py b/src/authur1.py index 39296bc..3f41a61 100755 --- a/src/authur1.py +++ b/src/authur1.py @@ -10,22 +10,75 @@ Since this (auth) hash did not have a name before, I gave it the name 'authur1' """ import math import argparse +from ctypes import c_uint32 -DEFINED_INITIAL = bytearray(bytes([0x52, 0x4f, 0x46, 0x4c])) +# constants for authur1 +SHIFT_LENGTH = 17 +DEFINED_INITIAL = bytes([0x52, 0x4f, 0x46, 0x4c]) -def Q(input: bytearray) -> bytearray: - assert len(input) == 4 # needs to be 32 bit +""" +C implementation for bit rotating: +see https://en.wikipedia.org/wiki/Circular_shift#Implementing_circular_shifts +```c +/* + * Shift operations in C are only defined for shift values which are + * not negative and smaller than sizeof(value) * CHAR_BIT. + * The mask, used with bitwise-and (&), prevents undefined behaviour + * when the shift count is 0 or >= the width of unsigned int. + */ - return input +#include // for uint32_t, to get 32-bit-wide rotates, regardless of the size of int. +#include // for CHAR_BIT -def H(input: bytearray) -> bytearray: +uint32_t rotl32 (uint32_t value, unsigned int count) { + const unsigned int mask = CHAR_BIT * sizeof(value) - 1; + count &= mask; + return (value << count) | (value >> (-count & mask)); +} + +uint32_t rotr32 (uint32_t value, unsigned int count) { + const unsigned int mask = CHAR_BIT * sizeof(value) - 1; + count &= mask; + return (value >> count) | (value << (-count & mask)); +} +``` +""" + +""" +The rotations are tested agains a c implementation and seem to work fine. +""" +# constant value defined in limits.h, it's 8 (bit) on my machine, on yours probably too. +CHAR_BIT = 8 +# python is being a dynamic dumbass, do a 32 bit shift / 4 byte +VALUE_SIZE = 4 +def rotl(value: int, count: int) -> int: + mask: int = CHAR_BIT * VALUE_SIZE - 1; + count &= mask; + return (value << count) | (value >> (-count & mask)); + +def rotr(value: int, count: int) -> int: + mask: int = CHAR_BIT * VALUE_SIZE - 1; + count &= mask; + return (value >> count) | (value << (-count & mask)); + +def Q(input: c_uint32) -> c_uint32: + output: c_uint32 + + output = input ^ (rot_r(input, SHIFT_LENGTH)) + + return output + +def H(input: bytes) -> bytes: return input def main(): - pass + a: str = input() + ai = int(a) + print((ai)) + print((rotr(ai,17))) if __name__ == "__main__": main()