Java實現驗證碼生成工具類-CheckSumBuilder

Java實現驗證碼生成工具類-CheckSumBuilder

背景

一版在Web系統登錄、表單提交及資源下載等關鍵功能爲了防止黑客遍歷破解都會用到驗證碼功能,下面就來了解一下Java如何簡單實現驗證碼的生成。

代碼實現

CheckSumBuilder.java

package com.utils;

import java.security.MessageDigest;

/**
 * 功能說明:驗證碼生成工具類
 * 修改說明:
 * @author zheng
 * @date 2020年6月29日 下午1:33:26
 * @version 0.1
 */
public class CheckSumBuilder {
    /**
     * 功能說明:計算並獲取CheckSum
     * 修改說明:
     * @author zheng
     * @date 2020年6月29日 下午1:33:49
     * @param appSecret 密碼
     * @param nonce 隨機串
     * @param curTime 當前時間戳
     * @return 返回生成的驗證碼
     */
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }

    /**
     * 功能說明:對參數進行MD5加密
     * 修改說明:
     * @author zheng
     * @date 2020年6月29日 下午1:34:49
     * @param requestBody 要加密的內容
     * @return 返回MD5加密後的字符串
     */
    public static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }

    /**
     * 功能說明:使用指定加密算法對字符串進行加密
     * 修改說明:
     * @author zheng
     * @date 2020年6月29日 下午1:35:45
     * @param algorithm 加密算法
     * @param value 要加密的字符串
     * @return 返回加密後的字符串
     */
    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 功能說明:把字節數組格式化爲16進制字符串
     * 修改說明:
     * @author zheng
     * @date 2020年6月29日 下午1:37:10
     * @param bytes 字節數組
     * @return 返回格式化後的字符串
     */
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }

    private static final char[] HEX_DIGITS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
}

單元測試

CheckSumBuilderTest.java

package com.utils;

import java.util.Date;
import java.util.Random;

import org.junit.Test;

/**
 * 功能說明:CheckSumBuilder單元測試類
 * 修改說明:
 * @author zheng
 * @date 2020年6月29日 下午1:54:27
 * @version 0.1
 */
public class CheckSumBuilderTest {
	
	/**
	 * 功能說明:驗證碼生成單元測試方法
	 * 修改說明:
	 * @author zheng
	 * @date 2020年6月29日 下午1:49:32
	 */
	@Test
	public void testGetCheckSum() {
		Random r = new Random();
		Date now = new Date();
		String appSecret = "zheng123";
		String nonce = "" + r.nextLong();
		String curTime = "" + now.getTime();
		String checkSum = CheckSumBuilder.getCheckSum(appSecret, nonce, curTime);
		System.out.println("checkSum = [" + checkSum.substring(0, 5) + "]");
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章