Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Ayrx / safeLongToInt.java
Created Aug 20, 2013
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
View safeLongToInt.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 / IntegerArrayType.java
Created Aug 6, 2013
A custom UserType that enables Hibernate to map a Java integer array to a PostgreSQL integer array.
View IntegerArrayType.java
public class IntegerArrayType implements UserType {
protected static final int SQLTYPE = java.sql.Types.ARRAY;
@Override
public Object nullSafeGet(final ResultSet rs, final String[] names, final SessionImplementor sessionImplementor, final Object owner) throws HibernateException, SQLException {
Array array = rs.getArray(names[0]);
Integer[] javaArray = (Integer[]) array.getArray();
return ArrayUtils.toPrimitive(javaArray);
}
@Ayrx
Ayrx / BcryptCredentialsMatcher.java
Created Jul 19, 2013
A hack to get Apache Shiro to work with Bcrypt.
View BcryptCredentialsMatcher.java
/**
* @author: Terry Chia (Ayrx)
*/
public class BcryptCredentialsMatcher implements CredentialsMatcher {
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
String password;
@Ayrx
Ayrx / vuln_scanner.py
Created Jul 17, 2013
The most awesomest scanner in the whole world!
View vuln_scanner.py
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 Jul 13, 2013
A generator for Pythagorean triplets.
View pythagorean_triplets.py
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 Jul 12, 2013
LCM function for python.
View lcm.py
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 Jul 11, 2013
Prime factorization snippet
View prime_factorize.py
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 Jul 11, 2013
Factorization snippet
View factorize.py
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)))
@Ayrx
Ayrx / fermat.py
Created Jun 28, 2013
Python implementation of the Fermat Primality Test
View fermat.py
def fermat_test(n, k):
# Implementation uses the Fermat Primality Test
# If number is even, it's a composite number
if n == 2:
return True
if n % 2 == 0:
@Ayrx
Ayrx / miller_rabin.py
Created Jun 28, 2013
Python implementation of the Miller-Rabin Primality Test
View miller_rabin.py
def miller_rabin(n, k):
# Implementation uses the Miller-Rabin Primality Test
# The optimal number of rounds for this test is 40
# See http://stackoverflow.com/questions/6325576/how-many-iterations-of-rabin-miller-should-i-use-for-cryptographic-safe-primes
# for justification
# If number is even, it's a composite number
if n == 2: