Other articles


  1. Quick'n'Dirty DNS Swapper

    NOTE: This isn't plug-and-play production software—it illustrates a quick and dirty way to swap between OpenDNS and TorDNS using a locally installed copy of Dnsmasq. Tested with Python 2.7.5 on Debian jessie.

    Caveats: The script's owner (weilawei), group (defaults to the owner), home directory (/home/weilawei/code …

    read more
  2. Simple XOR Hash

    NOTE: I originally posted this on Snipplr.

    This is a simple hash that pads its input to the block size and XORs every block together. Output is in hexadecimal octets. Probability of collisions is extremely high and they are easy to calculate, although the function is one-way, so this is …

    read more
  3. ARC4

    NOTE: I originally posted this on Snipplr.

    This is a pure Python implementation of ARC4 as a generator to highlight its nature as a stream cipher. Several improvements can be made, for instance, it could take a nonce, use multiple state spaces (parallelizable), automatically discard the first 4K of the …

    read more
  4. HMAC

    NOTE: I originally posted this on Snipplr.

    To HMAC, pass a hash from Crypto.Hash in PyCrypto. Key should be a bytes object. Returns a bytearray.

    def pad_key(a_hash, a_key):
        block_size = a_hash.digest_size
    
        if (len(a_key) > block_size):
            a_hasher = a_hash.new()
            a_hash.update(a_key)
            return a_hasher.digest()
        elif (len(a_key) < block_size …
    read more
  5. Chunk Data For Streaming

    NOTE: I originally posted this on Snipplr.

    Chunks data into block_size blocks for streaming, adds null padding.

    import math
    
    is_debug = False
    
    def chunk(data, block_size, padding=b'\x00'):
        data_size       = len(data)
        padding_size    = block_size - (data_size % block_size) if (data_size > block_size) else (block_size - data_size)
        is_padded       = (padding_size > 0) and (padding_size != block_size)
        total_blocks    = math.ceil …
    read more

Sections