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

Cryptography and Network Security

LAB-8

Name: Tejas Raikwar


Regno: 21BCE1063
Subject: Cryptography and network security
Slot: L9+L10
Aim: To compare time complexity in calculation between
sha1 and sha 256 algorithm.

Code:
SHA – 1:
// SHA - 1
import java.util.*;
import java.lang.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class sha1{

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.println("Enter String : ");
String input = in.next();
try {
long startTime = System.nanoTime();
byte[] hash = SHA1(input.getBytes());
long endTime = System.nanoTime();
System.out.println("SHA-1 Hash: " +
bytesToHex(hash));
System.out.println("Time taken: " + (endTime -
startTime) + " nanoseconds");
double seconds = (endTime - startTime) /
1_000_000_000.0;
System.out.println("Seconds : " + seconds + "
seconds");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

public static byte[] SHA1(byte[] input) throws


NoSuchAlgorithmException {
MessageDigest digest =
MessageDigest.getInstance("SHA-1");
return digest.digest(input);
}
public static String bytesToHex(byte[] bytes) {
StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
Output:
SHA – 256:
// SHA - 256

import java.util.*;
import java.lang.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class sha256{

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.println("Enter String : ");
String input = in.next();
try {
long startTime = System.nanoTime();
byte[] hash = SHA256(input.getBytes());
long endTime = System.nanoTime();
System.out.println("SHA-256 Hash: " +
bytesToHex(hash));
System.out.println("Time taken: " + (endTime -
startTime) + " nanoseconds");
double seconds = (endTime - startTime) /
1_000_000_000.0;
System.out.println("Seconds : " + seconds + "
seconds");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

public static byte[] SHA256(byte[] input) throws


NoSuchAlgorithmException {
MessageDigest digest =
MessageDigest.getInstance("SHA-256");
return digest.digest(input);
}

public static String bytesToHex(byte[] bytes) {


StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
output:
MD5

Code

import java.util.*;
import java.lang.*;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5{

public static void main(String[] args) {


Scanner in = new Scanner(System.in);
System.out.println("Enter String : ");
String input = in.next();
try {
long startTime = System.nanoTime();
byte[] hash = md5(input.getBytes());
long endTime = System.nanoTime();
System.out.println("SHA-1 Hash: " + bytesToHex(hash));
System.out.println("Time taken: " + (endTime - startTime) + " nanoseconds");
double seconds = (endTime - startTime) / 1_000_000_000.0;
System.out.println("Time taken : " + seconds + " seconds");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}
}

public static byte[] md5(byte[] input) throws NoSuchAlgorithmException {


MessageDigest digest = MessageDigest.getInstance("MD5");
return digest.digest(input);
}

public static String bytesToHex(byte[] bytes) {


StringBuilder result = new StringBuilder();
for (byte b : bytes) {
result.append(String.format("%02x", b));
}
return result.toString();
}
}
Output

You might also like