Password Authenticator : MD5 Message Digest algorithm  « Security « Java Tutorial






import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.security.MessageDigest;
import java.util.Arrays;

public class MainClass {
  public static void main(String[] args) throws Exception {
    authenticatePassword("password");
  }

  private static void authenticatePassword(String password) throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    FileInputStream fis = new FileInputStream("password");
    int theByte = 0;
    while ((theByte = fis.read()) != -1) {
      baos.write(theByte);
    }
    fis.close();
    byte[] hashedPasswordWithSalt = baos.toByteArray();
    baos.reset();

    byte[] salt = new byte[12];
    System.arraycopy(hashedPasswordWithSalt, 0, salt, 0, 12);

    MessageDigest md = MessageDigest.getInstance("MD5");
    md.update(salt);
    md.update(password.getBytes("UTF8"));
    byte[] digest = md.digest();

    byte[] digestInFile = new byte[hashedPasswordWithSalt.length - 12];
    System.arraycopy(hashedPasswordWithSalt, 12, digestInFile, 0,
        hashedPasswordWithSalt.length - 12);

    System.out.println(Arrays.equals(digest, digestInFile));
  }
}








36.26.MD5 Message Digest algorithm
36.26.1.creates an MD5 message digest from a file and displays it to the screen BASE64 Encoded.
36.26.2.Password Storage Example
36.26.3.Password Authenticator
36.26.4.Check password salt based on MD5
36.26.5.MD5 your password
36.26.6.Set password salt
36.26.7.Check password based on MD5
36.26.8.MD5 MessageDigest your message
36.26.9.Creating a Keyed Digest Using MD5
36.26.10.MD5 String
36.26.11.MD5 hashing: Encodes a string
36.26.12.Get MD5 Base64
36.26.13.MD5 BASE64 checksum for the specified input string.
36.26.14.Encode an MD5 digest into a String.
36.26.15.Hash 32 String
36.26.16.Hash 64 String