Skip to content

Instantly share code, notes, and snippets.

View kurtbrose's full-sized avatar
💭
carbon based

Kurt Rose kurtbrose

💭
carbon based
View GitHub Profile
@kurtbrose
kurtbrose / key_wrap.py
Last active December 28, 2022 13:15
implementation of RFC 3394 AES key wrapping/unwrapping
'''
Key wrapping and unwrapping as defined in RFC 3394.
Also a padding mechanism that was used in openssl at one time.
The purpose of this algorithm is to encrypt a key multiple times to add an extra layer of security.
Personally, I wouldn't recommend using this for most applications.
Just use AES/mode CTR to encrypt your keys, the same as you would any other data.
The time to use this code is when you need compatibility with another system that implements the RFC.
(For example, these functions are compatible with the openssl functions of the same name.)
@kurtbrose
kurtbrose / elliptic.py
Created December 31, 2012 22:58
Implementations of some basic elliptic curve cryptography primitives. Behavior specified by SECG in SEC1 version 1 and 2. Also ANSI X9.63. Curves specified from SEC2 version 1 and 2, and NIST "Recommended Elliptic Curves for Federal Government Use". See: http://en.wikipedia.org/wiki/Elliptic_curve_cryptography and http://www.johannes-bauer.com/c…
'''
Implementations of some basic elliptic curve cryptography primitivese.
Behavior specified by SECG in SEC1 version 1 and 2.
Also ANSI X9.63.
Curves specified from SEC2 version 1 and 2,
and NIST "Recommended Elliptic Curves for Federal Government Use".
See: http://en.wikipedia.org/wiki/Elliptic_curve_cryptography and
@kurtbrose
kurtbrose / gist:4694266
Created February 1, 2013 21:26
tar over scp -- handy command much faster than scp -r if you have many small files
$ tar cf - directory | ssh username@host tar xf - -C /tmp/
@kurtbrose
kurtbrose / gist:4720463
Last active December 12, 2015 05:18
metaclass variable resolution behavior
>>> class A(type):
... @property
... def a(cls): return 4
...
>>> class B1(object):
... __metaclass__ = A
... a = 3
...
>>> B1.a
4
@kurtbrose
kurtbrose / gist:5148795
Created March 13, 2013 01:57
IndexedSet
from bisect import bisect_left, insort
from itertools import ifilter
#maintain invariant that dead items are only in the middle?
class IndexedSet(object):
def __init__(self, other=None):
self.item_index_map = dict()
self.item_list = []
self.dead_indices = []
if other:
@kurtbrose
kurtbrose / tree.py
Last active December 14, 2015 22:28
working on a tree list
from itertools import repeat
class Tree(object):
def __init__(self):
self.tree = []
self.balance = bytearray('')
def insert(self, item):
i = self._get_index(item)
if i >= len(self.tree):
@kurtbrose
kurtbrose / tree.py
Last active December 14, 2015 23:38
decent python AVL tree implementation
class Tree(object):
#0 = item
#1 = left
#2 = right
#3 = height
def __init__(self):
self.root = None
@kurtbrose
kurtbrose / jks.py
Last active December 17, 2015 09:49 — forked from anonymous/jks.py
'''
JKS file format decoder.
Use in conjunction with PyOpenSSL to translate to PEM, or load private key and certs
directly into openssl structs and wrap sockets.
See http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/sun/security/provider/JavaKeyStore.java#JavaKeyStore.engineLoad%28java.io.InputStream%2Cchar%5B%5D%29
'''
import struct
import hashlib
@kurtbrose
kurtbrose / sockshare.py
Created June 9, 2013 07:31
work in progress ctypes back-port of windows socket sharing from Python 3.3 to Python 2.7; eventual use case is cross-process socket pool; maybe even fancier things as well
import ctypes
import struct
import socket
WIN32 = True
if WIN32:
def pass_sock():
pass
@kurtbrose
kurtbrose / gevent_profile_test.py
Last active December 18, 2015 13:59
demonstration gevent and line_profiler playing nicely together (note: gevent.sleep(0) counts as all the time it spends waiting; if the primary case of sleeps is waiting for network or CPU bound tasks, this is valid; e.g. under high load this profiling will start to break down)
import gevent
import hashlib
import os
import line_profiler
profiler = line_profiler.LineProfiler()
@profiler