Python

 

 

 

 

Python - Hash

 

The Python hashlib package is a built-in library that provides a variety of hash functions for secure, one-way data transformation. Hash functions are essential in cryptography, data integrity, and other security-related applications. They take an input (or "message") and return a fixed-size string of bytes, typically a "digest" that is unique to each unique input. Even small changes to the input will produce a completely different output, making it difficult to reverse-engineer the original data from the hash.

 

Here's an overview of the Python hashlib package without examples:

  • Importing hashlib: To use the hashlib package in your Python script, you need to import it
  • Supported algorithms: The hashlib package supports various hashing algorithms like MD5, SHA-1, SHA-224, SHA-256, SHA-384, SHA-512, SHA-3, and BLAKE2. The availability of these algorithms may vary depending on the Python build and OpenSSL library version.
  • Creating a hash object: To compute a hash, you need to create a hash object for the desired algorithm. You can do this by calling the appropriate function from the hashlib module, such as hashlib.sha256() or hashlib.md5().
  • Updating the hash object with data: Hash objects have an update() method that accepts the data you want to hash as a bytes-like object (e.g., bytes, bytearray). You can call this method multiple times to hash larger data in chunks.
  • Getting the hash digest: After updating the hash object with data, you can obtain the hash digest using two methods:
  • hexdigest(): Returns the hash digest as a hexadecimal string. This is a human-readable representation of the hash value.
  • digest(): Returns the hash digest as a bytes object. This is a more compact, binary representation of the hash value.
  • Hashlib utility functions:
    • hashlib.new(): Allows you to create a new hash object by specifying the algorithm name as a string (e.g., "sha256" or "md5"). This can be useful when the algorithm is determined at runtime.
    • hashlib.pbkdf2_hmac(): Implements the PBKDF2 (Password-Based Key Derivation Function 2) algorithm, which is commonly used to derive cryptographic keys from passwords.

 

NOTE 1 : All the examples in this page are written in Python 3.x. It may not work if you use Pyton 2.x

NOTE 2 : All the examples in this page are assumed to be written/run on Windows 7 unless specifically mentioned. You MAY (or may not) need to modify the syntax a little bit if you are running on other operating system.

 

 

Regarding the definition of Hash (What is Hash ?), refer to IP Security : Hash page.

 

 

 

Examples

 

 

< Example 1 >

 

# Import the hashlib library

import hashlib

 

# Create an MD5 hash object with the encoded string 'Hello World'

hashOut = hashlib.md5('Hello World'.encode())

 

# Print the binary representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [String] = ", hashOut.hexdigest())

 

# Create an MD5 hash object with the encoded string 'Hello World!'

hashOut = hashlib.md5('Hello World!'.encode())

 

# Print the binary representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [String] = ", hashOut.hexdigest())

 

# Create an MD5 hash object with the encoded string 'Hello World Hello World Hello World Hello World Hello World'

hashOut = hashlib.md5('Hello World Hello World Hello World Hello World Hello World'.encode())

 

# Print the binary representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] = ",

       hashOut.digest())

# Print the hexadecimal string representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] = ",

       hashOut.hexdigest())

Hash Out for 'Hello World' [Binary] =  b'\xb1\n\x8d\xb1d\xe0uA\x05\xb7\xa9\x9b\xe7.?\xe5'

Hash Out for 'Hello World' [String] =  b10a8db164e0754105b7a99be72e3fe5

 

Hash Out for 'Hello World!' [Binary] =  b'\xed\x07b\x87S.\x866^\x84\x1e\x92\xbf\xc5\r\x8c'

Hash Out for 'Hello World!' [String] =  ed076287532e86365e841e92bfc50d8c

 

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary]

     =  b'\x99\xa5\xae\xfc\xa17\xbfX\x8eq?BO\x8d\xfb\xfa'

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String]

     =  99a5aefca137bf588e713f424f8dfbfa

 

 

< Example 2 >

 

 

# Import the hashlib library

import hashlib

 

# Create an SHA-512 hash object with the encoded string 'Hello World'

hashOut = hashlib.sha512('Hello World'.encode())

 

# Print the binary representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World'

print("Hash Out for 'Hello World' [String] = ", hashOut.hexdigest())

 

# Create an SHA-512 hash object with the encoded string 'Hello World!'

hashOut = hashlib.sha512('Hello World!'.encode())

 

# Print the binary representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [Binary] = ", hashOut.digest())

# Print the hexadecimal string representation of the hash digest for 'Hello World!'

print("Hash Out for 'Hello World!' [String] = ", hashOut.hexdigest())

 

# Create an SHA-512 hash object with the encoded string 'Hello World Hello World Hello World Hello World Hello World'

hashOut = hashlib.sha512('Hello World Hello World Hello World Hello World Hello World'.encode())

 

# Print the binary representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] = ",

       hashOut.digest())

# Print the hexadecimal string representation of the hash digest for the long 'Hello World' string

print("Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] = ",

       hashOut.hexdigest())

Hash Out for 'Hello World' [Binary] =  

b',t\xfd\x17\xed\xaf\xd8\x0e\x84G\xb0\xd4gA\xee$;~\xb7M\xd2\x14\x9a\n\xb1\xb9$o\xb3\x03\x82\xf2~\

   x85=\x85\x85q\x9e\x0eg\xcb\xda\r\xaa\x8fQg\x10da]dZ\xe2z\xcb\x15\xbf\xb1D\x7fE\x9b'

Hash Out for 'Hello World' [String] =  

   2c74fd17edafd80e8447b0d46741ee243b7eb74dd2149a0ab1b9246fb30382f27e853d8585719e0e67cbda0da

  a8f51671064615d645ae27acb15bfb1447f459b

 

Hash Out for 'Hello World!' [Binary] =  

  b'\x86\x18D\xd6pN\x85s\xfe\xc3M\x96~ \xbc\xfe\xf3\xd4$\xcfH\xbe\x04\xe6\xdc\x08\xf2\xbdX\xc7)t3q\x01

  ^\xad\x89\x1c\xc3\xcf\x1c\x9d4\xb4\x92d\xb5\x10u\x1b\x1f\xf9\xe57\x93{\xc4k]o\xf4\xec\xc8'

Hash Out for 'Hello World!' [String] =

 861844d6704e8573fec34d967e20bcfef3d424cf48be04e6dc08f2bd58c729743371015ead891cc3cf1c9d34b4926

 4b510751b1ff9e537937bc46b5d6ff4ecc8

 

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [Binary] =  

 b'\xda&\xc0\xad\x86O\xcf|\xa2;Hp\xc2\x92\xba\x97\x95VO\xf9\xd8\xf5b\xaeS\x00\x15[\xb5Wd\xe3\xc0\xbb9

 \x84\x13\x9a\x84}3R\xaeu>\x8d\x0f\xcdlF\xba\xef\x9dw2+\xb2\x15\x812\x83\xf0\x90\xd3'

Hash Out for 'Hello World Hello World Hello World Hello World Hello World' [String] =

 da26c0ad864fcf7ca23b4870c292ba9795564ff9d8f562ae5300155bb55764e3c0bb3984139a847d3352ae753e8d0

  fcd6c46baef9d77322bb215813283f090d3