Introduction to MD5
MD5 (Message-Digest Algorithm 5) is a widely used hashing algorithm whose output is a 128-bit binary number, usually expressed in the form of a 32-bit hexadecimal number. Therefore, the output of MD5 can be represented as a 32-bit string.
The MD5 algorithm is often used in scenarios such as data integrity verification and password encryption. Usually, after the input data is processed by MD5, a 32-bit digest is obtained. This digest is the only representation of the data and can be used to verify the integrity of the data or to store it as an encrypted password. It should be noted that due to the security problems of the MD5 algorithm itself, it is no longer recommended for security scenarios such as password encryption, but should use more secure algorithms, such as SHA-2, bcrypt, etc.
MD5 (Message Digest Algorithm 5) is a hash function, not a symmetric encryption algorithm. MD5 is used to produce a fixed-size hash called a Message Digest, usually 128 bits. MD5 is not an encryption algorithm because the hash value is irreversible, that is, the original data cannot be restored from the hash value.
Symmetric encryption algorithm is an algorithm that uses the same key for encryption and decryption, such as DES (Data Encryption Standard), AES (Advanced Encryption Standard), etc. Unlike symmetric encryption, the hash function is usually one-way, and the hash value can only be calculated from the original data, but the original data cannot be restored from the hash value.
Security: MD5 has been considered no longer safe because it is vulnerable to collision attacks. For applications requiring security (such as password storage), stronger hashing algorithms (such as SHA-256) and appropriate security practices (such as salting) are recommended.
Performance: While MD5 performs well in performance, modern security requirements often require stronger hashing algorithms.
MD5 (Message Digest Algorithm 5) is a widely used cryptographic hash function, mainly used to generate unique digest values (hash values) of data. The underlying implementation of MD5 involves multiple complex steps and algorithms. Here are the underlying working principles and key concepts of MD5:
- Basic concepts
Hash function: MD5 is a hash function that maps input data (messages) of any length to a hash value of a fixed-length output (128 bits, or 16 bytes).
Irreverability: The hash generated by MD5 cannot theoretically inversely deduce the original input data.
Fixed length: MD5 always generates a 128-bit hash regardless of the length of the input data. - Steps for MD5
The underlying algorithm of MD5 includes the following main steps: - initialization
MD5 uses four 32-bit initial constants (A, B, C, D) as the calculation starting values:
( A = 0x67452301 )
( B = 0xEFCDAB89 )
( C = 0x98BADCFE )
( D = 0x10325476 )
These constants are used to initialize the internal state of MD5. - filling
Fill the input data into multiples of 512 bits. The filling process includes:
Fill bit: Add a 1-bit flag at the end of the input data to indicate the end of the data.
Fill zero: Add zero after the flag bit until the data length is close to 512 bits.
Additional length: Attach the length (in bits) of the original data at the end of the data, expressed in 64 bits. The total length of the filled data is a multiple of 512 bits. - Data chunking
The filled data is divided into 512-bit blocks, each block further divided into 16 32-bit sub-blocks (64 bytes in total). - Process each block of data
For each 512-bit data block, perform the following steps:
Extend: Expand 16 32-bit subblocks into 64 32-bit subblocks, and processed using nonlinear functions.
Rounds: MD5 uses 64 rounds of operation, each round uses a different nonlinear function (F, G, H, I) and constants (T[i]). Each round of operations includes:
Nonlinear functions: F(x, y, z), G(x, y, z), H(x, y, z), I(x, y, z)
Constant: T[i] is a set of predefined constants related to input data and rounds.
Bit operation: Use displacement and bitwise AND, OR, XOR and so on to update the internal state (A, B, C, D).
Data Mixing: Mix the data block with the current internal state. - Output
The processing results (A, B, C, D) of the last data block are concatenated to generate a 128-bit (16-byte) hash value. - Detailed steps of MD5 algorithm
filling:
Add a 1 bit of 1 at the end of the original message.
Add enough 0 so that the message length (including the padded portion) is 448 bits (the first 448 bits of 512 bits), i.e., the length of the 0 to 64 bits of data block must also be filled.
Add a 64-bit message length as the last part of the padding, ensuring that the final message length is a multiple of 512-bit.
Initialize variables:
Initialize A, B, C, D as constant values.
Processing data blocks:
Divide each data block into 16 32-bit subblocks, processed using specific nonlinear functions and constants.
Perform 64 rounds of operations to update the values of A, B, C, and D.
Output result:
Converts the final values of A, B, C, D into a 128-bit hash value, usually expressed in hexadecimal. - MD5 applications
Data Integrity Check: By comparing the MD5 hash of the file, you can detect whether the file has been tampered with.
Password storage: Stores the MD5 hash of the password in the database, not the plaintext password (Note: using MD5 is not recommended to store passwords only, as it has proven to be insecure). - Security
Although MD5 was once widely used, it is no longer considered safe due to its security issues such as collision attacks (i.e. different inputs can produce the same hash value). For applications requiring higher security, stronger hash functions such as SHA-256 or SHA-3 are recommended.
Summarize
MD5 is a hash function that maps input data to a fixed-length hash value. Its underlying implementation includes four main steps: data filling, chunking, processing and final output. Although MD5 was used widely in many applications, it is now recommended to use a safer hashing algorithm due to security issues.
Implementing MD5 encryption in Java
All three methods can effectively generate MD5 hashes. Choose a method that suits your project's needs. If you have already used Apache Commons Codec or Guava, you can directly take advantage of their features; if it is just a simple MD5 encryption requirement, using MessageDigest is the most direct way.
There are many ways to implement MD5 encryption in Java.
Method 1: Use MessageDigest
This is the most basic way to use the MessageDigest class built in Java.
Sample code:
import ; import ; public class MD5UsingMessageDigest { public static String md5(String input) { try { MessageDigest md = ("MD5"); byte[] messageDigest = (()); StringBuilder hexString = new StringBuilder(); for (byte b : messageDigest) { String hex = (0xff & b); if (() == 1) { ('0'); } (hex); } return (); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } } public static void main(String[] args) { String input = "Hello, World!"; ("MD5 Hash: " + md5(input)); } }
Method 2: Use Apache Commons Codec
Apache Commons Codec is a popular library that provides convenient tool classes for encoding and decoding.
Sample code:
First, make sure to add the dependencies of Apache Commons Codec in your project:
<dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> <version>1.15</version> <!-- Check the latest version --> </dependency>
Then you can use the following code:
import ; public class MD5UsingCommonsCodec { public static void main(String[] args) { String input = "Hello, World!"; String md5Hash = DigestUtils.md5Hex(input); ("MD5 Hash: " + md5Hash); } }
Method 3: Use Guava
Google's Guava library also provides simple MD5 encryption.
Sample code:
First, make sure to add Guava dependencies to your project:
<dependency> <groupId></groupId> <artifactId>guava</artifactId> <version>31.0.1-jre</version> <!-- Check the latest version --> </dependency>
Then you can use the following code:
import ; import ; public class MD5UsingGuava { public static void main(String[] args) { String input = "Hello, World!"; String md5Hash = Hashing.md5() .hashString(input, StandardCharsets.UTF_8) .toString(); ("MD5 Hash: " + md5Hash); } }
Method 4: SPRING core package
import ; import ; import ; import ; @RestController public class HashController { @GetMapping("/md5") public String getMD5(@RequestParam String input) { return DigestUtils.md5DigestAsHex(()); } }
MD5Util
package ; import .slf4j.Slf4j; import ; @Slf4j public class MD5Util { private static String byteArrayToHexString(byte[] b) { StringBuilder resultSb = new StringBuilder(); for (byte value : b) { (byteToHexString(value)); } return (); } private static String byteToHexString(byte b) { int n = b; if (n < 0) { n += 256; } int d1 = n / 16; int d2 = n % 16; return HEX_DIGITS[d1] + HEX_DIGITS[d2]; } /** * MD5 encryption with UTF8 encoding * * @param origin Origin string * @return Encrypted string */ public static String md5Encode(String origin) { return md5Encode(origin, "UTF-8"); } public static String md5Encode(String origin, String charsetname) { String resultString = null; try { resultString = origin; MessageDigest md = ("MD5"); if (charsetname == null || "".equals(charsetname)) { resultString = byteArrayToHexString((())); } else { resultString = byteArrayToHexString(((charsetname))); } } catch (Exception ignored) { } return resultString; } private static final String[] HEX_DIGITS = {"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f"}; }
The above is the detailed content of four ways Java implements MD5 encryption. For more information about Java MD5 encryption, please follow my other related articles!