Question
pls tracing output for this programs and comment how get output ------------------------------------------------------------------ Program1: package javaapplication4; /*package com.mkyong.test; import java.security.MessageDigest; /** * * @author Dr.Hossam Diab
pls tracing output for this programs and comment how get output
------------------------------------------------------------------
Program1:
package javaapplication4;
/*package com.mkyong.test;
import java.security.MessageDigest;
/**
*
* @author Dr.Hossam Diab
*/
import java.security.MessageDigest;
public class JavaApplication4 {
public static void main(String[] args)throws Exception
{
String password = "1234*5689";
MessageDigest md = MessageDigest.getInstance("SHA-256");
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()); } } Program2: package javaapplication5; import java.io.FileInputStream; import java.security.MessageDigest; /** * * @author Dr.Hossam Diab */ public class JavaApplication5 { public static void main(String[] args)throws Exception { MessageDigest md = MessageDigest.getInstance("SHA-256"); FileInputStream fis = new FileInputStream("d:\\loging.txt"); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }; byte[] mdbytes = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i sb.append(Integer.toString((mdbytes[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 hexString.append(Integer.toHexString(0xFF & mdbytes[i])); } System.out.println("Hex format : " + hexString.toString()); } } Program3: package javaapplication6; import java.io.FileInputStream; import java.security.MessageDigest; /** * * @author Dr.Hossam Diab */ public class JavaApplication6 { public static void main(String[] args)throws Exception { MessageDigest md = MessageDigest.getInstance("MD5"); FileInputStream fis = new FileInputStream("d:\\loging.txt"); byte[] dataBytes = new byte[1024]; int nread = 0; while ((nread = fis.read(dataBytes)) != -1) { md.update(dataBytes, 0, nread); }; byte[] mdbytes = md.digest(); //convert the byte to hex format method 1 StringBuffer sb = new StringBuffer(); for (int i = 0; i sb.append(Integer.toString((mdbytes[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 hexString.append(Integer.toHexString(0xFF & mdbytes[i])); } System.out.println("Hex format : " + hexString.toString()); } } 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; } }
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