Skip to content

Instantly share code, notes, and snippets.

@michalc
michalc / postman-hawk.js
Last active September 21, 2021 05:44
Postman pre-request script for Hawk authentication in custom header
/*****************************************************************************/
const hawkId = pm.variables.get('hawk_id');
const hawkKey = pm.variables.get('hawk_key');
const hawkHeader = pm.variables.get('hawk_header') || 'authorization';
/*****************************************************************************/
const timestamp = parseInt(new Date().getTime() / 1000);
const nonce = CryptoJS.enc.Base64.stringify(CryptoJS.lib.WordArray.random(6));
@michalc
michalc / libcrypto-decrypt-aes-ctr-little-endian.py
Last active September 13, 2021 05:27
Use libcrypto (OpenSSL) directly from Python with ctypes without compiling anything: AES decrypt with a little endian CTR counter
from contextlib import contextmanager
from ctypes import POINTER, cdll, c_char_p, c_void_p, c_int, create_string_buffer, byref
from sys import platform
# Uses a _little_ endian CTR counter, which OpenSSL doesn't directly support.
# Could be used to decrypt AES-encrypted ZIP files
def decrypt_aes_256_ctr_little_endian(
key, ciphertext_chunks,
get_libcrypto=lambda: cdll.LoadLibrary({'linux': 'libcrypto.so', 'darwin': 'libcrypto.dylib'}[platform])
):
@michalc
michalc / spec.json
Last active July 4, 2021 20:15
World Bank + Vega-lite
{
"$schema": "https://vega.github.io/schema/vega-lite/v5.json",
"width": 630,
"height": 630,
"data": {
"values": [
{
"type": "Feature",
"id": 0,
"geometry": {
@michalc
michalc / table_csv_view.py
Created March 7, 2019 09:19
Django + gevent + psycopg2 download whole PostgreSQL table as CSV
import csv
import logging
import gevent
from psycopg2 import (
connect,
sql,
)
from django.conf import (
@michalc
michalc / getaddrinfo_via_libc.py
Last active December 2, 2018 19:51
Own implementation of getaddrinfo by calling libc directly from Python + ctypes, supporting IPv4 and IPv6 (experimental)
from ctypes import (
byref,
c_char_p, c_uint32, c_uint16, c_ubyte,
CDLL,
POINTER,
pointer,
Structure, Union,
sizeof,
)
import platform
@michalc
michalc / path_lock_global_read_write.py
Last active November 25, 2018 09:17
Path lock using a global read/write lock
import asyncio
import contextlib
from fifolock import FifoLock
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / path_lock_local_read_write.py
Last active November 25, 2018 09:15
Path lock using a read/write lock on each ancestor path
import asyncio
import contextlib
import weakref
from fifolock import FifoLock
class Read(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / path_lock_local_read_write_ancestor.py
Last active November 25, 2018 09:14
Path lock using a read/write/ancestor lock on each ancestor path
import asyncio
import contextlib
import weakref
from fifolock import FifoLock
class ReadAncestor(asyncio.Future):
@staticmethod
def is_compatible(holds):
return not holds[Write]
@michalc
michalc / path_lock_global_exclusive.py
Last active November 22, 2018 19:23
Path lock using a global exclusive lock
import asyncio
import contextlib
class PathLock():
def __init__(self):
self.lock = asyncio.Lock()
@contextlib.asynccontextmanager
async def __call__(self, read, write):
@michalc
michalc / aws_sig_v4_headers_aiohttp_s3_put.py
Created October 28, 2018 10:37
PUTting an object to S3 using aiohttp and aws_sig_v4_headers
import asyncio
import os
import aiohttp
from aws_sig_v4_headers import aws_sig_v4_headers
async def main():
method = 'PUT'
host = 's3-eu-west-1.amazonaws.com'