Skip to content

Instantly share code, notes, and snippets.

@levigross
levigross / equality.clj
Last active January 13, 2023 06:06
Constant Time Comparison functions
; Taken from https://github.com/weavejester/crypto-equality/blob/master/src/crypto/equality.clj
(ns crypto.equality
"Securely test sequences of data for equality.")
(defn eq?
"Test whether two sequences of characters or bytes are equal in a way that
protects against timing attacks. Note that this does not prevent an attacker
from discovering the *length* of the data being compared."
[a b]
@levigross
levigross / djangoratelimit.py
Created November 29, 2010 02:05
Cache based rate limiting in Django
from django.core.cache import cache
from django.http import HttpResponseForbidden
from functools import wraps
from django.utils.decorators import available_attrs
def ratelimit(limit=10,length=86400):
""" The length is in seconds and defaults to a day"""
def decorator(func):
def inner(request, *args, **kwargs):
ip_hash = str(hash(request.META['REMOTE_ADDR']))