zoukankan      html  css  js  c++  java
  • Hash函数和消息摘要算法

    一、Hash函数

      哈希函数就是能将任意长度的数据映射为固定长度的数据的函数。哈希函数返回的值被叫做哈希值、哈希码、散列,或者直接叫做哈希。

    二、消息摘要

        将长度不固定的消息(message)作为输入参数,运行特定的Hash函数,生成固定长度的输出,这个输出就是Hash,也称为这个消息的消息摘要(Message Digest)

          信息摘要算法是hash算法的一种,具有以下特点:

    • 无论输入的消息有多长,计算出来的消息摘要的长度总是固定的,计算出的结果越长,一般认为该摘要算法越安全,MD5 128位 SHA-1 160位
    • 输入的消息不同,产生的消息摘要必不同,输入的消息相同,产生的消息摘要一定是相同的
    • 单向不可逆

    三、MessageDigest

      java中通过MessageDigest来为程序提供消息摘要算法的功能,例如md5 和sha,这个经常会使用的到,这里就不多解释了

    标记解释

    1. 通过入参的算法名获取MessageDigest实例,入参例如:MD2  MD5  SHA-1  SHA-256  SHA-384  SHA-512
    2. 指定的算法摘要的提供者,可通过Security.getProviders() 方法获取
    3. 使用指定的字节数组更新摘要
    4. 完成hash计算,只调用一次,在调用 digest()方法之后,MessageDigest 对象被重新设置成其初始状态
    5. 重置摘要

    四、使用

      由于commons-codec包中已经封装好了一些使用的方法,引入依赖,直接调用即可

    4.1、依赖

          <dependency>
              <groupId>commons-codec</groupId>
              <artifactId>commons-codec</artifactId>
              <version>1.4</version>
          </dependency>
    

    4.2、工具类

    package com.treebear.starwifi.common.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;
    
    /**
     * @author DUCHONG
     * @since 2018-05-02 9:18
     **/
    public class EncryptionUtils {
    
        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);
    
        }
    
        public static String getMD5File(File file){
            FileInputStream fis=null;
            try {
                fis=new FileInputStream(file);
                return DigestUtils.md5Hex(fis);
            }
            catch (IOException e) {
                e.printStackTrace();
            }
            finally {
                if(fis != null){
                    try {
                        fis.close();
                    }
                    catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return null;
        }
    
    
        public static void main(String[] args) {
    
    
            //long start= System.currentTimeMillis();
    
            System.out.println(getMD5File(new File("F:\temp\WEB-INF.zip")));
            System.out.println(getMD5File(new File("F:\temp2\WEB-INF.zip")));
    
    
            //long end=System.currentTimeMillis();
    
            //System.out.println("共耗时:"+(float)(end-start)/1000+"s");
        }
    }
    

      

  • 相关阅读:
    Yii2 在模块modules间跳转时,url自动加模块名
    PHP 变量的间接引用(将某一字符串转化为变量)
    windows鼠标悬停任务栏 延迟时间 修改
    dede 常用标签和调用方法汇总
    dedecms ---m站功能基础详解
    apache 2.2 和2.4 目录权限访问设置的区别
    apache httpd.conf 配置局域网访问
    ajax php 点击加载更多
    dede调用当前栏目名 、dede sql
    dede 添加 栏目缩略图
  • 原文地址:https://www.cnblogs.com/geekdc/p/8979736.html
Copyright © 2011-2022 走看看