This is a technique used in conjunction with message authentication codes to ensure message integrity. The main use of one-way hash function algorithm, can be used to verify the integrity of the message, and by hashing the password directly in the form of text preservation, etc., the current widely used algorithms have MD4, MD5, sha-1,jdk1.5 to support the above, the message digest in Java is very simple, Java.security.MessageDigest provides an easy way to do this:
/** *messagedigestexample.java *copyright 2005-2-16 */import Java.sec Urity. MessageDigest; /** * A single message digest algorithm that does not use a password. Can be used to hide a clear text message (such as: password) Save */public class messagedigestexample{public static void Main (string[] args) thr oWS exception{if (args.length!=1) {System.err.println ("Usage:java messagedigestexample text"); System.exit (1); } byte[] Plaintext=args[0].getbytes ("UTF8"); //use getinstance ("algorithm") to get a message digest, using SHA-1 's 160-bit algorithm messagedigest messagedigest= Messagedigest.getinstance ("SHA-1"); System.out.println ("\ n" Messagedigest.getprovider (). GetInfo ()); Start using algorithm messagedigest.update (plaintext); System.out.println ("\ndigest:"); Output algorithm operation result System.out.println (new String (Messagedigest.digest (), "UTF8")); } } |
You can also use the message authentication code for the encryption implementation, JAVAX.CRYPTO.MAC provides a solution, interested people can refer to the relevant API documentation, this article is simply to explain what is a digest algorithm.
Message Digest Java.security.MessageDigest