MessageDigestUtil.java
3.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.fh.util.express.util;
import org.apache.commons.codec.binary.Base64;
import com.fh.util.express.constant.Constants;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
* 消息摘要工具
*/
public class MessageDigestUtil {
/**
* 先进行MD5摘要再进行Base64编码获取摘要字符串
*
* @param str
* @return
*/
public static String base64AndMD5(String str) {
if (str == null) {
throw new IllegalArgumentException("inStr can not be null");
}
return base64AndMD5(toBytes(str));
}
/**
* 先进行MD5摘要再进行Base64编码获取摘要字符串
*
* @return
*/
public static String base64AndMD5(byte[] bytes) {
if (bytes == null) {
throw new IllegalArgumentException("bytes can not be null");
}
try {
final MessageDigest md = MessageDigest.getInstance("MD5");
md.reset();
md.update(bytes);
final Base64 base64 = new Base64();
final byte[] enbytes = base64.encode(md.digest());
return new String(enbytes);
} catch (final NoSuchAlgorithmException e) {
throw new IllegalArgumentException("unknown algorithm MD5");
}
}
/**
* UTF-8编码转换为ISO-9959-1
*
* @param str
* @return
*/
public static String utf8ToIso88591(String str) {
if (str == null) {
return str;
}
try {
return new String(str.getBytes("UTF-8"), "ISO-8859-1");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* ISO-9959-1编码转换为UTF-8
*
* @param str
* @return
*/
public static String iso88591ToUtf8(String str) {
if (str == null) {
return str;
}
try {
return new String(str.getBytes("ISO-8859-1"), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
/**
* String转换为字节数组
*
* @param str
* @return
*/
private static byte[] toBytes(final String str) {
if (str == null) {
return null;
}
try {
return str.getBytes(Constants.ENCODING);
} catch (final UnsupportedEncodingException e) {
throw new RuntimeException(e.getMessage(), e);
}
}
}