#!/usr/bin/python sig=""" -----BEGIN PGP SIGNED MESSAGE----- # This program implements a hash function based entirely on modular # exponentiation which was proposed by Adi Shamir. A prove of # collision-resistance was presented by Ronald L. Rivest. # (see: http://senderek.de/SDLH for details) # # The code was written by Ralf Senderek (27/7/2003) # This is free software. # Use this software on your own risk or not at all. # There is NO warranty; not even for MERCHANTABILITY # or FITNESS FOR A PARTICULAR PURPOSE. """ import sys HashModulus = 127591803097286953893100494339441011348391052328504594272155043892693303336312481545545475054278478854524034312374432499270105554744411485832351021152505745773538402718187183756998476794675330042232393745790032655631295437331043707963163673832402724061250274903083004931263395006053506534674232415216812527609L Min_Hashmodulus = 1024 HashModulusLength = 1024 Prime1 = 102639592829741105772054196573991675900716567808038066803341933521790711307779L Prime2 = 106603488380168454820927220360012878679207958575989291522270608237193062808643L # Both primes were taken from the RSA-155 factoring challenge Aug. 1999 Generator = Prime1 * Prime2 # 512 Bits GeneratorLength = 512 ################################################################ def ModExp (Base, Exp, Mod): Hash = 1 X = Exp Factor = Base while X > 0 : Remainder = X % 2 X = X / 2 if Remainder == 1: Hash = Hash * Factor % Mod Factor = Factor * Factor % Mod return Hash ################################################################ def slow_SDLH(Message) : Number = 0L index = 0 while index < len(Message) : Number = Number * 256 + ord(Message[index]) index = index + 1 return ModExp(Generator, Number, HashModulus) ################################################################ def SDLH(Message) : # Calculates hash(x) = Generator^x mod HashModulus # # Speeding up the computation by avoiding to convert the string into # a long integer # This code processes characters one by one using # two ModExp()-operations for every character # Hash = 1 # for Character in Message: # (1) A = ModExp(Generator, ord(Character), HashModulus) # (2) B = ModExp(Hash, 256, HashModulus) # (3) Hash = (A * B) % HashModulus # return Hash # Implementation with precomputed table # precomputing table for step (1) table = [] table.append(1L) index = 1 Number = 1L while index < 256 : Number = (Number * Generator) % HashModulus table.append(Number) index = index + 1 # process the message character by character Hash = 1 for Character in Message: A = table[ord(Character)] # step (1) B = Hash # step (2) index = 8 while index > 0 : B = (B * B) % HashModulus index = index - 1 Hash = (A * B) % HashModulus # step (3) return Hash ################################################################ def SDLH256(Message) : Hash256 = 0L X = SDLH(Message) Size = pow(2,256L) while X > 0 : Section = X % Size X = X / Size Hash256 = Hash256 ^ Section # XOR operation return Hash256 ################################################################ def read_keyfile(FileName) : import string global HashModulus, HashModulusLength, Generator, GeneratorLength global UserID try: FILE = open(FileName, "r") Key = FILE.readlines() FILE.close() except : print "Cannot open keyfile ", FileName sys.exit(2) if len(Key) >= 3 : try: if string.upper(Key[0][:11]) == "HASHMODULUS" and string.upper(Key[1][:9]) == "GENERATOR" : HashModulus = long(Key[0][14:-1]) X = HashModulus HashModulusLength = 0 while X > 0 : X = X / 2 HashModulusLength = HashModulusLength + 1 Generator = long(Key[1][12:-1]) GeneratorLength = 0 X = Generator while X > 0 : X = X / 2 GeneratorLength = GeneratorLength + 1 if HashModulusLength < Min_Hashmodulus : print "The Hashmodulus is too short!" sys.exit(2) if 2*GeneratorLength < Min_Hashmodulus : print "The Generator is too short!" sys.exit(2) else : error() UserID = Key[2][:-1] + " (" + str(HashModulusLength) + "," + str(GeneratorLength) + ")" except: print "The neccessary key information is not available." sys.exit(2) else: error() sys.exit(2) ################################################################ def print_hash(Message, Filename) : if Bits == 256 : Hash = SDLH256(Text) else : if Mode == "fast" : Hash = SDLH(Text) else : Hash = slow_SDLH(Text) print Hash, Filename, print Bits, "bit" print ################################################################ def error() : print "The keyfile is corrupt.", print "It must consist of 3 lines (for example)." print "" print "Hashmodulus = 342345" print "Generator = 323471" print "User Identification String" print "" sys.exit(2) ################################################################ # MAIN # ################################################################ Bits = HashModulusLength Mode = "fast" Key = "default" UserID = "None" HOME = "." # look for a config file with hashkey information. import os if os.name == "posix": HOME = os.environ['HOME'] if os.path.exists(HOME + "/.hashkey") : read_keyfile(HOME + "/.hashkey") Key = "config" else: print "No config file found." if len(sys.argv) > 0 : if len(sys.argv) > 1 : if sys.argv[1] == "-256" : Bits = 256 del sys.argv[1] if (len(sys.argv) > 1) and (sys.argv[1] == "-slow") : print "Slow hashing ..." Mode = "slow" del sys.argv[1] if (len(sys.argv) > 1 ) and (sys.argv[1] == "-key") and (len(sys.argv) >= 3) : read_keyfile(sys.argv[2]) Key = "file" del sys.argv[1:3] if Bits != 256 : Bits = HashModulusLength if Key == "config": print "Using config file." if Key == "file": print "Using key file." if Key == "default": print "Using default values." print "User: ", UserID if (len(sys.argv) == 1) or (sys.argv[1] == "-"): F = sys.stdin Text = F.read() F.close() print_hash(Text, "stdin") sys.exit(0) else : for File in sys.argv[1:] : try: F = open(File , "r") Text = F.read() F.close() print_hash(Text, File) except: print "Cannot open file ", File else : print "usage: sdlh [-256] [-slow] [-key keyfile] file" sys.exit(0) sig=""" -----BEGIN PGP SIGNATURE----- Version: 2.6.3ia Charset: noconv iQEVAwUBPySviL6wVDeIE49tAQHkWggAkKKxJXBWX5RzQd0oMXPGGk3ojbTDbsxR QusOt8yDaraHgLvnDNgss105y+/JUoTrvF75qbDIYO1lQ2VhKMB9rMmoBQGNzlWp /pQ7tbj0SkqtG57javsjh6I82ysWWJoj8rSixj+7wWJJhc+JWliAjsyaLhDppFyd UM8+Uh7MPuZ5gKeMrd9uNu5UsC8aGvaoFxv6TYVJb4gHwnhz98iJqSBcEtEzmAnI OQ3pJFlzfstsLWGsLRXGYvYRHaXVpU1n5xnPsxv/2BGkUSdZi+kL0LckOFfuuYIW VT8N/0OVadDWdqffWGp/tk+fvqMV78ZjyMTx67GEmkfwGxe2f6l3qA== =vyNl -----END PGP SIGNATURE----- """