import hashlib
import cfg

def fast_exponentiation(a, p, n):
    "calculates a^p mod n"
    r = a % n
    b = []
    while p != 1:
        b.append(p & 1)
        p = p >> 1
    while b:
        r = ((a ** b.pop()) * r ** 2) % n
    return r

def hash(message):
    hash = [hashlib.sha512(message).hexdigest()]
    for i in range(6):
        hash.append(hashlib.sha512(hash[-1]).hexdigest())
    return int(''.join(hash), 16)

def decrypt(x):
    return fast_exponentiation(x, cfg.updates.rsa_e, cfg.updates.rsa_n)
