Skip to content

Instantly share code, notes, and snippets.

@jvarho
jvarho / ws.py
Created November 8, 2016 04:53
Window Saver
#!/usr/bin/env python2
#
# Copyright (c) 2016, Jan Varho
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
@jvarho
jvarho / enigma.py
Last active June 23, 2016 10:15
Enigma machine emulator, written in Python
alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
class Rotor():
perms = []
turnover_position = ''
position = 'A'
def __init__(self, perms, turnover_position, ring_setting):
i = alphabet.index(ring_setting)
perms = perms[i:] + perms[:i]
self.perms = [c for c in perms]
with open('in.pnm') as f:
data = f.read().split('\n')
data[-1] = bytearray(data[-1])
for i, j in enumerate(data[-1]):
data[-1][i] = (j & 3) * 255 / 3
data[-1] = bytes(data[-1])
with open('out.pnm', 'w') as f:
f.write('\n'.join(data))
@jvarho
jvarho / gist:09baddea8e2cd81f7609
Last active August 29, 2015 14:02
collision test for iterated, truncated sha1
import hashlib
def H(s):
for i in xrange(N):
s = hashlib.sha1(s).hexdigest()[:4]
return s
n = 1000000
for N in range(1, 17):