Skip to content

Instantly share code, notes, and snippets.

@Ayrx
Ayrx / scrapecomments.py
Created June 16, 2013 08:23
This is a simple python script to scrape comments from a web page.
#!/usr/bin/env python
import argparse
import urllib
import urlparse
from HTMLParser import HTMLParser
arg_parser = argparse.ArgumentParser()
arg_parser.add_argument('url')
args = arg_parser.parse_args()
@Ayrx
Ayrx / prime_sieve.py
Created June 25, 2013 12:46
Sieve of Eratosthenes
def gen_prime_sieve(n):
# Implementation uses the Sieve of Eratosthenes
# Optimized code from: http://stackoverflow.com/a/3941967/1170681
a = [True] * n
a[0] = a[1] = False
for (i, isprime) in enumerate(a):
if isprime:
@Ayrx
Ayrx / miller_rabin.py
Created June 28, 2013 13:47
Python implementation of the Miller-Rabin Primality Test
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:
@Ayrx
Ayrx / fermat.py
Created June 28, 2013 13:48
Python implementation of the Fermat Primality Test
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 / 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)))
@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 / 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 / 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 / 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 / BcryptCredentialsMatcher.java
Created July 19, 2013 15:30
A hack to get Apache Shiro to work with Bcrypt.
/**
* @author: Terry Chia (Ayrx)
*/
public class BcryptCredentialsMatcher implements CredentialsMatcher {
@Override
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
String password;