Python Create SHA256 Hash of String[with Code Examples] - JSON Viewer

Python Create SHA256 Hash of String[with Code Examples]

What Is SHA256 ?

SHA-256 (Secure Hash Algorithm 256) is a cryptographic hash function that is widely used for securing data and ensuring its integrity. It takes an input (message) of any length and produces a fixed-size 256-bit output (hash value) that is unique to the input data.

SHA-256 is a one-way function, meaning that it is computationally infeasible to derive the original message from its hash value. This property makes it useful for verifying the authenticity of data and ensuring that it has not been tampered with. It is commonly used in digital signatures, password storage, and blockchain technology.

Python hashlib Library

What is hashlib Library ?

  • The hashlib library in Python provides a set of hash functions that can be used to create a message digest of data. A hash function takes input data of arbitrary length and produces a fixed-length output, which is typically a hexadecimal string.
  • The hashlib library provides several hash functions, including MD5, SHA-1, SHA-224, SHA-256, SHA-384, and SHA-512. These functions are secure, widely-used, and available on all major platforms.
  • To use hashlib, you first need to create an instance of a hash object by calling one of the provided hash functions, such as hashlib.sha256().

What is hashlib.sha256() function ?

  • Python hashlib library provides the hashlib.sha256() function that creates an instance of the SHA-256 hash algorithm. SHA-256 algorithm is a widely used secure hash function that produces a 256-bit message digest of input data.
  • The hashlib.sha256() function takes a bytes-like object (String in our case) as input and returns a sha256 hash object.
  • You can get the resulting hash value by calling the hexdigest() method of the hash object.

How to create Python SHA256 Hash of String

To create a SHA256 hash of a string in Python using the hashlib library , we can use the following

Syntax –

import hashlib
string_to_hash = "Hello, goom!"
sha256_hash = hashlib.sha256(string_to_hash.encode()).hexdigest()
print(sha256_hash)

Output

41ebe69257d01db858d0e4043ba57d83100073d42eeef7883ffa70b3a2f08741

The hexdigest() method is called on the hash object to obtain the hexadecimal representation of the hash.

Why do we need SHA256 encryption ?

Data Integrity

SHA-256 ensures that data is not modified in transit. If a sender hashes a file or message using SHA-256, the recipient can then hash the same file or message and compare the two hash values to ensure that they match. If the hash values do not match, it indicates that the data has been altered in transit.

Password Storage

SHA-256 can also be used to store passwords securely. When a user creates a password, it can be hashed using SHA-256 and the resulting hash value can be stored in a database. When the user logs in, the system can hash the entered password and compare it to the stored hash value. If the two hash values match, it indicates that the user entered the correct password.

Digital Signatures

SHA-256 can be used to generate digital signatures, which are used to verify the authenticity of a document or message. A digital signature is created by hashing the document or message using SHA-256 and then encrypting the resulting hash value with a private key. The recipient can then verify the signature by decrypting the hash value using the sender’s public key and comparing it to the hash value of the original document or message.