Hash Function

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 62

Hash Function

What is a Hash Function?


• A hash function is a mathematical algorithm that takes an input
(message) and produces a fixed-size string of characters.
• The output, known as the hash value or hash code, is unique to the
input data.
• Hash functions are designed to be fast and efficient, generating a
hash value in a short amount of time.
Properties of Hash Functions
• Collision Resistance: It should be computationally infeasible to find
two different inputs that produce the same hash value.
• Deterministic: The same input will always produce the same hash
value.
• Fixed Output Size: Hash functions generate a fixed-length hash value,
regardless of the input size.
• Avalanche Effect: A small change in the input should produce a
significant change in the output.
Properties of Hash Functions
• Hash function: A hash function is a mathematical function that takes
an input of any length and produces an output of a fixed length.
• Cryptographic hash function: A cryptographic hash function is a hash
function that is designed to be secure against collisions and
preimages.
• Collision: A collision is when two different inputs produce the same
output.
Hash Function Usage purpose
• Hash functions are used in cryptography for a variety of purposes:
• To create digital signatures
• To verify the integrity of data
• To protect passwords
Create digital signatures
• Digital signatures: A digital signature is a way of verifying the
authenticity of a message or document.
• To create a digital signature, the sender of the message or document
uses their private key to encrypt a hash of the message or document.
• The encrypted hash is then attached to the message or document.
• When the recipient receives the message or document, they use the
sender's public key to decrypt the encrypted hash.
• If the decrypted hash matches the hash of the message or document,
then the recipient can be confident that the message or document
has not been tampered with.
Verify the integrity of data
• Verifying the integrity of data: Hash functions can also be used to
verify the integrity of data.
• To verify the integrity of data, a hash of the data is created and then
stored.
• When the data is received, a new hash of the data is created and
compared to the stored hash.
• If the two hashes match, then the data has not been tampered with.
Protect passwords
• Protecting passwords: Hash functions can also be used to protect
passwords.
• To protect a password, it is hashed using a cryptographic hash
function.
• The hashed password is then stored.
• When the user tries to log in, their password is hashed and compared
to the stored hash.
• If the hashed passwords match, then the user is allowed to log in.
Hash Functions vs. Encryption Algorithms
• Encryption algorithms are designed to provide confidentiality by
transforming data into an unreadable format, whereas hash functions
do not aim for confidentiality.
• Hash functions are one-way functions, meaning it is computationally
infeasible to reverse-engineer the original input from the hash value.
• Encryption algorithms use keys for encryption and decryption, while
hash functions do not require a key.
Commonly used hash functions in Cryptography
• Here are some of the most commonly used hash functions in cryptography:
• MD5: MD5 is a 128-bit hash function that was developed by Rivest, Shamir, and Adleman in 1991.
MD5 is no longer considered secure for most applications, but it is still used in some older
applications.
• SHA-1: SHA-1 is a 160-bit hash function that was developed by the National Institute of Standards
and Technology (NIST) in 1995. SHA-1 is considered to be secure for most applications.
• SHA-256: SHA-256 is a 256-bit hash function that was also developed by NIST in 2001. SHA-256 is
considered to be more secure than SHA-1.
• SHA-512: SHA-512 is a 512-bit hash function that was also developed by NIST in 2001. SHA-512 is
considered to be the most secure hash function available.
• keccak256 (SHA-3 family) algorithm computes the hash of an input to a fixed length output. The
input can be a variable length string or number, but the result will always be a fixed bytes32 data
type.
• RIPEMD
MD5 Message-Digest Algorithm
• The MD5 message-digest algorithm is a widely used hash function
producing a 128-bit hash value.
• MD5 was designed by Ronald Rivest in 1991 to replace an earlier hash
function MD4, and was specified in 1992 as RFC 1321
• MD5 message-digest algorithm is a widely used hash function producing a
128-bit hash value. MD5 was designed by Ronald Rivest in 1991 to replace an
earlier hash function MD4, and was specified in 1992 as RFC 1321..
• MD5 can be used as a checksum to verify data integrity against
unintentional corruption.
How MD5 Works:
• MD5 works by taking a message of any length and dividing it into 512-
bit blocks.
• Each block is then processed using a series of mathematical
operations to produce a 128-bit hash value.
• The hash value is a unique identifier for the message, and can be used
to verify that the message has not been tampered with.
Uses of MD5
• MD5 is used in a variety of applications, including:
• File checksums: MD5 can be used to create a checksum for a file, which can
then be used to verify that the file has not been tampered with.
• Password hashing: MD5 can be used to hash passwords, which makes it more
difficult for attackers to crack passwords.
• Digital signatures: MD5 can be used to create digital signatures, which can be
used to verify the authenticity of a message.
Security of MD5:
• MD5 is considered to be a secure hash function, but it has been
shown to be vulnerable to collision attacks.
• A collision attack is an attack that allows an attacker to find two
messages with the same MD5 hash value.
• collision attack is an attack that allows an attacker to find two messages with
the same hash value. This does not mean that the attacker can find the
original message from the hash value, but it does mean that the attacker can
create a fake message that has the same hash value as the original message
• This does not mean that MD5 can be used to crack passwords, but it
does mean that MD5 should not be used for applications where
security is critical.
Advantages of MD5
• Speed: MD5 is a fast hash function, which makes it ideal for
applications where speed is important, such as verifying the integrity
of large files.
• Efficiency: MD5 is also an efficient hash function, which means that it
uses relatively few resources, such as CPU time and memory.
• Widespread support: MD5 is widely supported by software, which
makes it easy to use in a variety of applications.
• History of use: MD5 has a long history of use and is considered to be
a secure hash function.
Limitations of MD5
• Vulnerability to collision attacks: MD5 has been shown to be vulnerable to
collision attacks. This means that it is possible for an attacker to find two
different messages that have the same MD5 hash value. While this does
not mean that MD5 can be used to crack passwords, it does mean that
MD5 should not be used for applications where security is critical.
• Not considered to be a secure hash function for new applications: MD5 is
still considered to be a secure hash function for many applications.
However, it is no longer considered to be a secure hash function for new
applications. This is because MD5 has been shown to be vulnerable to
collision attacks.
• Overall, MD5 is a good choice for applications where speed and efficiency
are important. However, MD5 should not be used for applications where
security is critical.
Implementation of MD5
What is SHA-1?
• SHA-1, which stands for Secure Hash Algorithm 1, is a cryptographic
hash function that produces a 160-bit hash value.
• It was designed by the United States National Security Agency (NSA)
and is a U.S. Federal Information Processing Standard (FIPS).
• It is widely used for a variety of purposes, including verifying the
integrity of files, passwords, and digital signatures.
How does SHA-1 work?
• SHA-1 works by taking a message of any length and dividing it into
512-bit blocks.
• Each block is then processed using a series of mathematical
operations to produce a 160-bit hash value.
• The hash value is a unique identifier for the message, and can be used
to verify that the message has not been tampered with.
Why is SHA-1 important?
• SHA-1 is important because it is a widely used and trusted
cryptographic hash function. It is used in a variety of applications,
including:
• File verification: SHA-1 can be used to verify that a file has not been tampered
with.
• Password hashing: SHA-1 can be used to hash passwords, making it more
difficult for attackers to crack them.
• Digital signatures: SHA-1 can be used to create digital signatures, which can
be used to verify the authenticity of a message.
SHA-1 Application
• File verification: SHA-1 can be used to verify that a file has not been tampered with. A file is
hashed and the hash value is stored in a database. When the file is downloaded, it is hashed
again and the hash value is compared to the hash value in the database. If the hash values do
not match, then the file has been tampered with.
• Password hashing: SHA-1 can be used to hash passwords, making it more difficult for
attackers to crack them. When a user creates a password, the password is hashed using SHA-
1 and the hash value is stored in the database. When the user logs in, the password is hashed
again and the hash value is compared to the hash value in the database. If the hash values
match, then the user is logged in.
• Digital signatures: SHA-1 can be used to create digital signatures, which can be used to verify
the authenticity of a message. A digital signature is created by using a private key to hash the
message. The hash value is then encrypted with a public key and attached to the message.
The recipient of the message can use the public key to decrypt the hash value and verify that
the message has not been tampered with.
Is SHA-1 secure?
• SHA-1 is considered to be a secure cryptographic hash function.
However, it has been shown to be vulnerable to collision attacks
• A collision attack is an attack that allows an attacker to find two
messages with the same SHA-1 hash value.
• This does not mean that SHA-1 can be used to crack passwords, but it
does mean that SHA-1 should not be used for applications where
security is critical.
Advantage of SHA-1
• Speed: SHA-1 is a fast hash function, which makes it ideal for
applications where speed is important, such as file verification and
password hashing.
• Efficiency: SHA-1 is an efficient hash function, which means that it can
be implemented in software and hardware with relatively little
resources.
• Widespread support: SHA-1 is widely supported by software, which
makes it easy to use in a variety of applications.
• Security: SHA-1 was designed to be a secure hash function, and it has
a long history of use without any known vulnerabilities.
Limitation of SHA-1
• Vulnerability to collision attacks: SHA-1 has been shown to be vulnerable to
collision attacks. This means that it is possible for an attacker to find two
messages with the same SHA-1 hash value. This does not mean that SHA-1
can be used to crack passwords, but it does mean that SHA-1 should not be
used for applications where security is critical.
• Short output: The output of SHA-1 is only 160 bits long. This means that
there are a limited number of possible hash values, which makes it easier
for an attacker to find a collision.
• If you are looking for a secure hash function, you should use a newer hash
function, such as SHA-256 or SHA-512. These hash functions have been
designed to be more resistant to collision attacks.
SHA-1 Implementation Example
SHA-1 Implementation Example
SHA-256
• A cryptographic hash function that produces a 256-bit hash value.
• Developed by the National Security Agency (NSA) in 2001.
• Considered to be more secure than SHA-1.
• Used in a variety of applications, including file verification, password
hashing, and digital signatures.
SHA-256
How SHA-256 works
• The message is padded to a multiple of 512 bits.
• The message is divided into 512-bit blocks.
• Each block is processed using a series of 64 rounds.
• The output of the 64 rounds is the hash value.
Applications of SHA-256
• File verification: SHA-256 can be used to verify the integrity of files. The hash value of
a file is calculated before the file is sent to the recipient. The recipient then calculates
the hash value of the file they received and compares it to the hash value that was
sent. If the hash values are the same, then the file has not been tampered with.
• Password hashing: SHA-256 can be used to hash passwords. This makes it more
difficult for attackers to crack passwords. When a user creates a password, the
password is hashed using SHA-256 and the hash value is stored in the database. When
the user logs in, the password they enter is hashed using SHA-256 and the hash value
is compared to the hash value in the database. If the hash values match, then the user
is logged in.
• Digital signatures: SHA-256 can be used to create digital signatures. A digital signature
is a way to verify the authenticity of a message. When a sender signs a message, they
use their private key to hash the message. The hash value is then encrypted with their
public key and attached to the message. The recipient can use the sender's public key
to decrypt the hash value and verify that the message has not been tampered with.
Security of SHA-256
• SHA-256 is considered to be a secure cryptographic hash function.
However, it is important to note that no cryptographic hash function
is completely secure.
• As computing power increases, it is possible that attackers will be
able to find collisions for SHA-256. However, this is not expected to
happen for many years.
SHA-256 Advantages

• Security: SHA-256 is considered to be one of the most secure hash


functions available. It has been designed with a number of security
features that make it difficult for attackers to find collisions or preimages.
• Speed: SHA-256 is relatively fast. It can hash data at a rate of about 60
MB/s on a modern CPU. This makes it suitable for use in applications where
speed is important, such as file verification and password hashing.
• Versatility: SHA-256 can be used in a variety of applications. It is commonly
used for file verification, password hashing, and digital signatures.
SHA-256 Limitations
• Computational expense: SHA-256 is computationally expensive. It
requires a lot of computing power to hash data. This can be a
limitation in some applications, such as embedded devices.
• Not perfect: No cryptographic hash function is completely secure. As
computing power increases, it is possible that attackers will be able to
find collisions for SHA-256. However, this is not expected to happen
for many years.
• Overall, SHA-256 is a very secure and versatile cryptographic hash
function. It is considered to be a good choice for applications where
security is important.
SHA-256 Implementation Example
SHA-256 Implementation Example
SHA-512
• A cryptographic hash function that produces a 512-bit hash value.
• Developed by the National Security Agency (NSA) in 2001.
• Considered to be more secure than SHA-256.
• Used in a variety of applications, including file verification, password
hashing, and digital signatures.
How SHA-512 works
• The message is padded to a multiple of 1024 bits.
• The message is divided into 1024-bit blocks.
• Each block is processed using a series of 80 rounds.
• The output of the 80 rounds is the hash value.
Applications of SHA-512
• File verification: SHA-512 can be used to verify the integrity of files. The hash value of
a file is calculated before the file is sent to the recipient. The recipient then calculates
the hash value of the file they received and compares it to the hash value that was
sent. If the hash values are the same, then the file has not been tampered with.
• Password hashing: SHA-512 can be used to hash passwords. This makes it more
difficult for attackers to crack passwords. When a user creates a password, the
password is hashed using SHA-512 and the hash value is stored in the database. When
the user logs in, the password they enter is hashed using SHA-512 and the hash value
is compared to the hash value in the database. If the hash values match, then the user
is logged in.
• Digital signatures: SHA-512 can be used to create digital signatures. A digital signature
is a way to verify the authenticity of a message. When a sender signs a message, they
use their private key to hash the message. The hash value is then encrypted with their
public key and attached to the message. The recipient can use the sender's public key
to decrypt the hash value and verify that the message has not been tampered with.
Security of SHA-512
• SHA-512 is considered to be a secure cryptographic hash function.
• However, it is important to note that no cryptographic hash function
is completely secure.
• As computing power increases, it is possible that attackers will be
able to find collisions for SHA-512. However, this is not expected to
happen for many years.
Advantages of SHA-512
• Security: SHA-512 is considered to be one of the most secure hash
functions available. It has been designed with a number of security
features that make it difficult for attackers to find collisions or
preimages.
• Speed: SHA-512 is relatively fast. It can hash data at a rate of about 20
MB/s on a modern CPU. This makes it suitable for use in applications
where speed is important, such as file verification and password
hashing.
• Versatility: SHA-512 can be used in a variety of applications. It is
commonly used for file verification, password hashing, and digital
signatures.
Limitations of SHA-512
• Computational expense: SHA-512 is computationally expensive. It
requires a lot of computing power to hash data. This can be a
limitation in some applications, such as embedded devices.
• Not perfect: No cryptographic hash function is completely secure. As
computing power increases, it is possible that attackers will be able to
find collisions for SHA-512. However, this is not expected to happen
for many years.
• Overall, SHA-512 is a very secure and versatile cryptographic hash
function. It is considered to be a good choice for applications where
security is important.
SHA-512 Implementation Example
SHA-512 Implementation Example
Keccak256
• Keccak256 is a member of the SHA-3 family of hash functions, which
was selected as the winner of the NIST hash function competition in
2012.
• It was developed by a team of cryptographers led by Guido Bertoni,
Joan Daemen, and Gilles Van Assche.
• Keccak256 is designed to be secure against a wide range of attacks,
including preimage attacks, collision attacks, and length extension
attacks.
• It is also efficient, with a relatively low computational cost and a
simple implementation.
How does Keccak-256 work?
• Keccak-256 is a sponge function. A sponge function is a function that
takes an input of any size and produces an output of a fixed size.
Keccak-256 takes an input of any size and produces an output of 256
bits.
• Keccak-256 works by first expanding the input into a larger state. The
state is then processed using a series of rounds. Each round consists
of a number of operations, including rotations, permutations, and
additions.
• After the final round, the state is compressed to produce the output.
• The Keccak-256 state is a 1600-bit word. The state is initialized to all
zeros.
The Keccak-256 rounds
• There are 24 rounds in the Keccak-256 algorithm. Each round consists
of the following steps:
• The state is expanded using a permutation called the Keccak-P permutation.
• The state is divided into two halves.
• A series of operations are performed on the two halves of the state.
• The two halves of the state are swapped.
• The state is compressed using a permutation called the Keccak-R
permutation.
Security of Keccak-256
• Keccak-256 is considered to be a very secure cryptographic hash
function.
• It has been subjected to a number of attacks, but no attack has been
successful in finding a collision or preimage.
Applications of Keccak-256
• File verification: Keccak-256 can be used to verify the integrity of files. The hash value of a file is
calculated before the file is sent to the recipient. The recipient then calculates the hash value of the
file they received and compares it to the hash value that was sent. If the hash values are the same,
then the file has not been tampered with.
• Password hashing: Keccak-256 can be used to hash passwords. This makes it more difficult for
attackers to crack passwords. When a user creates a password, the password is hashed using Keccak-
256 and the hash value is stored in the database. When the user logs in, the password they enter is
hashed using Keccak-256 and the hash value is compared to the hash value in the database. If the
hash values match, then the user is logged in.
• Digital signatures: Keccak-256 can be used to create digital signatures. A digital signature is a way to
verify the authenticity of a message. When a sender signs a message, they use their private key to
hash the message. The hash value is then encrypted with their public key and attached to the
message. The recipient can use the sender's public key to decrypt the hash value and verify that the
message has not been tampered with.
Keccak-256 Advantages
• Security: Keccak-256 is considered to be a very secure cryptographic hash
function. It has been subjected to a number of attacks, but no attack has
been successful in finding a collision or preimage.
• Performance: Keccak-256 is a relatively fast cryptographic hash function. It
can hash data at a rate of about 20 MB/s on a modern CPU.
• Versatility: Keccak-256 can be used in a variety of applications. It is commonly
used for file verification, password hashing, and digital signatures.
Keccak-256 Limitations
• Computational expense: Keccak-256 is computationally expensive. It
requires a lot of computing power to hash data. This can be a
limitation in some applications, such as embedded devices.
• Not perfect: No cryptographic hash function is completely secure. As
computing power increases, it is possible that attackers will be able to
find collisions for Keccak-256. However, this is not expected to
happen for many years.
Limitations Implementation Example
Limitations Implementation Example
RIPEMD (RACE Integrity Primitives Evaluation
Message Digest)
• RIPEMD (RACE Integrity Primitives Evaluation Message Digest) is a
family of cryptographic hash functions. Although there are several
versions of RIPEMD
• RIPEMD-128
• RIPEMD-160
• RIPEMD-256
• RIPEMD-320
• I'll provide a depth overview of RIPEMD-160, which is one of the
most widely used variants.
RIPEMD-160 how works?
• Message Padding: RIPEMD-160 processes the input message in blocks of 512 bits. If the message
length is not a multiple of 512 bits, it is padded with additional bits to reach the next multiple. The
padding includes a length representation of the original message to ensure data integrity.
• Message Expansion: The padded message is divided into 16 words, each consisting of 32 bits. These
words are then expanded into an internal buffer of 80 words using a nonlinear function that
introduces diffusion.
• Compression Function: The compression function iterates through the 80-word buffer, performing a
series of bitwise operations, logical functions, and modular addition operations. This process mixes
the input data and updates the internal state of the hash function.
• Output Transformation: After the compression function processes all 80 words, the resulting internal
state is transformed to produce the final hash value. This transformation involves combining and
manipulating the words using logical and bitwise operations.
• Final Hash Value: The output of the transformation step is a 160-bit hash value, represented as a
sequence of 40 hexadecimal characters. This value is the unique representation of the input message.
RIPEMD-160 Security
• RIPEMD-160 is a cryptographic hash function that was designed by
the RIPEMD team as part of the RACE Integrity Primitives Evaluation,
in 1992.
• It is considered to be a secure hash function, and has not been
broken to date.
• However, it is not as secure as newer hash functions, such as SHA-256
or SHA-3.
RIPEMD-160 Application
• File verification: RIPEMD-160 can be used to verify the integrity of files. The hash value of a file is
calculated before the file is sent to the recipient. The recipient then calculates the hash value of the file
they received and compares it to the hash value that was sent. If the hash values are the same, then the
file has not been tampered with.
• Password hashing: RIPEMD-160 can be used to hash passwords. This makes it more difficult for
attackers to crack passwords. When a user creates a password, the password is hashed using RIPEMD-
160 and the hash value is stored in the database. When the user logs in, the password they enter is
hashed using RIPEMD-160 and the hash value is compared to the hash value in the database. If the hash
values match, then the user is logged in.
• Digital signatures: RIPEMD-160 can be used to create digital signatures. A digital signature is a way to
verify the authenticity of a message. When a sender signs a message, they use their private key to hash
the message. The hash value is then encrypted with their public key and attached to the message. The
recipient can use the sender's public key to decrypt the hash value and verify that the message has not
been tampered with.
RIPEMD-160 Application
• Cryptocurrencies: RIPEMD-160 is used to generate the addresses of
Bitcoin and other cryptocurrencies.
• Data integrity checks: RIPEMD-160 is used to verify the integrity of
data stored on a variety of media, such as hard drives, CDs, and DVDs.
• Fraud detection: RIPEMD-160 is used to detect fraud in a variety of
applications, such as credit card transactions and online banking.
RIPEMD-160 Advantages
• Speed: RIPEMD-160 is a relatively fast hash function. It can hash data
at a rate of about 10 MB/s on a modern CPU.
• Security: RIPEMD-160 was designed to be a secure hash function. It
has not been broken to date, and is considered to be a good choice
for applications where security is important.
• Versatility: RIPEMD-160 can be used in a variety of applications. It is
commonly used for file verification, password hashing, and digital
signatures.
RIPEMD-160 Limitations
• Not as secure as newer hash functions: RIPEMD-160 is not as secure
as newer hash functions, such as SHA-256 or SHA-3. This is because it
has been around for longer and has been more thoroughly analyzed.
• Not as efficient as newer hash functions: RIPEMD-160 is not as
efficient as newer hash functions, such as SHA-256 or SHA-3. This is
because it uses a more complex algorithm.
RIPEMD-160 Implementation Example
RIPEMD-160 Implementation Example
Hash Function Analysis
Hash Function Bit Size Speed Security Applications

File verification, password


MD5 128 Fast Vulnerable to collisions
hashing, digital signatures

File verification, password


SHA-1 160 Fast Vulnerable to collisions
hashing, digital signatures

File verification, password


SHA-256 256 Moderate Secure hashing, digital
signatures, blockchain

File verification, password


SHA-512 512 Slow Secure hashing, digital
signatures, blockchain

File verification, password


Keccak256 256 Moderate Secure hashing, digital
signatures, blockchain

File verification, password


RIPEMD 128, 160, 256, 320 Fast Vulnerable to collisions
hashing, digital signatures

You might also like