001package edu.pdx.cs410J.security;
002
003import java.security.*;
004
005/**
006 * This program computes the SHA digest of a sentence specified on the
007 * command line.
008 */
009public class PrintDigest {
010
011  public static void main(String[] args) {
012    String message = String.join(" ", args);
013    MessageDigest algorithm = null;
014    try {
015      algorithm = MessageDigest.getInstance("SHA");
016    } catch (NoSuchAlgorithmException ex) {
017      ex.printStackTrace(System.err);
018      System.exit(1);
019    }
020
021    algorithm.reset();
022    algorithm.update(message.getBytes());
023    byte[] digest = algorithm.digest();
024
025    StringBuilder hexString = new StringBuilder();
026    for (byte b : digest) {
027      String s = Integer.toHexString(0xFF & b);
028      hexString.append(s);
029    }
030    
031    System.out.println("Message: " + message);
032    System.out.println("Digest: " + hexString);
033  }
034}