Receive Self Mac : SHA Secure Hash Algorithm « Security « Java Tutorial






import java.io.FileInputStream;
import java.io.ObjectInputStream;
import java.security.MessageDigest;

public class MainClass {
  public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object o = ois.readObject();
    if (!(o instanceof String)) {
      System.out.println("Unexpected data in file");
      System.exit(-1);
    }
    String data = (String) o;
    System.out.println("Got message " + data);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
      System.out.println("Unexpected data in file");
      System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    byte pass[] = "aaa".getBytes();
    byte buf[] = data.getBytes();
    MessageDigest md = MessageDigest.getInstance("SHA");
    md.update(pass);
    md.update(buf);
    byte digest1[] = md.digest();
    md.update(pass);
    md.update(digest1);
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
  }
}








36.43.SHA Secure Hash Algorithm
36.43.1.Save SHA Message
36.43.2.SHA MessageDigest
36.43.3.Receive Self Mac
36.43.4.An alternate SHA Interleave algorithm as implemented in the SRP distribution