Advertisement
4SS4SS1N

hsh decrypter

Sep 13th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.28 KB | None | 0 0
  1. # setup.py for hashlib standalone for Python versions < 2.5
  2. #
  3.  
  4. __version__ = "20081119"
  5.  
  6. import sys, os, re
  7.  
  8. from distutils.core import setup, Extension
  9. from distutils.ccompiler import new_compiler
  10.  
  11.  
  12. # additional paths to check, set from the command line
  13. SSL_INCDIR=''   # --openssl-incdir=
  14. SSL_LIBDIR=''   # --openssl-libdir=
  15. SSL_DIR=''      # --openssl-prefix=
  16.  
  17.  
  18. def add_dir_to_list(dirlist, dir):
  19.     """Add the directory 'dir' to the list 'dirlist' (at the front) if
  20.    'dir' actually exists and is a directory.  If 'dir' is already in
  21.    'dirlist' it is moved to the front."""
  22.     if dir is not None and os.path.isdir(dir) and dir not in dirlist:
  23.         if dir in dirlist:
  24.             dirlist.remove(dir)
  25.         dirlist.insert(0, dir)
  26.  
  27.  
  28. def prepare_hashlib_Extensions():
  29.     """Decide which C extensions to build and create the appropriate
  30.    Extension objects to build them.  Returns a list of Extensions."""
  31.  
  32.     # this CCompiler object is only used to locate include files
  33.     compiler = new_compiler()
  34.  
  35.     # Ensure that these paths are always checked
  36.     if os.name == 'posix':
  37.         add_dir_to_list(compiler.library_dirs, '/usr/local/lib')
  38.         add_dir_to_list(compiler.include_dirs, '/usr/local/include')
  39.  
  40.         add_dir_to_list(compiler.library_dirs, '/usr/local/ssl/lib')
  41.         add_dir_to_list(compiler.include_dirs, '/usr/local/ssl/include')
  42.  
  43.         add_dir_to_list(compiler.library_dirs, '/usr/contrib/ssl/lib')
  44.         add_dir_to_list(compiler.include_dirs, '/usr/contrib/ssl/include')
  45.  
  46.         add_dir_to_list(compiler.library_dirs, '/usr/lib')
  47.         add_dir_to_list(compiler.include_dirs, '/usr/include')
  48.  
  49.     # look in command line supplied paths
  50.     if SSL_LIBDIR:
  51.         add_dir_to_list(compiler.library_dirs, SSL_LIBDIR)
  52.     if SSL_INCDIR:
  53.         add_dir_to_list(compiler.include_dirs, SSL_INCDIR)
  54.     if SSL_DIR:
  55.         if os.name == 'nt':
  56.             add_dir_to_list(compiler.library_dirs, os.path.join(SSL_DIR, 'out32dll'))
  57.             # prefer the static library
  58.             add_dir_to_list(compiler.library_dirs, os.path.join(SSL_DIR, 'out32'))
  59.         else:
  60.             add_dir_to_list(compiler.library_dirs, os.path.join(SSL_DIR, 'lib'))
  61.         add_dir_to_list(compiler.include_dirs, os.path.join(SSL_DIR, 'include'))
  62.  
  63.     osNameLibsMap = {
  64.         'posix':  ['ssl', 'crypto'],
  65.         'nt':     ['libeay32',  'gdi32', 'advapi32', 'user32'],
  66.     }
  67.     if not osNameLibsMap.has_key(os.name):
  68.         print "unknown OS, please update setup.py"
  69.         sys.exit(1)
  70.  
  71.     exts = []
  72.  
  73.     ssl_inc_dirs = []
  74.     ssl_incs = []
  75.     for inc_dir in compiler.include_dirs:
  76.         f = os.path.join(inc_dir, 'openssl', 'ssl.h')
  77.         if os.path.exists(f):
  78.             ssl_incs.append(f)
  79.             ssl_inc_dirs.append(inc_dir)
  80.  
  81.     ssl_lib = compiler.find_library_file(compiler.library_dirs, osNameLibsMap[os.name][0])
  82.  
  83.     # find out which version of OpenSSL we have
  84.     openssl_ver = 0
  85.     openssl_ver_re = re.compile(
  86.         '^\s*#\s*define\s+OPENSSL_VERSION_NUMBER\s+(0x[0-9a-fA-F]+)' )
  87.     ssl_inc_dir = ''
  88.     for ssl_inc_dir in ssl_inc_dirs:
  89.         name = os.path.join(ssl_inc_dir, 'openssl', 'opensslv.h')
  90.         if os.path.isfile(name):
  91.             try:
  92.                 incfile = open(name, 'r')
  93.                 for line in incfile:
  94.                     m = openssl_ver_re.match(line)
  95.                     if m:
  96.                         openssl_ver = eval(m.group(1))
  97.                         break
  98.             except IOError:
  99.                 pass
  100.  
  101.         # first version found is what we'll use
  102.         if openssl_ver:
  103.             break
  104.  
  105.     if (ssl_inc_dir and
  106.         ssl_lib is not None and
  107.         openssl_ver >= 0x00907000):
  108.  
  109.         print 'Using OpenSSL version 0x%08x from' % openssl_ver
  110.         print ' Headers:\t', ssl_inc_dir
  111.         print ' Library:\t', ssl_lib
  112.  
  113.         # The _hashlib module wraps optimized implementations
  114.         # of hash functions from the OpenSSL library.
  115.         exts.append( Extension('_hashlib', ['_hashopenssl.c'],
  116.                                include_dirs = [ ssl_inc_dir ],
  117.                                library_dirs = [ os.path.dirname(ssl_lib) ],
  118.                                libraries = osNameLibsMap[os.name]) )
  119.     else:
  120.         exts.append( Extension('_sha', ['shamodule.c']) )
  121.         exts.append( Extension('_md5',
  122.                         sources = ['md5module.c', 'md5.c'],
  123.                         depends = ['md5.h']) )
  124.  
  125.     if (not ssl_lib or openssl_ver < 0x00908000):
  126.         # OpenSSL doesn't do these until 0.9.8 so we'll bring our own
  127.         exts.append( Extension('_sha256', ['sha256module.c']) )
  128.         exts.append( Extension('_sha512', ['sha512module.c']) )
  129.  
  130.     def prependModules(filename):
  131.         return os.path.join('Modules', filename)
  132.  
  133.     # all the C code is in the Modules subdirectory, prepend the path
  134.     for ext in exts:
  135.         ext.sources = [ prependModules(fn) for fn in ext.sources ]
  136.         if hasattr(ext, 'depends') and ext.depends is not None:
  137.             ext.depends = [ prependModules(fn) for fn in ext.depends ]
  138.  
  139.     return exts
  140.  
  141.  
  142. # do the actual build, install, whatever...
  143. def main():
  144.     if sys.version >= (2,5):
  145.         print "You do not need to build hashlib, it comes standard with Python >= 2.5"
  146.         sys.exit(1)
  147.  
  148.     # parse command line for --openssl=path
  149.     global SSL_INCDIR, SSL_LIBDIR, SSL_DIR
  150.     for arg in sys.argv[:]:
  151.         if arg.startswith('--openssl-incdir='):
  152.             SSL_INCDIR = arg.split('=')[1]
  153.             sys.argv.remove(arg)
  154.         if arg.startswith('--openssl-libdir='):
  155.             SSL_LIBDIR = arg.split('=')[1]
  156.             sys.argv.remove(arg)
  157.         if arg.startswith('--openssl-prefix='):
  158.             SSL_DIR = arg.split('=')[1]
  159.             sys.argv.remove(arg)
  160.  
  161.     setup(
  162.       name =        'hashlib',
  163.       version =     __version__,
  164.       description = 'Secure hash and message digest algorithm library',
  165.       long_description = """\
  166. This is a stand alone packaging of the hashlib
  167. library introduced in Python 2.5 so that it can
  168. be used on older versions of Python.""",
  169.       license = "PSF license",
  170.  
  171.       Coder = "SR.STORM",
  172.    
  173.  
  174.       ext_modules = prepare_hashlib_Extensions(),
  175.  
  176.       py_modules = [ 'hashlib' ],
  177.     )
  178.  
  179. if __name__ == '__main__':
  180.     main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement