Create an encrypted string for password : MessageDigest « Security « Java Tutorial






import java.security.MessageDigest;

public class Main {
  public static void main(String[] args) throws Exception {
    String password = "secret";
    String algorithm = "SHA";

    byte[] plainText = password.getBytes();

    MessageDigest md = MessageDigest.getInstance(algorithm);

    md.reset();
    md.update(plainText);
    byte[] encodedPassword = md.digest();

    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < encodedPassword.length; i++) {
      if ((encodedPassword[i] & 0xff) < 0x10) {
        sb.append("0");
      }
      sb.append(Long.toString(encodedPassword[i] & 0xff, 16));
    }
    System.out.println("Plain    : " + password);
    System.out.println("Encrypted: " + sb.toString());
  }
}








36.27.MessageDigest
36.27.1.Message Digest Demo
36.27.2.Digest Stream
36.27.3.Make SHA Message
36.27.4.Make SHA Message with update methods
36.27.5.Masher a file
36.27.6.extends MessageDigest
36.27.7.MD5 MessageDigest
36.27.8.Digest Input
36.27.9.Create an encrypted string for password
36.27.10.Cryptography Streams: URLDigest