-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathDigitalSign.java
More file actions
178 lines (156 loc) · 5.11 KB
/
DigitalSign.java
File metadata and controls
178 lines (156 loc) · 5.11 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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.zhao.test;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
public class DigitalSign {
public static String getDigitalSign(Object arg, String secretKey)
throws Exception {
String digitalSign = "";
// 获取参与加密的值
Map<String, String> params = new HashMap<String, String>();// 参与加密的各项值存储在params中
objectToMap(arg, params);
String[] keys = new String[params.size()];
int keys_idx = 0;
for (Map.Entry<?, ?> keyEntry : params.entrySet()) {
keys[keys_idx] = (String) keyEntry.getKey();
keys_idx++;
}
// 排序
Bubble_sort(keys);
// 拼装数字签名内容
StringBuffer sb = new StringBuffer();
for (int i = 0; i < keys.length; i++) {
sb.append(keys[i]).append(params.get(keys[i]));
}
SimpleDateFormat sdf_yyyyMMdd = new SimpleDateFormat("yyyyMMdd");
sb.append(sdf_yyyyMMdd.format(new Date())).append(secretKey);
// 加密
// System.out.println("MD5加密报文:"+URLEncoder.encode(sb.toString(),"UTF-8").toLowerCase());
// System.out.println("MD5加密报文:"+sb.toString());
digitalSign = MD5.md5Encode(sb.toString());
// System.out.println("MD5加密后报文:"+digitalSign);
return digitalSign;
}
private static boolean isPrimitives(Class<?> cls) {
if (cls.isArray()) {
return isPrimitive(cls.getComponentType());
}
return isPrimitive(cls);
}
private static boolean isPrimitive(Class<?> cls) {
return cls.isPrimitive() || cls.getClass().equals(String.class)
|| cls.getClass().equals(Integer.class)
|| cls.getClass().equals(Byte.class)
|| cls.getClass().equals(Long.class)
|| cls.getClass().equals(Double.class)
|| cls.getClass().equals(Float.class)
|| cls.getClass().equals(Character.class)
|| cls.getClass().equals(Short.class)
|| cls.getClass().equals(BigDecimal.class)
|| cls.getClass().equals(BigInteger.class)
|| cls.getClass().equals(Boolean.class)
|| Number.class.isAssignableFrom(cls)
|| Date.class.isAssignableFrom(cls);
}
public static void objectToMap(Object arg, Map<String, String> params)
throws Exception {
if (arg != null && !isPrimitives(arg.getClass())) {
if (Map.class.isInstance(arg)) {
for (Map.Entry<?, ?> entry : ((Map<?, ?>) arg).entrySet()) {
try {
params.put((String) entry.getKey(),
(String) entry.getValue());// 无法强转为String类型的参数不参与加密
} catch (Exception e) {
continue;
}
}
} else {
Class<?> cls = arg.getClass();
Field[] fs = cls.getDeclaredFields();
Field[] superfs = cls.getSuperclass().getDeclaredFields();
for (int i = 0; i < superfs.length; i++) {
objectToMap_field(arg,superfs[i],params);
}
for (int i = 0; i < fs.length; i++) {
objectToMap_field(arg,fs[i],params);
}
}
}
}
private static void objectToMap_field(Object arg, Field f,Map<String, String> params) {
try {
f.setAccessible(true); // 设置该属性是可以访问的
Object val = f.get(arg);// 得到此属性的值
if(val==null){
params.put(f.getName(), String.valueOf(val));
} else if (val.getClass().isPrimitive()
|| val.getClass().equals(String.class)
|| val.getClass().equals(Integer.class)
|| val.getClass().equals(Byte.class)
|| val.getClass().equals(Long.class)
|| val.getClass().equals(Double.class)
|| val.getClass().equals(Float.class)
|| val.getClass().equals(Character.class)
|| val.getClass().equals(Short.class)
|| val.getClass().equals(BigDecimal.class)
|| val.getClass().equals(BigInteger.class)
|| val.getClass().equals(Boolean.class)
|| Number.class
.isAssignableFrom(val.getClass())) {
params.put(f.getName(), String.valueOf(val));
} else if (val instanceof Date) {
Date val_date = (Date) val;
SimpleDateFormat sdf = new SimpleDateFormat(
"yyyyMMddHHmm");
params.put(f.getName(), sdf.format(val_date));
} else if (Collection.class.isAssignableFrom(val
.getClass())) {
Collection collection = (Collection<?>) val;
StringBuffer sb = new StringBuffer();
int j = 0;
for (Object s : collection) {
j++;
sb.append(String.valueOf(s));
if (j < collection.size()) {
sb.append(",");
}
}
params.put(f.getName(), sb.toString());
} else if (Object[].class.isInstance(val)) {
StringBuffer sb = new StringBuffer();
int j = 0;
for (Object s : (Object[]) val) {
j++;
sb.append(String.valueOf(s));
if (j < ((Object[]) val).length) {
sb.append(",");
}
}
params.put(f.getName(), sb.toString());
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 冒泡排序
*
* @param strDate
*/
private static void Bubble_sort(String[] strDate) {
for (int i = 0; i < strDate.length; i++) {
for (int j = strDate.length - 1; j > i; j--) {
if (strDate[j].compareTo(strDate[j - 1]) < 0) {
String temp = strDate[j - 1];
strDate[j - 1] = strDate[j];
strDate[j] = temp;
}
}
}
}
}