What You Want To Know About Hashing in Python

by | Nov 6, 2023 | Etcetera | 0 comments

Python programmers use hashing to turn into input data proper right into a fixed-size value. This value represents the information uniquely, and the hashing technique makes it easy to transmit and store moderately a large number of varieties of data securely.

Hashing protects data from unauthorized get entry to and tampering. It’s an a very powerful part in data integrity and safety use cases.

This text explores the entire thing you want to be informed about hashing in Python. It dives into hashing uses and highlights moderately a large number of hashing algorithms that make your code additional atmosphere pleasant, protected, and loyal.

What Is Hashing in Python?

Hashing converts input data, identical to a string, report, or object, proper right into a fixed-size string of bytes. The hash or digest represents the input in a novel and reproducible manner.

Hashing plays a very powerful serve as in detecting data manipulation and embellishing protection. It should compute a hash value for a report, message, or other piece of knowledge. An tool retail outlets the hash securely to make sure later that the information has now not been tampered with.

One of the now not atypical uses of hashing in protection is password garage. Hashing is a viable variety to storing easy text passwords in a database. When a shopper enters their password, the machine hashes it faster than storing it throughout the database. If a hacker accesses the database, they’ll to search out that the password is hard to steal.

Python hashing functions make all this imaginable. The ones mathematical functions let an tool manipulate data into hash values.

How To Make an Environment friendly Hashing Function

A hashing function should meet the following requirements to be environment friendly and safe:

  • Deterministic — Given the identical input, the function should at all times return the identical output.
  • Surroundings pleasant — It’s going to must be computationally atmosphere pleasant when calculating the hash value of any given input.
  • Collision resistant — The function should lower the risk of two inputs making the identical hash value.
  • Uniform — The function’s outputs should be uniformly disbursed across the range of imaginable hash values.
  • Non-invertible — It’s going to must be now not going for a computer to calculate the function’s input value in step with the hash value.
  • Non-predictable — Predicting the function’s outputs should be tricky, given a set of inputs.
  • Subtle to go into changes — The function should be refined to minor permutations in input. Slight changes should cause a big difference throughout the resulting hash value.

Hashing Use Cases

Once you have an excellent sufficient hashing function with most of these characteristics, you’ll have the ability to apply it to moderately a large number of use cases. Hashing functions art work neatly for:

  • Password storage — Hashing is without doubt one of the easiest techniques to store shopper passwords in trendy strategies. Python combines moderately a large number of modules to hash and protected passwords faster than storing them in a database.
  • Caching — Hashing retail outlets a function’s output to save some time when calling it later.
  • Data retrieval — Python uses a hash table with a built-in dictionary data development to quickly retrieve values via key.
  • Digital signatures — Hashing can read about the authenticity of messages that have digital signatures.
  • Record integrity tests — Hashing can take a look at a report’s integrity all over the place its transfer and acquire.
See also  The right way to create a vintage WordPress theme: step by step information

Python’s Built-In Hashing Function

Python’s built-in hashing function, hash(), returns an integer value representing the input object. The code then uses the following hash value to make a decision the item’s location throughout the hash table. This hash table is a data development that implements dictionaries and gadgets.

The code beneath demonstrates how the hash() function works:

my_string = "hello world"

# Calculate the hash value of the string
hash_value = hash(my_string)

# Print the string and its hash value
print("String: ", my_string)
print("Hash value: ", hash_value)

If we save that code in a report named hash.py, we can execute it (and see the output) like this:

% python3 hash.py
String:  hello world
Hash value:  2213812294562653681

Let’s run that yet again:

% python3 hash.py
String:  hello world
Hash value:  -631897764808734609

The hash value is different when invoked a 2nd time because of recent releases of Python (permutations 3.3 and up), via default, observe a random hash seed for this function. The seed changes on each invocation of Python. Inside of a single instance, the results it is going to be identical.

For example, let’s put this code in our hash.py report:

my_string = "hello world"

# Calculate 2 hash values of the string
hash_value1 = hash(my_string)
hash_value2 = hash(my_string)

# Print the string and its hash values
print("String: ", my_string)
print("Hash value 1: ", hash_value1)
print("Hash value 2: ", hash_value2)

When executed, we see something like this:

String: hello world
Hash value 1:  -7779434013116951864
Hash value 2:  -7779434013116951864

Limitations of Hashing

Even supposing Python’s hash function is promising for moderately a large number of use cases, its limitations make it mistaken for protection purposes. Proper right here’s how:

  • Collision attacks — A collision occurs when two different inputs produce the identical hash value. An attacker might use the identical input-making way to bypass security measures that rely on hash values for authentication or data integrity tests.
  • Limited input duration — Since hash functions produce a fixed-sized output regardless of the input’s duration, an input higher in duration than the hash function’s output might motive a collision.
  • Predictability — A hash function should be deterministic, giving the identical output each and every time you provide the identical input. Attackers would perhaps profit from this susceptible spot via precompiling hash values for a lot of inputs, and then comparing them to concentrate on value hashes to find a have compatibility. This process is referred to as a rainbow table attack.

To prevent attacks and keep your data safe, use protected hashing algorithms designed to resist such vulnerabilities.

Using hashlib for Secure Hashing in Python

Instead of the use of the built-in Python hash(), use hashlib for added protected hashing. This Python module offers a large number of hash algorithms to hash data securely. The ones algorithms include MD5, SHA-1, and the additional protected SHA-2 family, at the side of SHA-256, SHA-384, SHA-512, and others.

MD5

The repeatedly used cryptographic algorithm MD5 reveals a 128-bit hash value. Use the code like that beneath to generate an MD5 hash the use of the hashlib‘s md5 constructor:

import hashlib

text = "Hello World"
hash_object = hashlib.md5(text.encode())
print(hash_object.hexdigest())

The output of the above (in our hash.py report) it is going to be consistent all through invocations:

b10a8db164e0754105b7a99be72e3fe5

Practice: The hexdigest() means throughout the code above returns the hash in a hexadecimal construction safe for any non-binary presentation (identical to email correspondence).

SHA-1

The SHA-1 hash function secures data via making a 160-bit hash value. Use the code beneath with the sha1 constructor for the hashlib module’s SHA-1 hash:

import hashlib

text = "Hello World"
hash_object = hashlib.sha1(text.encode())
print(hash_object.hexdigest())

The output of the above:

See also  Best possible Buyer Knowledge Platform Firms

0a4d55a8d778e5022fab701977c5d840bbc486d0

SHA-256

There are moderately a large number of hash alternatives throughout the SHA-2 family. The hashlib SHA-256 constructor generates a additional protected type in that family with a 256-bit hash value.

Programmers incessantly use SHA-256 for cryptography, like digital signatures or message authentication codes. The code beneath demonstrates the easiest way to generate a SHA-256 hash:

import hashlib

text = "Hello World"
hash_object = hashlib.sha256(text.encode())
print(hash_object.hexdigest())

The output of the above:

a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

SHA-384

SHA-384 is a 384-bit hash value. Programmers incessantly use the SHA-384 function in applications short of additional data protection.

In step with the previous examples, you’ll have the ability to maximum indisputably guess that this is a commentary that can generate a SHA-384 hash:

hash_object = hashlib.sha384(text.encode())

SHA-512

SHA-512 is largely probably the most protected member of the SHA-2 family. It makes a 512-bit hash value. Programmers use it for high-throughput applications, identical to checking data integrity. The code beneath displays the easiest way to generate a SHA-512 hash with the hashlib module in Python:

hash_object = hashlib.sha512(text.encode())

How To Make a selection a Hashing Algorithm

Since the ones algorithms vary, select your hashing algorithm in step with your use case and its protection prerequisites. Listed here are some steps to use:

  • Understand the use case — Your use case determines what kind of algorithm to use. For example, when storing refined data identical to passwords, your hashing algorithm could have to offer protection to against brute-force attacks.
  • Imagine your protection prerequisites — Your use case’s protection prerequisites depend on the type of data you propose to store, they typically make a decision what kind of algorithm to select. For example, a robust hashing algorithm could be very highest for storing extraordinarily refined wisdom.
  • Research the available hashing algorithms — Uncover each hashing type to grab its strengths and weaknesses. This information helps you select the most suitable option for your use case.
  • Review the selected hashing algorithm — Once you choose a hashing algorithm, analysis whether or not or now not it meets your protection prerequisites. This process would perhaps include checking out it against identified attacks or vulnerabilities.
  • Enforce and check out the hashing algorithm — Finally, enforce and check out the algorithm completely to make sure it functions accurately and securely.

How To Use Hashing for Password Storage

Hashing has superb imaginable for storing passwords, a a very powerful a part of cybersecurity.

Ideally, the application hashes and retail outlets passwords in a protected database to stop unauthorized get entry to and data breaches. However, hashing alone might not be enough to give protection to the tips. Hashed passwords are nevertheless at risk of brute energy and dictionary attacks. Hackers incessantly use the ones practices to guess passwords and reach unauthorized get entry to to accounts.

A additional protected manner to use hashing for password storage involves the salting technique. Salting supplies unique, random strings or characters to each password faster than hashing it. The salt is unique to each password, and the application retail outlets it alongside the hashed password throughout the database.

Every time a shopper logs in, the application retrieves the salt from the database, supplies it to the entered password, and then hashes the combined salt and password.

If an attacker options get entry to to the database, they will have to compute the hash for each password and each imaginable salt value. Salting makes the ones attacks additional sophisticated, so it’s an invaluable technique to deter dictionary attacks.

Python’s secrets and techniques and strategies module makes salting easy. This module generates random salts, securely storing passwords and managing tokens and cryptographic keys.

See also  8 Courses We Discovered from Those Well-known Rebrands [2023 Edition]

The code beneath uses the hashlib library and secrets and techniques and strategies module to protected shopper passwords further:

import hashlib
import secrets and techniques and strategies

# Generate a random salt the use of the secrets and techniques and strategies module
salt = secrets and techniques and strategies.token_hex(16)

# Get the patron's password from input
password = input("Enter your password: ")

# Hash the password the use of the salt and the SHA-256 algorithm
hash_object = hashlib.sha256((password + salt).encode())

# Get the hexadecimal representation of the hash
hash_hex = hash_object.hexdigest()

# Store the salt and hash hex on your database

How To Use Hashing for Data Integrity Exams

Hashing moreover helps take a look at data integrity and offer protection to transmitted data from modification and tampering. This four-step technique uses a cryptographic hash function to give you the report a novel hash value.

First, select the correct hash function and use it to generate a hash value for the input data. Store that hash value, then use it for comparison when sought after. Each and every time you want to make sure the information’s integrity, the application generates the hash value of the current data the use of the identical hash function. Then, the application compares the new hash value with the stored value to make sure they’re identical. If so, the information is uncorrupted.

The hashed value is unique, or perhaps a tiny alternate throughout the input data triggers a significantly different hash value. This makes it easy to return throughout any unauthorized changes or adjustments to the transmitted data.

The steps beneath show the use of a hash function for info integrity tests.

Step 1: Import the hashlib Module

import hashlib

Step 2: Use a hashlib Hash Algorithm

def generate_hash(file_path):

    # Open the report in binary mode
    with open(file_path, "rb") as f:

        # Be informed the contents of the report
        contents = f.be told()

        # Generate the SHA-256 hash of the contents
        hash_object = hashlib.sha256(contents)

        # Return the hexadecimal representation of the hash
        return hash_object.hexdigest()

Step 3: Identify the Function and Transfer throughout the Record Path

file_path = "path/to/my/report.txt"
hash_value = generate_hash(file_path)
print(hash_value)

Step 4: Generate Hashes for the Original Record and Transmitted or Modified Record

# Generate the hash of the original report
original_file_path = "path/to/my/report.txt"
original_file_hash = generate_hash(original_file_path)

# Transmit or control the report (for example, via copying it to some other location)
transmitted_file_path = "path/to/transmitted/report.txt"

# Generate the hash of the transmitted report
transmitted_file_hash = generate_hash(transmitted_file_path)

Step 5: Assessment the Two Hashes

if original_file_hash == transmitted_file_hash:
    print("The report has now not been tampered with")
else:
    print("The report has been tampered with")

Summary

Hashing is recommended for info integrity and password protection. You get necessarily probably the most out of a hashing function whilst you enforce protected hashing tactics, identical to the use of the hashlib module and salting.

The ones tactics have the same opinion prevent rainbow attacks, collision attacks, and other protection vulnerabilities that have an effect on hashing. Programmers incessantly use the ones tactics with hashing functions in Python to make sure the information integrity of knowledge and store passwords securely.

Now that you simply’ve learned additional about hashing tactics in Python use them to improve your own tool’s protection. Uncover additional Python articles on the Kinsta blog to expand your enjoy, and then consider deploying your next Python tool on Kinsta’s Utility Website hosting platform.

The publish What You Want To Know About Hashing in Python appeared first on Kinsta®.

WP Hosting

[ continue ]

WordPress Maintenance Plans | WordPress Hosting

read more

0 Comments

Submit a Comment

DON'T LET YOUR WEBSITE GET DESTROYED BY HACKERS!

Get your FREE copy of our Cyber Security for WordPress® whitepaper.

You'll also get exclusive access to discounts that are only found at the bottom of our WP CyberSec whitepaper.

You have Successfully Subscribed!