I've just started a new course in Java Network Programming, and consequently, I'm reading the book with the same title. Now somewhere around pages 70 to 90 the book brings up Digest Stream and Encryption. However, I just don't understand the difference. I get what encryption is, but when I read about Digest Stream it just strikes me as pretty much the same thing. To scramble the stream so that it cannot be read by another. So basically I would like a little explanation about the differences between these, and what they are used for.
Also, while I'm on the topic. I actually haven't touched up on my Java since about 2006-2007, and even then I was mostly working with relatively basic stuff. And unfortunately, by now, I've forgotten a great big part of what I once knew. In the book, on a page a bit further ahead that deals with Treads, there is a bit of code that brings up a few things that I recall using years ago but forgot what they do. Like toString(), append(). And coincidentally, it also uses Digest, so I thought it would be a good code to have explained to me. So, if anyone would be so kind as to give me a line for line explanation of what the code bellow does.
import.java.io.*;
import java.security.*;
public class DigestThread extends Thread{
private File input;
public DigestThread(File input){
this.input = input;
}
public void run(){
try{
FileInputStream in = new FileInputStream(input);
MessageDigest sha = new MessageDigest.getInstance("SHA");
DigestInputStream din = new DigestInputStream(in, sha);
int b;
while ((b = din.read()) != -1);
din.close();
byte[] digest = sha.digest();
StringBuffer result = new StringBuffer(input.toString());
result.append(": ");
for(int i = 0; i < digest.length; i++){
result.append(digest[i] + " ");
}
System.out.println(result);
}catch (IOException ex){
System.err.println(ex);
}catch (NoSuchAlgorithmException ex){
System.err.println(ex);
}
}
public static void main(String[] args){
for(int i = 0; i < args.length; i++){
File f = new File(args[i]);
Thread t = new DigestThread(f);
t.start();
}
}
}
Question
Razorblade
Hi,
I've just started a new course in Java Network Programming, and consequently, I'm reading the book with the same title. Now somewhere around pages 70 to 90 the book brings up Digest Stream and Encryption. However, I just don't understand the difference. I get what encryption is, but when I read about Digest Stream it just strikes me as pretty much the same thing. To scramble the stream so that it cannot be read by another. So basically I would like a little explanation about the differences between these, and what they are used for.
Also, while I'm on the topic. I actually haven't touched up on my Java since about 2006-2007, and even then I was mostly working with relatively basic stuff. And unfortunately, by now, I've forgotten a great big part of what I once knew. In the book, on a page a bit further ahead that deals with Treads, there is a bit of code that brings up a few things that I recall using years ago but forgot what they do. Like toString(), append(). And coincidentally, it also uses Digest, so I thought it would be a good code to have explained to me. So, if anyone would be so kind as to give me a line for line explanation of what the code bellow does.
import.java.io.*; import java.security.*; public class DigestThread extends Thread{ private File input; public DigestThread(File input){ this.input = input; } public void run(){ try{ FileInputStream in = new FileInputStream(input); MessageDigest sha = new MessageDigest.getInstance("SHA"); DigestInputStream din = new DigestInputStream(in, sha); int b; while ((b = din.read()) != -1); din.close(); byte[] digest = sha.digest(); StringBuffer result = new StringBuffer(input.toString()); result.append(": "); for(int i = 0; i < digest.length; i++){ result.append(digest[i] + " "); } System.out.println(result); }catch (IOException ex){ System.err.println(ex); }catch (NoSuchAlgorithmException ex){ System.err.println(ex); } } public static void main(String[] args){ for(int i = 0; i < args.length; i++){ File f = new File(args[i]); Thread t = new DigestThread(f); t.start(); } } }Thanks in advance,
Raz
Link to comment
Share on other sites
4 answers to this question
Recommended Posts