java摘要_java 摘要

packagecom.aarony.test;importjava.io.IOException;importjava.security.MessageDigest;importsun.misc.BASE64Decoder;importsun.misc.BASE64Encoder;public classDigestDemo {/***

* 此方法描述的是:base64 解码

*

*@author: Aarony

*@version: 2018年6月20日 下午9:16:57*/

public static byte[] base642byte(String base64) throwsIOException {

BASE64Decoder decoder= newBASE64Decoder();returndecoder.decodeBuffer(base64);

}/***

* 此方法描述的是: base 64编码

*

*@author: Aarony

*@version: 2018年6月20日 下午9:15:14*/

public static String byte2base64(byte[] bytes) {

BASE64Encoder base= newBASE64Encoder();returnbase.encode(bytes);

}/***

* 此方法描述的是:16位数转换成byte

*

*@author: Aarony

*@version: 2018年6月20日 下午9:17:43*/

public static byte[] hex2bytes(String hex) {byte[] bytes = new byte[hex.length() / 2];for (int i = 0; i < hex.length(); i = i + 2) {

String subStr= hex.substring(i, i + 2);boolean negative = false;int inte = Integer.parseInt(subStr, 16);if (inte > 127) {

negative= true;

}if (inte == 128) {

inte= -128;

}else if(negative) {

inte= 0 - (inte & 0x7f);

}byte b = (byte) inte;

bytes[i/ 2] =b;

}returnbytes;

}/***

* 此方法描述的是:byte 转换成 16位

*

*@author: Aarony

*@version: 2018年6月20日 下午9:17:16*/

public static String bytes2hex(byte[] bytes) {

StringBuilder sBuilder= newStringBuilder();for (int i = 0; i < bytes.length; i++) {byte b =bytes[i];boolean negative = false;if (b < 0) {

negative= true;

}int inte =Math.abs(b);if(negative) {

inte= inte | 0x80;

}

String temp= Integer.toHexString(inte & 0xff);if (temp.length() == 1) {

sBuilder.append("0");

}

sBuilder.append(temp.toLowerCase());

}returnsBuilder.toString();

}/***

* 此方法描述的是:sha

*

*@author: Aarony

*@version: 2018年6月20日 下午9:18:11*/

public static byte[] testSHA(String content) throwsException {

MessageDigest messageDigest= MessageDigest.getInstance("SHA-1");return messageDigest.digest(content.getBytes("utf-8"));

}/***

* 此方法描述的是:md5

*

*@author: Aarony

*@version: 2018年6月20日 下午9:18:20*/

public static byte[] testMD5(String content) throwsException {

MessageDigest messageDigest= MessageDigest.getInstance("MD5");byte[] bytes = messageDigest.digest(content.getBytes("utf-8"));returnbytes;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值