Basic IO example using SHA1 : SHA1 Secure Hash Algorithm « Security « Java Tutorial






import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.security.DigestInputStream;
import java.security.DigestOutputStream;
import java.security.MessageDigest;

public class MainClass {
  public static void main(String[] args) throws Exception {
    byte[] input = "1234567".getBytes();

    MessageDigest hash = MessageDigest.getInstance("SHA1");

    ByteArrayInputStream bIn = new ByteArrayInputStream(input);
    DigestInputStream dIn = new DigestInputStream(bIn, hash);
    ByteArrayOutputStream bOut = new ByteArrayOutputStream();

    int ch;
    while ((ch = dIn.read()) >= 0) {
      bOut.write(ch);
    }
    byte[] newInput = bOut.toByteArray();
    System.out.println("in digest : " + new String(dIn.getMessageDigest().digest()));

    DigestOutputStream dOut = new DigestOutputStream( new ByteArrayOutputStream(), hash);
    dOut.write(newInput);
    dOut.close();
    System.out.println("out digest: " + new String(dOut.getMessageDigest().digest()));
  }
}








36.42.SHA1 Secure Hash Algorithm
36.42.1.Basic IO example using SHA1
36.42.2.PSS parameter recovery and encoding
36.42.3.Use SHA1
36.42.4.Encrypt a password