otp.py

Go to the documentation of this file.
00001 ##
00002 #  
00003 # This module provides an Otp object that can be used to get one time
00004 # pad strings.  It should be adequate for situations that don't require
00005 # high security.
00006 # 
00007 # You can call the module directly as a script; it will want the
00008 # number of otp strings to print out and an optional seed number.
00009 # For example, 'python otp.py 20' will print out 20 otp strings.
00010 # 
00011 # You may pass in a function to the constructor.  This function takes
00012 # an integer parameter (defaults to 0) and must return a string.
00013 # This string is hashed with the MD5 algorithm and the hex
00014 # representation of the hash is returned.  If you do not pass in a
00015 # string generating function, an internal function is used that is
00016 # based on the whrandom module.
00017 # 
00018 # Once you have constructed an Otp object, call the Get() method to
00019 # return an OTP string.  The Get() method can have an integer
00020 # parameter that is passed to the str_function() function.  For the
00021 # default function (GenerateString()), if the seed is nonzero, the
00022 # Wichmann-Hill generator is started over and initialized from the
00023 # seed.
00024 # 
00025 # Each call to Get generates a new random string, sends it to
00026 # md5.update(), (which appends it to its own internal copy of all the
00027 # strings it's been sent), and then a new md5 hash is gotten, which is
00028 # converted to the hex representation with 32 hex characters.
00029 # 
00030 # This module could be made to provide cryptographically secure
00031 # one time pads by substituting a cryptographic quality random
00032 # number generator for the whrandom object.  You can go out on
00033 # the web and search for "random number" and find some hardware
00034 # devices to do this.
00035 # 
00036 # Copyright (C) 2002 GDS Software
00037 # 
00038 # This program is free software; you can redistribute it and/or
00039 # modify it under the terms of the GNU General Public License as
00040 # published by the Free Software Foundation; either version 2 of
00041 # the License, or (at your option) any later version.
00042 # 
00043 # This program is distributed in the hope that it will be useful,
00044 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00045 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00046 # GNU General Public License for more details.
00047 # 
00048 # You should have received a copy of the GNU General Public
00049 # License along with this program; if not, write to the Free
00050 # Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
00051 # MA  02111-1307  USA
00052 # 
00053 # See http://www.gnu.org/licenses/licenses.html for more details.
00054 # 
00055 import md5, whrandom
00056 __version__ = "$Id: otp.py,v 1.3 2002/08/21 12:41:49 donp Exp $"
00057 
00058 whG = whrandom.whrandom()
00059 
00060 ##
00061 # Generate a string from a four byte integer.  The string is the
00062 #     4 bytes of the integer, each converted to a character.
00063 #     
00064 def GenerateString(seed = 0):
00065     global whG
00066     if seed:      # seed != 0 means to restart; whrandom seeds from time.
00067         whG.seed(seed & 0xff, 
00068                  (seed & 0xff00) >> 8, 
00069                  (seed & 0xff0000) >> 16)
00070     n = whG.randint(0, 2**30-1)
00071     str = ""
00072     str = str + chr((n & 0xFF000000) >> 24)
00073     str = str + chr((n & 0x00FF0000) >> 16)
00074     str = str + chr((n & 0x0000FF00) >>  8)
00075     str = str + chr((n & 0x000000FF) >>  0)
00076     return str
00077 
00078 class Otp:
00079     def __init__(self, str_function = GenerateString, seed = 0):
00080         str_function(seed) # Initialize random number generator
00081         self.m = md5.new()
00082 
00083     ##
00084     # Return an OTP.
00085     #         
00086     def Get(self, seed=0):
00087         self.m.update(GenerateString(seed))
00088         string = self.m.digest()
00089         str = ""
00090         for ix in xrange(len(string)):
00091             str = str + "%02X" % ord(string[ix])
00092         return str
00093 
00094 if __name__ == "__main__":
00095     import sys
00096     num = 1
00097     seed = 0
00098     if len(sys.argv) < 2:
00099         print "Usage:  otp num_times [seed]"
00100         sys.exit(1)
00101     num = int(sys.argv[1])
00102     if len(sys.argv) == 3:
00103         seed = int(sys.argv[2])
00104     o = Otp(seed=seed)
00105     for ix in xrange(num):
00106         print o.Get()
00107 
00108 

© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved; If you reference this document or any part of this document you must use the citation verbatim (including the link) "© Copyright 2008-2009 Vyper Logix Corp., All Right Reserved."

Notice: This source code contained in this document is NOT open source and is NOT being distributed as open source.

122,241 lines of code and growing...