Question
pls tracing output for this programs and comment how get output ------------------------------------------------------------------ Program4: package javaapplication7; /*package com.mkyong.test; import java.security.MessageDigest; /** * * @author Dr.Hossam Diab
pls tracing output for this programs and comment how get output
------------------------------------------------------------------
Program4:
package javaapplication7;
/*package com.mkyong.test;
import java.security.MessageDigest;
/**
*
* @author Dr.Hossam Diab
*/
import java.security.MessageDigest;
public class JavaApplication7 {
public static void main(String[] args)throws Exception
{
String password = "1234*5689asdr000";
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(password.getBytes());
byte byteData[] = md.digest();
//convert the byte to hex format method 1
StringBuffer sb = new StringBuffer();
for (int i = 0; i
sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
}
System.out.println("Hex format : " + sb.toString());
//convert the byte to hex format method 2
StringBuffer hexString = new StringBuffer();
for (int i=0;i
String hex=Integer.toHexString(0xff & byteData[i]);
if(hex.length()==1) hexString.append('0');
hexString.append(hex);
}
System.out.println("Hex format : " + hexString.toString());
}
}
Program5:
package javaapplication8;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class JavaApplication8 {
public static void main(String[] args) throws NoSuchAlgorithmException {
String passwordToHash = "password";
byte[] salt = getSalt();
String securePassword = get_SHA_1_SecurePassword(passwordToHash, salt);
System.out.println(securePassword);
}
private static String get_SHA_1_SecurePassword(String passwordToHash, byte[] salt)
{
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
//Add salt
private static byte[] getSalt() throws NoSuchAlgorithmException
{
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
return salt;
}
}
Program6:
package javaapplication9;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
public class JavaApplication9 {
public static void main(String[] args) throws NoSuchAlgorithmException {
String passwordToHash = "password";
byte[] salt = getSalt();
String securePassword = get_SHA_1_SecurePassword(passwordToHash, salt);
System.out.println(securePassword);
securePassword = get_SHA_256_SecurePassword(passwordToHash, salt);
System.out.println(securePassword);
securePassword = get_SHA_384_SecurePassword(passwordToHash, salt);
System.out.println(securePassword);
securePassword = get_SHA_512_SecurePassword(passwordToHash, salt);
System.out.println(securePassword);
}
private static String get_SHA_1_SecurePassword(String passwordToHash, byte[] salt)
{
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-1");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
private static String get_SHA_256_SecurePassword(String passwordToHash, byte[] salt)
{
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
private static String get_SHA_384_SecurePassword(String passwordToHash, byte[] salt)
{
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-384");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
private static String get_SHA_512_SecurePassword(String passwordToHash, byte[] salt)
{
String generatedPassword = null;
try {
MessageDigest md = MessageDigest.getInstance("SHA-512");
md.update(salt);
byte[] bytes = md.digest(passwordToHash.getBytes());
StringBuilder sb = new StringBuilder();
for(int i=0; i
{
sb.append(Integer.toString((bytes[i] & 0xff) + 0x100, 16).substring(1));
}
generatedPassword = sb.toString();
}
catch (NoSuchAlgorithmException e)
{
e.printStackTrace();
}
return generatedPassword;
}
//Add salt
private static byte[] getSalt() throws NoSuchAlgorithmException
{
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
byte[] salt = new byte[16];
sr.nextBytes(salt);
return salt;
}
}
Output-JavaApplication1 (run) Hex format 768f151f011adac0213beac29cd34509 Hex format 768f151f011adac0213beac29cd34509 BUILD SUCCESSFUL (total time: 0 seconds) Output- JavaApplication1 (run) cbb29d8c2cd2aa3a8d787a613bed0418 e3518fla2330c1e06e76889cafd8d74058b93691eb1978ed 0013dd6d6f1c77bfbc420fbc4823f182cf93e4294ef99dbd38d4931201e9bb6a289d95b6b18d0 3 8 0 3 5 0 3 0 2 9c90a 9b5067bf9428b37924fbl5649238843427d20a391b468bb4dcd293d670e762a9150137066ff4efs6ad8512f586a678e5c2590b2408607b9357 BUILD SUCCESSFUL (total time: 1 second) d2dc1a48 98c56bd1cbb80777 d72b5bc0d47257 Output-JavaApplication1 (run) Hex format 768f151f011adac0213beac29cd34509 Hex format 768f151f011adac0213beac29cd34509 BUILD SUCCESSFUL (total time: 0 seconds) Output- JavaApplication1 (run) cbb29d8c2cd2aa3a8d787a613bed0418 e3518fla2330c1e06e76889cafd8d74058b93691eb1978ed 0013dd6d6f1c77bfbc420fbc4823f182cf93e4294ef99dbd38d4931201e9bb6a289d95b6b18d0 3 8 0 3 5 0 3 0 2 9c90a 9b5067bf9428b37924fbl5649238843427d20a391b468bb4dcd293d670e762a9150137066ff4efs6ad8512f586a678e5c2590b2408607b9357 BUILD SUCCESSFUL (total time: 1 second) d2dc1a48 98c56bd1cbb80777 d72b5bc0d47257
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started