Summary of Java Security messages

Source: Internet
Author: User

Message Digest also known as data fingerprint, mainly used for data integrity check, such as when you download a jar package in Apache, in addition to the download list will provide a download link, but also provide the MD5 summary value, when you download the downloaded file to calculate the digest value, if the digest values consistent, Indicates that no data loss or change occurred during the download process to ensure that the data is complete.

The algorithm of message digest mainly includes MD (message Digest), SHA (Secure Hash algorithm) and MAC (message authentication Code) A total of 3 series, is also the core algorithm of data signature. MD Series contains md2,md4,md5 a total of three kinds of algorithms, of which MD5 most commonly used is the most perfect; the SHA algorithm mainly includes SHA-1, SHA-224, SHA-256, SHA-384, SHA-512 and so on, of which SHA-1 is most commonly used; Mac algorithm synthesizes the above two algorithms , including HmacMD5, HmacSHA1, HmacSHA256, HmacSHA384, HmacSHA512, Mac algorithm synthesis MD5 and SHA algorithm advantages, and joined the key support, is a more secure message digest algorithm.

In the application, the primary is to calculate the message digest and Checksum message digest for the data, and see how it is used in Java, Its entry class is also known as the engine class: Java.security.MessageDigest But when using the MAC algorithm the engine class is: Javax.crypto.Mac.


First, MD and SHA series


public class Digesttest {@Testpublic void Test () throws Exception {String MyInfo = "My test information";//parameter is a message digest algorithm, the algorithm supported by default in Java is: MD 2, MD5, SHA-1, SHA-256, SHA-384, Sha-512messagedigest digest = messagedigest.getinstance ("SHA-1");//update the data for the summary to be computed, The method can be called multiple times, for example a file is large, need to be updated digest.update (Myinfo.getbytes ()),//Get summary results byte[] result = Digest.digest ();//abstract is actually a piece of binary data , but in order to make it easier for people to view it, it is generally represented by a 16-character string System.out.println ("This Information Digest is:" + tohexstring (result)); MessageDigest MD = messagedigest.getinstance ("SHA-1"); Md.update (Myinfo.getbytes ()); The//isequal method is used to determine if the two digest values are consistent if ( Messagedigest.isequal (result, md.digest ())) {System.out.println ("Digest check OK"),} else {System.out.println ("illegal Digest");}} Binary data to 16 binary string public static string tohexstring (byte[] data) {StringBuilder builder = new StringBuilder (); String stmp = ""; for (int n = 0; n < data.length; n++) {stmp = (java.lang.Integer.toHexString (data[n] & 0XFF)), if (stmp.length () = = 1) {builder.append ("0");} Builder.append (STMP);} Return builder.tostring (). toUpperCase ();} @Testpublic void TestdigestInputStream () throws Exception {MessageDigest digest = messagedigest.getinstance ("MD5"); String value = "XTAYFJPK"; byte[] bytes = Value.getbytes ();//You can also use Digestinputstream to automatically calculate message digests, As long as the data is read from the Digestinputstream stream,//is updated to messagedigest, equivalent to an indirect call to the MessageDigest Update method Digestinputstream stream = new Digestinputstream (New Bytearrayinputstream (bytes), digest);//Must be turned on, default is on Stream.on (true); Stream.read (bytes); byte[ ] result = Stream.getmessagedigest (). Digest (); System.out.println (tohexstring (Result)); Stream.Close ();} @Testpublic void Testdigestoutstream () throws Exception {MessageDigest digest = messagedigest.getinstance ("MD5"); String value = "XTAYFJPK"; byte[] bytes = Value.getbytes (); Bytearrayoutputstream BAOs = new Bytearrayoutputstream ();//similar to Digestinputstream, Only data that is streamed from it when using Digestoutputstream is updated to messagedigest digestoutputstream stream = new Digestoutputstream (BAOs, Digest) ;//Must be turned on, default is on Stream.on (true); Stream.Write (bytes); byte[] result = Stream.getmessagedigest (). Digest (); System.out.println (SecurityUtils.tohexstring (Result)); Stream.Close ();}} 

Second, Mac series

@Testpublic void Testmac () throws Exception {String MyInfo = "My Test Information"; Mac Digest = mac.getinstance ("HmacSHA512");//Get key generator, AES for key algorithm Keygenerator keygenerator = Keygenerator.getinstance (" AES ");//Initialize the Digest.init (Keygenerator.generatekey ()) with the generated key,//update the summary data to be computed Digest.update (Myinfo.getbytes ());// Get summary result byte[] result = digest.dofinal (); SYSTEM.OUT.PRINTLN ("This Information Digest is:" + tohexstring (Result));}


Summary of Java Security messages

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.