Ah, a little bit of comment, too lazy to introduce, put a code bar, to understand the effect can be.
1 PackageJdbc.pro.lin;2 3 Importjava.security.InvalidKeyException;4 Importjava.security.MessageDigest;5 Importjava.security.NoSuchAlgorithmException;6 7 ImportJavax.crypto.KeyGenerator;8 ImportJavax.crypto.Mac;9 ImportJavax.crypto.SecretKey;Ten ImportJavax.crypto.spec.SecretKeySpec; One A Importorg.apache.commons.codec.binary.Base64; - - Public classMymessagedigest { the Public Static FinalString Plain_text = "I m a sample"; - Public Static FinalString md_algorithm = "MD5"; - Public Static FinalString sha_algorithm = "SHA-512"; - Public Static FinalString mac_algorithm = "HmacSHA512"; + - Public Static voidMain (string[] args) { +System.out.println ("MD5:" +MD5 (Plain_text.getbytes ())); ASystem.out.println ("SHA-512:" +SHA (Plain_text.getbytes ())); atSystem.out.println ("HmacSHA512:" +MAC (Plain_text.getbytes ())); - } - - /** - * 1. Message digest algorithm, MD family, with MD2 MD4 MD5, where MD4 JDK does not support - * in * @paramplaintext - * @return to */ + Public StaticString MD5 (byte[] plaintext) { - messagedigest messagedigest; the Try { *MessageDigest =messagedigest.getinstance (md_algorithm); $ returnbase64.encodebase64string (Messagedigest.digest (plaintext));Panax Notoginseng}Catch(nosuchalgorithmexception e) { - //TODO auto-generated Catch block the e.printstacktrace (); + } A return NULL; the + } - $ /** $ * 2.SHA security hash Algorithm secure Hash algorithm, fixed length summary information SHA-1 SHA-2 (SHA-224 - * SHA-256 SHA-384 SHA-512) is still used MessageDigest class, JDK does not support 224 - * the * @paramplaintext - * @returnWuyi */ the Public StaticString SHA (byte[] plaintext) { - messagedigest messagedigest; Wu Try { -MessageDigest =messagedigest.getinstance (sha_algorithm); About returnbase64.encodebase64string (Messagedigest.digest (plaintext)); $}Catch(Exception e) { - e.printstacktrace (); - } - return NULL; A } + the /** - * 3.MAC (Message authentication code) message authentication code algorithm, is contains the key hash function algorithm. $ * Features of MD and SHA are compatible. the * The encryption process is three steps away, similar to the symmetric and asymmetric cryptography that are described later the * 1) incoming algorithm, instantiation of an encryption device the * 2) incoming key, initialize the encryption device the * 3) Call the Dofinal method for encryption - * @paramplaintext in * @return the */ the Public StaticString MAC (byte[] plaintext) { About the Try { the byte[] Secretbytes =Generatormacsecretkey (); theSecretkey key =Restoremacsecretkey (secretbytes); +Mac Mac =mac.getinstance (mac_algorithm); - Mac.init (key); the returnbase64.encodebase64string (mac.dofinal (plaintext));Bayi}Catch(NoSuchAlgorithmException |invalidkeyexception e) { the //TODO auto-generated Catch block the e.printstacktrace (); - } - return NULL; the the } the the /** - * Mac generates a random key two-step walk 1. Create a Keygenerator 2. Call the Keygenerator.generatekey method the * the * @return the */94 Public Static byte[] Generatormacsecretkey () { the keygenerator Keygenerator; the Try { theKeygenerator =keygenerator.getinstance (mac_algorithm);98Secretkey key =Keygenerator.generatekey (); About returnkey.getencoded (); -}Catch(nosuchalgorithmexception e) {101 //TODO auto-generated Catch block102 e.printstacktrace ();103 }104 return NULL; the }106 107 /**108 * Restore Key109 * the * @paramsecretbytes111 * @return the */113 Public StaticSecretkey Restoremacsecretkey (byte[] secretbytes) { theSecretkey key =NewSecretkeyspec (secretbytes, mac_algorithm); the returnkey; the }117}
JDK self-bringing method for message digest operation