| |
| 1 |
package cn7.javasec.blatt8; |
| 2 |
|
| 3 |
import java.io.BufferedInputStream; |
| 4 |
import java.io.ByteArrayInputStream; |
| 5 |
import java.io.FileInputStream; |
| 6 |
import java.security.DigestInputStream; |
| 7 |
import java.security.MessageDigest; |
| 8 |
|
| 9 |
public class Aufgabe2b { |
| 10 |
|
| 11 |
/** |
| 12 |
* @param args |
| 13 |
*/ |
| 14 |
public static void main(String[] args) { |
| 15 |
String alg = "SHA-512"; |
| 16 |
//alg = "MD5"; |
| 17 |
|
| 18 |
try { |
| 19 |
MessageDigest md = MessageDigest.getInstance(alg); |
| 20 |
|
|
DigestInputStream dis = new DigestInputStream(new FileInputStream("digest.file"), md); |
| 22 |
|
| 23 |
while ((dis.read()) != -1); |
| 24 |
dis.close(); |
| 25 |
|
| 26 |
// out |
| 27 |
byte[] out = md.digest(); |
| 28 |
BufferedInputStream br = new BufferedInputStream(new ByteArrayInputStream(out)); |
| 29 |
int k = 0; |
| 30 |
String output = ""; |
| 31 |
while ((k = br.read()) != -1) { |
| 32 |
k = k & 0xFF; |
| 33 |
if (k < 16) |
| 34 |
output += "0"; |
| 35 |
output += Integer.toString(k, 16).toUpperCase() + " "; |
| 36 |
} |
| 37 |
System.out.println("OUT:\n" +output); |
| 38 |
|
| 39 |
|
| 40 |
System.out.println(); |
| 41 |
} catch (Exception e) { |
| 42 |
e.printStackTrace(); |
| 43 |
} |
| 44 |
|
| 45 |
} |
| 46 |
|
| 47 |
} |
| |