Chapter 4 message digest algorithm SHA and digest sha
Note: In this section, refer to "verify data integrity-message digest algorithm" in Chapter 2nd of Java encryption and decryption art (version 6th"
4.1. SHA
Principle: the longer the message digest length (which can be quantified as the length of the encrypted string), the higher the Security
- MD5: 128-bit binary Digest (32-bit hexadecimal string) (cracked)
- SHA1: 160-bit binary Digest (40-bit hexadecimal string) (cracked)
- SHA256: 256-bit binary Digest (64-bit hexadecimal string )(Common, Also used in spring security)
Implementation Method:
- Commons Codec("CC ",Most recommendedBecause it encapsulates the underlying code of JDK and provides a way to convert binary byte arrays to hexadecimal notation)
- JDK (there is no way to convert a binary byte array to a hexadecimal format. to convert a binary byte array, you need to use BC)
- Bouncy Castle ("BC", not recommended, complicated implementation)
Only CC-based tool code is listed below. For JDK-based code, refer to "Java encryption and decryption art (version 2nd)". for BC-based code, refer to "MOOC"
4.1.1 CC-based SHA Encryption Algorithm
1 package com. util. sha; 2 3 import java. io. unsupportedEncodingException; 4 import java. security. noSuchAlgorithmException; 5 import org. apache. commons. codec. digest. digestUtils; 6/** 7 * Based on Commons Codec SHA algorithm 8 */9 public class SHACC {10 private static final String ENCODING = "UTF-8 "; 11 12/** 13 * SHA encryption, the encrypted result is a binary byte array 14 */15 public static byte [] encode (String data) throws NoSuchAlgorithmException, U NsupportedEncodingException {16 // return DigestUtils. sha1 (data. getBytes (ENCODING); // The return DigestUtils SHA-117. sha256 (data. getBytes (ENCODING); // SHA-25618} 19 20/** 21 * SHA encrypted, the encrypted result is a binary byte array, here, we convert the binary byte array to a hexadecimal String 22 */23 public static String encodeSHAHex (String data) throws NoSuchAlgorithmException, UnsupportedEncodingException {24 // return new String (DigestUtils. sha1Hex (data. getBytes (ENCODING ))); 25 return new String (DigestUtils. sha256Hex (data. getBytes (ENCODING ))); 26} 27 28/** 29 * Test 30 * @ param args31 * @ throws UnsupportedEncodingException 32 * @ throws NoSuchAlgorithmException 33 */34 public static void main (String [] args) throws UnsupportedEncodingException, NoSuchAlgorithmException {35 36 String data = "It is my dream to find a good girl as a wife! "; 37/************** test encode () ****************/38 System. out. println ("original -->" + data); 39 byte [] encodedByte = SHACC. encode (data); 40 System. out. println ("encrypted -->" + encodedByte); 41 byte [] encodedByte2 = SHACC. encode (data); 42 System. out. println ("encrypted -->" + encodedByte2); 43 for (int I = 0; I <encodedByte. length; I ++) {44 System. out. println (encodedByte [I] = encodedByte2 [I]); 45} 46/************* test encodeSHAHex () * ************/47 System. out. println ("original -->" + data); 48 String encodedStr = SHACC. encodeSHAHex (data); 49 System. out. println ("encrypted -->" + encodedStr); 50 String encodedStr2 = SHACC. encodeSHAHex (data); 51 System. out. println ("encrypted -->" + encodedStr2); 52 System. out. println (encodedStr. equals (encodedStr2); 53} 54}View Code
When we need to adopt the corresponding sha algorithm, we only need to use different functions (query the Commons Codec API for details, and the link is at the end of chapter 5 ).
The introduction of the jar package can be carried out by referring to the first example in chapter 2. During the test, try to see if "the same message has the same result after SHA encryption for multiple times ";
In the test of the encode () method, determine whether two byte [] are equal: Compare the elements in the two byte arrays in order by index (if the main method is used for testing ); if Junit is used, use assertArrayEquals (array1, array2) directly.