#!/usr/bin/python
import md5, string

# Kazzy 0.1
# Copyright (C) 2007 rampo and ascii @ ush.it, under the WTFPL
#
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#                    Version 2, December 2004
#
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
#            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
#   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
#  0. You just DO WHAT THE FUCK YOU WANT TO.

# Lewis, Goodman & Miller minimal standard
# Barone et al., Programmazione scientifica (Pearson), p. 282
MAGIC_MUL = 16807
MAGIC_MOD = 0x7FFFFFFF # 2^31 - 1
PORT_MOD  = 65535

# Passphrase to port sequence
def pass2ports(passphrase, nports):
        hexDigits = md5.md5(passphrase).hexdigest()
        randGen = []       # 4 pseudo-random number generators
        for i in range(4):
                randGen.append( string.atoi(hexDigits[8*i : 8*(i+1)], 16) )
        ports = []
        for k in range(nports):
                randGen[k%4] = (randGen[k%4]*MAGIC_MUL)%MAGIC_MOD
                ports.append(randGen[k%4]%PORT_MOD + 1) # ports from 1 to 65535
        return ports

print pass2ports("ciao", 12)

