Hash function and message digest algorithm

1. Hash function

  A hash function is a function that can map data of arbitrary length to data of fixed length. The value returned by a hash function is called a hash value, hash code, hash, or simply a hash.

2. Message Summary

    Taking a message of variable length as an input parameter, running a specific Hash function to generate a fixed-length output, this output is Hash, also known as the message digest (Message Digest) of this message

      The message digest algorithm is a type of hash algorithm with the following characteristics:

  • No matter how long the input message is, the length of the calculated message digest is always fixed. The longer the calculated result, the more secure the digest algorithm is. MD5 128-bit SHA-1 160-bit
  • The input message is different, the generated message digest must be different, the input message is the same, the generated message digest must be the same
  • one-way irreversible

3. MessageDigest

  In java, MessageDigest is used to provide the program with the function of message digest algorithm, such as md5 and sha. This is often used, so I won't explain it here.

markup explanation

  1. Get the MessageDigest instance by the algorithm name of the parameter, such as: MD2 MD5 SHA-1 SHA-256 SHA-384 SHA-512
  2. The provider of the specified algorithm digest, which can be obtained by the Security.getProviders() method
  3. Update the digest with the specified byte array
  4. After the hash calculation is completed, it is called only once. After the call  digest()方法, the MessageDigest object is reset to its initial state
  5. reset summary

4. Use

  Since the commons-codec package has already encapsulated some of the used methods, introduce dependencies and call them directly

<dependency>
          <groupId>commons-codec</groupId>
          <artifactId>commons-codec</artifactId>
          <version>1.4</version>
      </dependency>
package com.geenk.web.util;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.codec.digest.DigestUtils;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.security.MessageDigest;

/**
 * @author DUCHONG
 * @since 2018-05-02 9:18
 **/
public class EncryptUtils {

    public static String base64Encode(String data){

        return Base64.encodeBase64String(data.getBytes());

    }


    public static byte[] base64Decode(String data){

        return Base64.decodeBase64(data.getBytes());

    }


    public static String md5(String data) {

        return DigestUtils.md5Hex(data);

    }

    public static String sha1(String data) {

        return DigestUtils.shaHex(data);

    }

    public static String sha256Hex(String data) {

        return DigestUtils.sha256Hex(data);

    }

    / / Calculate the hash value of the file, you can compare whether the file has been changed
    public static String getMD5File(File file){

        try {
            return DigestUtils.md5Hex(new FileInputStream(file));
        }
        catch (IOException e) {
            e.printStackTrace ();
        }
        return null;
    }

}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325257127&siteId=291194637