Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Ayrx / constant_compare.py
Created November 29, 2013 11:53
Function for constant time string comparison.
def is_equal(a, b):
if len(a) != len(b):
return False
result = 0
for x, y in zip(a, b):
result |= x ^ y
return result == 0
@Ayrx
Ayrx / enum.py
Created October 10, 2013 03:17
Code snippet for using enum in Python 2. http://stackoverflow.com/a/1695250/1170681
def enum(**enums):
return type('Enum', (), enums)
@Ayrx
Ayrx / case_sensitive_path.py
Created September 12, 2013 11:51
A code snippet to find the case sensitive version of path name on the system given a case insensitive version.
import os
def get_case_sensitive_pathname(path, top):
for root, dirs, files in os.walk(top):
for d in dirs:
if os.path.join(root, d).lower() == path.lower():
return os.path.join(root, d)
for f in files:
if os.path.join(root, f).lower() == path.lower():
return os.path.join(root, f)
@Ayrx
Ayrx / getenvaddr.c
Created August 28, 2013 05:49
A simple piece of C code to print out the memory address of a specific environmental variable.
#include <stdlib.h>
#include <stdio.h>
int main(int argc, char *argv[]) {
char *addr;
if (argc < 2) {
printf("Usage:\n%s <environment variable name>\n", argv[0]);
exit(0);
}
@Ayrx
Ayrx / safeLongToInt.java
Created August 20, 2013 13:53
A function to safely cast a long to an int without relying on any external libraries. From the brilliant Jon Skeet @ http://stackoverflow.com/questions/1590831/safely-casting-long-to-int-in-java
public static int safeLongToInt(long l) {
if (l < Integer.MIN_VALUE || l > Integer.MAX_VALUE) {
throw new IllegalArgumentException
(l + " cannot be cast to int without changing its value.");
}
return (int) l;
}
@Ayrx
Ayrx / vuln_scanner.py
Created July 17, 2013 14:00
The most awesomest scanner in the whole world!
import os
for r,d,f in os.walk("/var/www"):
for files in f:
if files.endswith(".php"):
print "You are fucked!"
@Ayrx
Ayrx / pythagorean_triplets.py
Created July 13, 2013 08:16
A generator for Pythagorean triplets.
def pythagorean_triplets(n):
for i in xrange(1, n):
j = i+1
k = j+1
while k <= n:
while k*k < (i*i) + (j*j):
k += 1
if k*k == (i*i) + (j*j) and k <= n:
@Ayrx
Ayrx / lcm.py
Created July 12, 2013 12:47
LCM function for python.
def lcm(*numbers):
"""Return lowest common multiple."""
def lcm(a, b):
return (a * b) // gcd(a, b)
return reduce(lcm, numbers, 1)
@Ayrx
Ayrx / prime_factorize.py
Created July 11, 2013 13:26
Prime factorization snippet
def prime_factors(n):
"""Returns all the prime factors of a positive integer"""
factors = []
d = 2
while n > 1:
while n % d == 0:
factors.append(d)
n /= d
d += 1
if d*d > n:
@Ayrx
Ayrx / factorize.py
Created July 11, 2013 12:57
Factorization snippet
def factors(n):
return set(reduce(list.__add__,
([i, n // i] for i in range(1, int(n ** 0.5) + 1) if n % i == 0)))