MessageDigest: isEqual(byte[] digesta, byte[] digestb) : MessageDigest « java.security « Java by API






MessageDigest: isEqual(byte[] digesta, byte[] digestb)

  


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

public class Main {
  public static void main(String args[]) throws Exception {
    FileInputStream fis = new FileInputStream("test");
    MessageDigest md = MessageDigest.getInstance("SHA");
    DigestInputStream dis = new DigestInputStream(fis, md);
    ObjectInputStream ois = new ObjectInputStream(dis);
    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);
    dis.on(false);
    o = ois.readObject();
    if (!(o instanceof byte[])) {
      System.out.println("Unexpected data in file");
      System.exit(-1);
    }
    byte origDigest[] = (byte[]) o;
    System.out.println(MessageDigest.isEqual(md.digest(), origDigest));
  }
}

   
    
  








Related examples in the same category

1.MessageDigest: getInstance(String algorithm)
2.extends MessageDigest