from hashlib import sha1
import struct

def hash(word, salt):
    return {"*%s"%_scramble(word,salt):word}

def _scramble(password, salt):
    stage1 = sha1(password).digest()
    stage2 = sha1(stage1).digest()
    s = sha1()
    s.update(salt)
    s.update(stage2)
    result = s.digest()
    return _my_crypt(result, stage1)

def _my_crypt(message1, message2):
    length = len(message1)
    result = struct.pack('B', length)
    for i in xrange(length):
        x = (struct.unpack('B', message1[i:i+1])[0] ^ \
             struct.unpack('B', message2[i:i+1])[0])
        result += struct.pack('B', x)
    return result

