Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java's MD5 utility class and client test class

Shulou Source: shulou.com Published: 2022-06-02 06:24:57 10月01日 Update

This article mainly explains the "Java MD5 tool class and client test class how to use", the article explains the content is simple and clear, easy to learn and understand, the following please follow the editor's ideas slowly in depth, together to study and learn "Java MD5 tool class and client test class how to use" it!

What is MD5?

Message Digest Algorithm MD5 (Chinese name message digest algorithm version 5) is a hash function widely used in the field of computer security, which is used to protect the integrity of messages. The file number of the algorithm is RFC 1321 (R.RivestreMIT Laboratory for Computer Science and RSA Data Security Inc. April 1992).

MD5, or Message-Digest Algorithm 5 (Information-Summary algorithm 5), is used to ensure that the transmission of information is complete and consistent. It is one of the hash algorithms (abstract algorithm and hash algorithm) widely used in computers. MD5 has been implemented in mainstream programming languages. Computing data (such as Chinese characters) into another fixed length value is the basic principle of hash algorithm. The predecessor of MD5 is MD2, MD3 and MD4.

MD5 algorithm has the following characteristics:

1. Compressibility: for data of any length, the length of the calculated MD5 value is fixed.

2. Easy to calculate: it is easy to calculate the MD5 value from the original data.

3. Anti-modification: if you make any changes to the original data, even if you only modify 1 byte, the MD5 values obtained are very different.

4. Strong anti-collision: knowing the original data and its MD5 value, it is very difficult to find a data with the same MD5 value (that is, forged data).

A utility class package com.huaidan.utils;import java.io.UnsupportedEncodingException;import java.security.MessageDigest;import java.security.NoSuchAlgorithmException;import java.security.SecureRandom;import java.util.Arrays;public class MyMD5Util {private static final String HEX_NUMS_STR= "0123456789ABCDEF"; private static final Integer SALT_LENGTH = 12 / * * convert hexadecimal strings to byte arrays * @ param hex * @ return * / public static byte [] hexStringToByte (String hex) {int len = (hex.length () / 2); byte [] result = new byte [len]; char [] hexChars = hex.toCharArray (); for (int I = 0; I < len) Byte +) {int pos = I * 2; result [I] = (byte) (HEX_NUMS_STR.indexOf (hexchars [pos])

Tags: Passwords messages summaries data bytes arrays algorithms objects encryption clients clients tools tests variables binary characters strings databases users length Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia OPPO Reno Xiaomi Apple Shulou Technology