CIT PYTHON COHORT THREE
  • CIT Python Cloud Software Engineering
  • week one
    • What is Python
    • Python Syntax
    • variables
    • Numbers / Integers
  • week Two
    • Control Flow Statements
      • If Statements
      • For Loops
      • While Loops
      • Break and Continue Statements
    • Operators
      • Assignment Operators
      • Arithmetic Operators
      • Comparison Operators
      • Logical Operators
      • Relational Operators
      • Bitwise Operators
      • Identity Operators
      • Membership Operators
    • Data Types
      • Strings
      • Numbers
      • Booleans
      • Lists
      • Dictionaries
      • Tuples
      • Sets
  • Week 3
    • Functions
      • Function Arguments
      • Python Recursion
      • Python Anonymous/Lambda Function
    • Object Oriented Programming
      • Classes
      • Inheritance
      • Polymorphism
      • Abstraction
      • Encapsulation
    • Python Modules
      • Python Packages
      • Python Built-in Modules
      • Python Standard Library
      • Python Third Party Modules
    • Python Exceptions
      • Python Try Except
      • Python Raise
      • Python Assert
      • Python User-defined Exceptions
  • Week 4
    • Python File Handling
  • Week6
    • Data Structures and Algorithms
      • DSA Introduction
      • What is an Algorithm?
      • Data Structures and Types
      • Stack
      • Queue
      • Linked List
      • Bubble Sort Algorithm
      • Selection Sort Algorithm
      • Insertion Sort Algorithm
      • Merge Sort Algorithm
      • Quick Sort Algorithm
  • Week8
    • Cryptography
      • Reverse Cipher
      • Caesar Cipher
      • Hash Functions
        • Applications of Hash Functions
        • Examples of Hash Functions
  • Assignments and Solutions
    • Loops
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub
  1. Week8
  2. Cryptography
  3. Hash Functions

Examples of Hash Functions

In this section we shall provide a few examples about calculating cryptographic hash functions in Python. We shall use the `hashlib` module from the Python standard library.

We shall use the standard Python library hashlib. The input data for hashing should be given as bytes sequence (bytes object), so we need to encode the input string using some text encoding, e.g. utf8. The produced output data is also a bytes sequence, which can be printed as hex digits using binascii.hexlify() as shown below:

import hashlib, binascii

text = 'hello'
data = text.encode("utf8")

sha256hash = hashlib.sha256(data).digest()
print("SHA-256:   ", binascii.hexlify(sha256hash))

sha3_256 = hashlib.sha3_256(data).digest()
print("SHA3-256:  ", binascii.hexlify(sha3_256))

blake2s = hashlib.new('blake2s', data).digest()
print("BLAKE2s:   ", binascii.hexlify(blake2s))

ripemd160 = hashlib.new('ripemd160', data).digest()
print("RIPEMD-160:", binascii.hexlify(ripemd160))

The output of the above program is:

SHA-256:    b'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
SHA3-256:   b'3338be694f50c5f338814986cdf0686453a888b84f424d792af4b9202398f392'
BLAKE2s:    b'19213bacc58dee6dbde3ceb9a47cbb330b3d86f8cca8997eb00be456f140ca25'
RIPEMD-160: b'108f07b8382412612c048d07d13f814118445acd'
PreviousApplications of Hash FunctionsNextAssignments and Solutions

Last updated 2 years ago

Was this helpful?