EXPERIMENT-07
Aim: SHA-1 algorithm
The objective of the SHA-1 (Secure Hash Algorithm 1) algorithm is to demonstrate the
process of producing a fixed-size, cryptographic hash value from variable-length input data.
The primary purpose of SHA-1 is to ensure data integrity and authenticity by generating a
unique hash value, for a given set of data.
Theory:
SHA-1 is a cryptographic hash function that was widely used for data integrity and digital
signature applications. It was designed by the National Security Agency (NSA) and published
by the National Institute of Standards and Technology (NIST) in 1993. However, its security
has been compromised due to vulnerabilities found in its design, and its use has been
deprecated in modern cryptographic applications.
Key characteristics of the SHA-1 algorithm:
1. Hash Function: SHA-1 is a one-way hash function, which means it takes an input
message of any length and produces a fixed-size output, known as the hash value or message
digest. The SHA-1 hash value is 160 bits (20 bytes) in length, typically represented as a
sequence of hexadecimal digits.
2. Collision Resistance: A fundamental property of a secure hash function is collision
resistance, meaning it should be computationally infeasible to find two different inputs that
produce the same hash value. Unfortunately, researchers have demonstrated that SHA-1 is
vulnerable to collision attacks, where two distinct messages can produce the same hash value.
3. Data Integrity and Authentication: SHA-1 was commonly used to verify the integrity
of data during transmission or storage. By calculating the SHA-1 hash value of a message
before and after transmission, one can ensure that the data has not been altered in transit. It
was also used in digital signature schemes to provide authentication and non-repudiation.
4. Fixed Output Size: SHA-1 always produces a 160-bit hash value, regardless of the
size of the input data. This fixed-length output makes it convenient for use in various
cryptographic applications.
Source Code:
import java.math.BigInteger ; import
java.nio.charset.StandardCharsets ; import
java.security.MessageDigest ; import
java.security.NoSuchAlgorithmException ; //
Program in Java for calculating SHA hash value
class JavaSha{
public static byte[ ] getSHA( String input ) throws NoSuchAlgorithmException
{
// Static getInstance method is called with hashing SHA
// Make sure you pass "SHA-256" without any spaces
// and just a '-' in between properly
MessageDigest md = MessageDigest.getInstance( "SHA-256" ) ;
// To caculate message digest of an input
// digest( ) method is called
// which returns an array of bytes
return md.digest( input.getBytes( StandardCharsets.UTF_8 ) ) ;
}
public static String toHexString( byte[ ] hash )
{
// For converting byte array into signum representation
BigInteger number = new BigInteger( 1, hash ) ;
// For converting message digest into hex value
StringBuilder hexString = new StringBuilder( number.toString( 16 ) ) ;
// Pad with leading zeros
while ( hexString.length( ) < 32 )
{
hexString.insert( 0, " 0 " ) ;
}
return hexString.toString( ) ;
}
public static void main( String args[ ] )
{
try
{
System.out.println( " HashCode Generated by SHA - 256 for : " ) ;
String s1 = "Ayushi" ;
System.out.println( " \n " + s1 + " : " + toHexString( getSHA( s1 ) ) ) ;
String s2 = "HashFunc" ;
System.out.println( " \n " + s2 + " : " + toHexString( getSHA( s2 ) ) ) ;
}
// For specifying wrong message digest algorithms
catch ( NoSuchAlgorithmException e ) {
System.out.println( " Exception thrown for incorrect algorithm : " + e ) ;
}
}
}
Output:
SUBMITTED BY:-
NAME :AMIT KUMAR
REGN:2201030045
BRANCH:CSE(AI&ML)
SEMESTER:5th