常见单向加密算法的调用【懒人大法】

一、JAVA后端加密

把DigestUtils 作为自用工具类,把注释打开就可以简单测试,可运行

(org.apache.commons.codec.digest.DigestUtils)

省去了翻源码的时间,PS:这些算法只有加密,无解密,所以,别费劲了

package com.chinaunicom.rights.util;

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

public class DigestUtilEZ {
	private enum ALGORITHM_NAME {
		MD2, MD5, SHA_1, SHA_256, SHA_384, SHA_512
	}
	/**
     * 编码字符串
     *
     * @param sourceStr    需要编码的字符串String
     * @param algorithmsName 算法名称(如:MD2,MD5,SHA1,SHA256,SHA384,SHA512)
     * @return
     */
	public static String digestString(String sourceStr, ALGORITHM_NAME algorithmsName) {
		String password = null;
		switch (algorithmsName) {
		case MD2:
			password = DigestUtils.md2Hex(sourceStr);
			break;
		case MD5:
			password = DigestUtils.md5Hex(sourceStr);
			break;
		case SHA_1:
			password = DigestUtils.sha1Hex(sourceStr);
			break;
		case SHA_256:
			password = DigestUtils.sha256Hex(sourceStr);
			break;
		case SHA_384:
			password = DigestUtils.sha384Hex(sourceStr);
			break;
		case SHA_512:
			password = DigestUtils.sha512Hex(sourceStr);
			break;
		}
		return password;
	}
//	public static void main(String[] args) {
//		
//		String pswd = DigestUtilEZ.digestString("186****0888", ALGORITHM_NAME.MD5);
//		System.out.println(pswd);
//		pswd = DigestUtilEZ.digestString("186****0888", ALGORITHM_NAME.MD2);
//		System.out.println(pswd);
//		pswd = DigestUtilEZ.digestString("186****0888", ALGORITHM_NAME.SHA_256);
//		System.out.println(pswd);
//		pswd = DigestUtilEZ.digestString("186****0888", ALGORITHM_NAME.SHA_512);
//		System.out.println(pswd);
//	}

}

二、Javascript前端加密【MD5】

1.导入js文件

 

https://cdn.bootcss.com/blueimp-md5/2.10.0/js/md5.js

copy到自己本地也行


2.使用

md5(passwd)

你可能感兴趣的:(Java笔记,java)