★ Programming ideas :
The MessageDigest class in the java.security package provides a method for calculating message digests. First, generate an object, execute its update() method to pass the original data to the object, and then execute its digest() method to get the message digest. The specific steps are as follows:
(1) Generate MessageDigest object
MessageDigest m=MessageDigest.getInstance("MD5");
Analysis: Same as the KeyGenerator class in Section 2.2.1. The MessageDigest class is also a factory class, and its constructor is protected. It is not allowed to use new MessageDigist() directly to create objects, but must generate MessageDigest objects through its static method getInstance().
The parameters passed in specify the algorithm used to calculate the message digest. Commonly used ones include "MD5", "SHA", etc. If you are interested in the details of the MD5 algorithm, please refer to
http://www.gztarena.com/rfc1321.txt.
(2) Pass in the string that needs to be calculated
m.update(x.getBytes("UTF8" ));
Analysis: x is the string that needs to be calculated. The parameters passed in by update are byte type or byte type array. For strings, you need to use
The getBytes() method generates a string array.
(3) Calculate message digest
byte s[ ]=m.digest( );
Analysis: Execute the digest (Guangzhou Danai java) method of the MessageDigest object to complete the calculation, and the calculation result is returned through a byte type array.
(4) Processing calculation results <BR>If necessary, you can use the following code to convert the calculation result s into a string.