Dark Developments Where Knowledge Meets Power

9Aug/110

PY – Hashing

Posted by Dark#Basics

Basic Python code that displays how to:
- Use import
- Use classes and function calls
- Read arguments
- Handle exeptions
- Handle encodings

Select All Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@author: Dark#Basics @email: darkbasics@darkdevelopments.org @website: darkdevelopments.org
 
''' This module implements a common interface to many different secure hash and message digest algorithms. Included are the FIPS secure hash algorithms SHA1, SHA224, SHA256, SHA384, and SHA512 (defined in FIPS 180-2) as well as RSA’s MD5 algorithm. The terms secure hash and message digest are interchangeable. Older algorithms were called message digests. The modern term is secure hash. '''
import hashlib;
''' This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter. '''
import sys;
''' This module helps scripts to parse the command line arguments in sys.argv. It supports the same conventions as the Unix getopt() function (including the special meanings of arguments of the form ‘-‘ and ‘--‘). Long options similar to those supported by GNU software may be used as well via an optional third argument. '''
import getopt;
 
''' Contains all the functionality for the script '''
class Hashing:
 
    #Help for this class def _help_(self):
        print( '\n===============================================================')
        print( '\hashing.py -p [plaintext]')
        print( '-p [plaintext] Plaintext to encrypt')
        print( '-h Show help')
        print( '===============================================================')
 
    # Hash plaintext argument to MD5 equivalent. Do note when using the md5 function that the string needs to be in unicode. # Putting a String-object to unicode/UTF8 can be done by using the encode-function. # Hexdigest returns a string of double length, containing only hexadecimal digits. This may be used to exchange the value safely in email or other non-binary environments. def _hashit_(self,plaintext):
        print('\n[+] Plaintext: '+ plaintext + ' \n[+] MD5-hash: ' + hashlib.md5(plaintext.encode('utf8')).hexdigest())
 
#Process starts here if __name__ == '__main__':
 
    #Print a header print(' ____ __ ____ __ __ ')
    print(' / __ \____ ______/ /__/ __ \___ _ _____ / /____ ____ ____ ___ ___ ____ / /______')
    print(' / / / / __ `/ ___/ //_/ / / / _ \ | / / _ \/ // __ \/ __ \/ __ `__ \/ _ \/ __ \/ __/ ___/')
    print(' / /_/ / /_/ / / / ,< / /_/ / __/ |/ / __/ // /_/ / /_/ / / / / / / __/ / / / /_(__ ) ')
    print('_____/\__,_/_/ /_/|_/_____/\___/|___/\___/_/ \____/ .___/_/ /_/ /_/\___/_/ /_/\__/____/ ')
    print(' /_/ ')
    print('Tool:Hashing Tool\nAuthor:Dark#Basics\nWebsite:darkdevelopments.org')
 
    #Create object h = Hashing()
 
    #Try and read the arguments try:
        #Read arguments with getopt opts, args = getopt.getopt(sys.argv[1:], 'p:h')
        for o, arg in opts:
            if o == '-p' and isinstance(arg,str):
                h._hashit_(arg)
            elif o == '-h':
                h._help_()
            else:
                raise Exception()
 
    #If all fails except:
        print("\n[+] Invalid arguments! Show help with -h argument.")