MessageDigest class
The MessageDigest class provides applications with the functionality of information digest algorithms, such as MD5 or SHA algorithms. The information digest is a safe one-way hash function that receives data of any size and outputs a fixed-length hash value.
The MessageDigest object begins to be initialized. This object processes data by using the update() method. You can call the reset() method at any time to reset the summary. Once all data that needs to be updated has been updated, one of the digest() methods should be called to complete the hash calculation.
The digest method can only be called once for a given amount of update data. After the digest is called, the MessageDigest object is reset to its initial state.
illustrate:
In the website, in order to protect the privacy information such as username and password of the website member, we directly encrypt it in MD5 or other ways when registering. Even the database administrator cannot view the member's password and other information. The password effect in the database is such as: 8e830882f03b2cb84d1a657f346dd41a effect.
Because the MD5 algorithm is irreversible, it is widely used by many websites.
Three commonly used encryption methods
Method 1: Use bit operators to convert encrypted data into hexadecimal Method 2: Use formatting method to convert encrypted data into hexadecimal (recommended)
Method 3: Use algorithm to convert the encrypted data into hexadecimal
import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;/** * Use the MessageDigest class that comes with Java* @author xiaokui */public class EncryptionUtil {/** * Since MD5 and SHA-1 both developed from MD4, their structure and strength characteristics have many similarities* The biggest difference between SHA-1 and MD5 is that its digest is 32 bits longer than the MD5 digest (1byte=8bit, equivalent to 4bytes long, 8 characters more than MD5 after conversion to hexadecimal). * For forced attacks, MD5 is an operation of the order of 2128 and SHA-1 is an operation of the order of 2160. * Difficulty for two messages with the same digest: MD5 is an operation of 264 is an operation of order of magnitude, and SHA-1 is an operation of order of 280 is an operation of order of magnitude. * Therefore, SHA-1 is more powerful against forced attacks. However, because SHA-1 has more cycle steps than MD5 (80:64) and the cache to be processed is larger (160 bits:128 bits), SHA-1 runs slowly than MD5. * * @param source string that needs to be encrypted* @param hashType Encryption type (MD5 and SHA) * @return */public static String getHash(String source, String hashType) {// Characters used to convert bytes into hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};try {MessageDigest md = MessageDigest.getInstance(hashType);md.update(source.getBytes());// By processing data using the update method, the specified byte array is updated to update the digest byte[] encryptStr = md.digest();// Obtain the ciphertext to complete the hash calculation, and produce a 128-bit long integer char str[] = new char[16 * 2];// If each byte is represented in hexadecimal, use two characters int k = 0;// Denote the corresponding character position in the conversion result for (int i = 0; i < 16; i++) {// Starting from the first byte, convert each byte into hexadecimal characters byte byte0 = encryptStr[i];// Take the i-th byte str[k++] = hexDigits[byte0 >>> 4 & 0xf];// Take the numerical conversion of the upper 4 bits in byte, >>> For logical shift right, shift the symbol bits right together str[k++] = hexDigits[byte0 & 0xf];// Take the numerical conversion of the lower 4 bits in byte}return new String(str);// Convert the result after conversion to a string}catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}/** @param source string that needs to be encrypted* @param hashType Encryption type (MD5 and SHA) * @return */public static String getHash2(String source, String hashType) {StringBuilder sb = new StringBuilder();MessageDigest md5;try {md5 = MessageDigest.getInstance(hashType);md5.update(source.getBytes());for (byte b : md5.digest()) {sb.append(String.format("%02X", b));// Decimal to hexadecimal, X means output in hexadecimal, 02 Indicates that there are less than two digits before the output is 0}return sb.toString();}catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}/** @param source string that needs to be encrypted* @param hashType Encryption type (MD5 and SHA) * @return */public static String getHash3(String source, String hashType) {// Characters used to convert bytes into hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};StringBuilder sb = new StringBuilder();MessageDigest md5;try {md5 = MessageDigest.getInstance(hashType);md5.update(source.getBytes());byte[] encryptStr = md5.digest();for (int i = 0; i < encryptStr.length; i++) {int iRet = encryptStr[i];if (iRet < 0) {iRet += 256;}int iD1 = iRet / 16;int iD2 = iRet % 16;sb.append(hexDigits[iD1] + "" + hexDigits[iD2]);}return sb.toString();}catch (NoSuchAlgorithmException e) {e.printStackTrace();}return null;}public static void main(String[] args) {System.out.println(getHash("Xiaokui", "MD5"));System.out.println(getHash("Xiaokui", "SHA") + "/n");System.out.println(getHash2("Xiaokui", "MD5"));System.out.println(getHash2("Xiaokui", "SHA") + "/n");System.out.println(getHash3("Xiaokui", "MD5"));System.out.println(getHash3("Xiaokui", "SHA") + "/n");}}Output result
8e830882f03b2cb84d1a657f346dd41a 0ba5512371d00c86e91712f44aab7138 8E830882F03B2CB84D1A657F346DD41A 0BA5512371D00C86E91712F44AAB713898745F91 8e830882f03b2cb84d1a657f346dd41a 0ba5512371d00c86e91712f44aab713898745f91
We found that the three methods performed the same effect, and the SHA length is 8 characters (32 bits) more than MD5.
Summarize
The above is the entire content of this article about the code example of the encryption class MessageDigest that comes with Java. I hope it will be helpful to everyone. If there are any shortcomings, please leave a message to point it out.