Created
April 7, 2025 18:57
-
-
Save bit-hack/6a5b63ebf75451e39a49a022606defa1 to your computer and use it in GitHub Desktop.
gatekeeper
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from tkinter import * | |
PWD_LENGTH = 16 | |
tokens = ''.join(( | |
'abcdefghijklmnopqrstuvwxyz', | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ', | |
'0123456789')) | |
def to_uint64(x): | |
return x & 0xffffffffffffffff | |
def to_uint32(x): | |
return x & 0xffffffff | |
class generator(object): | |
def __init__(self): | |
self.seed = 1 | |
def next_char(self): | |
# xor shift | |
self.seed = to_uint64(self.seed) | |
self.seed ^= to_uint64(self.seed >> 12) | |
self.seed ^= to_uint64(self.seed << 25) | |
self.seed ^= to_uint64(self.seed >> 27) | |
# whiten | |
v = to_uint64(self.seed * 2685821657736338717) | |
# bring down into lower bits | |
v = to_uint32((v >> 24) ^ (v >> 16) ^ (v >> 8) ^ v) | |
# 32bit abs it | |
v = ((v ^ 0xffffffff) + 1) if v & 0x80000000 else v | |
# index into token array | |
return tokens[v % len(tokens)] | |
def run(self, a, b): | |
self.seed = 1 | |
al = len(a) | |
bl = len(b) | |
assert (al > 0 and bl > 0) | |
maxl = max(al, bl) | |
for i in range(maxl): | |
oa = ord(a[i % al]) | |
ob = ord(b[i % bl]) | |
self.seed = to_uint64(self.seed * oa + ob) | |
out = '' | |
for i in range(PWD_LENGTH): | |
out += self.next_char() | |
return out | |
def self_check(): | |
pwd = generator() | |
assert (pwd.run('test', 'hello_there') == 'gbueMBr6PZTfwsRF') | |
assert (pwd.run('more_testing', 'foobar') == 'IkiNM6u1jNUH4o90') | |
def on_clear(args): | |
root, ent_pwd, ent_url = args | |
ent_pwd.delete(0, END) | |
ent_url.delete(0, END) | |
def on_copy(args): | |
root, ent_pwd, ent_url = args | |
pwd = generator() | |
out = pwd.run(ent_pwd.get(), ent_url.get()) | |
root.clipboard_clear() | |
root.clipboard_append(out) | |
ent_pwd.delete(0, END) | |
ent_url.delete(0, END) | |
def main(): | |
root = Tk() | |
root.wm_title('gatekeeper') | |
ent_pwd = Entry(root, show='*') | |
ent_pwd.pack(side=TOP) | |
ent_url = Entry(root) | |
ent_url.pack(side=TOP) | |
but = Button(root, | |
text='clear', | |
width=10, | |
command=lambda: on_clear((root, | |
ent_pwd, | |
ent_url))) | |
but.pack(side=LEFT) | |
but = Button(root, | |
text='copy', | |
width=10, | |
command=lambda: on_copy((root, | |
ent_pwd, | |
ent_url))) | |
but.pack(side=LEFT) | |
# enter tkinter main loop | |
mainloop() | |
self_check() | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment