started lab5
This commit is contained in:
parent
60ebd2b53c
commit
f4f4b53f24
4 changed files with 41 additions and 0 deletions
31
IS/Lab/Lab5/customhash.py
Normal file
31
IS/Lab/Lab5/customhash.py
Normal file
|
@ -0,0 +1,31 @@
|
|||
import time
|
||||
|
||||
MASK32 = 0xFFFFFFFF
|
||||
|
||||
def hash_gen(s: str, h: int, mult: int):
|
||||
"""Compute hash of the whole string, print it each loop iteration, repeat."""
|
||||
try:
|
||||
while True:
|
||||
cur = h & MASK32
|
||||
for ch in s:
|
||||
cur = (cur * mult + ord(ch)) & MASK32
|
||||
cur ^= (cur >> 16)
|
||||
cur = (cur * 0x85ebca6b) & MASK32
|
||||
cur ^= (cur >> 13)
|
||||
print(f"String: {s!r} Hash: {cur:#010x}")
|
||||
h = cur
|
||||
time.sleep(2)
|
||||
except KeyboardInterrupt:
|
||||
print("\nStopped.")
|
||||
|
||||
def main():
|
||||
hash_init = int(input("Enter initial Hash value: "))
|
||||
hash_mult = int(input("Enter Hash Multiplier: "))
|
||||
|
||||
s = input("Enter String to Hash: ")
|
||||
print("Welcome to the Hash Generator™: ")
|
||||
hash_gen(s, hash_init, hash_mult)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
Loading…
Add table
Add a link
Reference in a new issue