MD5加密封装

Java笔记 专栏收录该内容
29 篇文章 0 订阅
/*
* @(#)DigestUtil.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-12-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.commons.security;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
* @author Geek_Soledad (66704238@51uc.com)
*/
public class DigestUtil {
/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法
* @param message
* 进行加密的String对象
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, String message) {
return doDigest(algorithm, message.getBytes());
}

/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法
* @param message
* 进行加密的byte数组
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, byte[] message) {
MessageDigest md = null;
try {
md = MessageDigest.getInstance(algorithm);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
}

byte[] result = md.digest(message);
return StringUtil.bytesToHexString(result);
}

/**
* 获取一个字符串加密后的16进制值
*
* @param algorithm
* 摘要算法名称
* @param message
* 进行加密的byte数组
*
* @return String 计算后的结果
*/
public static String doDigest(String algorithm, char[] message) {
return doDigest(algorithm, new String(message));
}
}


/*
* @(#)StringUtil.java Project:com.sinaapp.msdxblog.androidkit
* Date:2012-12-18
*
* Copyright (c) 2011 CFuture09, Institute of Software,
* Guangdong Ocean University, Zhanjiang, GuangDong, China.
* All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.lurencun.cfuture09.commons.security;

/**
* @author Geek_Soledad (66704238@51uc.com)
*/
public class StringUtil {

/**
* byte[]数组转换为16进制的字符串。
*
* @param data
* 要转换的字节数组。
* @return 转换后的结果。
*/
public static final String bytesToHexString(byte[] data) {
StringBuilder valueHex = new StringBuilder();
for (int i = 0, tmp; i < data.length; i++) {
tmp = data[i] & 0xff;
if (tmp < 16) {
valueHex.append(0);
}
valueHex.append(Integer.toHexString(tmp));
}
return valueHex.toString();
}

/**
* 16进制表示的字符串转换为字节数组。
*
* @param hexString
* 16进制表示的字符串
* @return byte[] 字节数组
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
char[] hexChars = hexString.toCharArray();
int length = hexString.length();
byte[] d = new byte[length >>> 1];
for (int n = 0; n < length; n += 2) {
String item = new String(hexChars, n, 2);
// 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个进制字节
d[n >>> 1] = (byte) Integer.parseInt(item, 16);
}
return d;
}
}
  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值