提交 | 用户 | 时间
|
58d006
|
1 |
package com.mandi.common; |
A |
2 |
|
|
3 |
import java.io.UnsupportedEncodingException; |
|
4 |
import java.security.InvalidKeyException; |
|
5 |
import java.security.MessageDigest; |
|
6 |
import java.security.NoSuchAlgorithmException; |
|
7 |
import java.security.SecureRandom; |
|
8 |
import java.text.SimpleDateFormat; |
|
9 |
import java.util.Calendar; |
|
10 |
import java.util.Date; |
|
11 |
import java.util.HashSet; |
|
12 |
import java.util.Iterator; |
|
13 |
import java.util.Random; |
|
14 |
import java.util.Set; |
|
15 |
import java.util.regex.Matcher; |
|
16 |
import java.util.regex.Pattern; |
|
17 |
|
|
18 |
import javax.crypto.BadPaddingException; |
|
19 |
import javax.crypto.Cipher; |
|
20 |
import javax.crypto.IllegalBlockSizeException; |
|
21 |
import javax.crypto.KeyGenerator; |
|
22 |
import javax.crypto.NoSuchPaddingException; |
|
23 |
import javax.crypto.SecretKey; |
|
24 |
import javax.crypto.spec.SecretKeySpec; |
|
25 |
|
|
26 |
import org.jsoup.Jsoup; |
|
27 |
import org.jsoup.nodes.Document; |
|
28 |
import org.jsoup.nodes.Element; |
|
29 |
import org.jsoup.select.Elements; |
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
public class BasicMethod { |
|
34 |
public static String byte2hexStr(byte[] buf){ |
|
35 |
if(buf==null) |
|
36 |
return null; |
|
37 |
StringBuffer sb=new StringBuffer(); |
|
38 |
for(int i=0;i<buf.length;i++){ |
|
39 |
String hex=Integer.toHexString(buf[i]&0xff); |
|
40 |
if(hex.length()==1) |
|
41 |
{ |
|
42 |
hex='0'+hex; |
|
43 |
} |
|
44 |
sb.append(hex); |
|
45 |
} |
|
46 |
return sb.toString(); |
|
47 |
} |
|
48 |
public static byte[] hexStr2bytes(String str){ |
|
49 |
if(str==null||str.length()<1) |
|
50 |
return null; |
|
51 |
byte[] result=new byte[str.length()>>1]; |
|
52 |
for(int i=0;i<str.length();i+=2){ |
|
53 |
int high=Integer.parseInt(str.substring(i, i+1),16); |
|
54 |
int low=Integer.parseInt(str.substring(i+1, i+2),16); |
|
55 |
result[i>>1]=(byte)(((high<<4)&0xf0)+low); |
|
56 |
} |
|
57 |
return result; |
|
58 |
} |
|
59 |
/** |
|
60 |
* Aes 加密 |
|
61 |
* @param source |
|
62 |
* @param password |
|
63 |
* @return |
|
64 |
*/ |
|
65 |
public static String encryptAES(String source,String password){ |
|
66 |
if(source==null||password==null) |
|
67 |
return null; |
|
68 |
try { |
|
69 |
KeyGenerator keygen=KeyGenerator.getInstance("AES"); |
|
70 |
SecureRandom sr=SecureRandom.getInstance("SHA1PRNG"); |
|
71 |
sr.setSeed(password.getBytes()); |
|
72 |
keygen.init(128,sr); |
|
73 |
SecretKey key=keygen.generateKey(); |
|
74 |
byte[] keyendoce=key.getEncoded(); |
|
75 |
SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES"); |
|
76 |
Cipher cipher=Cipher.getInstance("AES"); |
|
77 |
cipher.init(Cipher.ENCRYPT_MODE,keyspec ); |
|
78 |
byte[] sbytes=null; |
|
79 |
try { |
|
80 |
sbytes = cipher.doFinal(source.getBytes("utf8")); |
|
81 |
} catch (UnsupportedEncodingException e) { |
|
82 |
System.out.println("加密过程中,getbyte有错误!"); |
|
83 |
e.printStackTrace(); |
|
84 |
} |
|
85 |
return byte2hexStr(sbytes); //这里也可以用base64来表示,这样解密的时候反解析base64 |
|
86 |
} catch (NoSuchAlgorithmException e) { |
|
87 |
e.printStackTrace(); |
|
88 |
} catch (NoSuchPaddingException e) { |
|
89 |
e.printStackTrace(); |
|
90 |
} catch (InvalidKeyException e) { |
|
91 |
e.printStackTrace(); |
|
92 |
} catch (IllegalBlockSizeException e) { |
|
93 |
e.printStackTrace(); |
|
94 |
} catch (BadPaddingException e) { |
|
95 |
e.printStackTrace(); |
|
96 |
} |
|
97 |
|
|
98 |
return ""; |
|
99 |
} |
|
100 |
public static String decryptAES(String source,String password) |
|
101 |
{ |
|
102 |
if(source==null||password==null) |
|
103 |
return ""; |
|
104 |
byte[] sbytes=hexStr2bytes(source); |
|
105 |
try { |
|
106 |
KeyGenerator keygen=KeyGenerator.getInstance("AES"); |
|
107 |
SecureRandom sr=SecureRandom.getInstance("SHA1PRNG"); |
|
108 |
sr.setSeed(password.getBytes()); |
|
109 |
keygen.init(128,sr); |
|
110 |
SecretKey key=keygen.generateKey(); |
|
111 |
byte[] keyendoce=key.getEncoded(); |
|
112 |
SecretKeySpec keyspec=new SecretKeySpec(keyendoce, "AES"); |
|
113 |
Cipher cipher=Cipher.getInstance("AES"); |
|
114 |
cipher.init(Cipher.DECRYPT_MODE, keyspec); |
|
115 |
byte[] dbytes=cipher.doFinal(sbytes); |
|
116 |
try { |
|
117 |
return new String(dbytes,"utf8"); |
|
118 |
} catch (UnsupportedEncodingException e) { |
|
119 |
System.out.println("解密过程中,编码错误!"); |
|
120 |
e.printStackTrace(); |
|
121 |
} |
|
122 |
} catch (NoSuchAlgorithmException e) { |
|
123 |
e.printStackTrace(); |
|
124 |
} catch (NoSuchPaddingException e) { |
|
125 |
e.printStackTrace(); |
|
126 |
} catch (InvalidKeyException e) { |
|
127 |
e.printStackTrace(); |
|
128 |
} catch (IllegalBlockSizeException e) { |
|
129 |
e.printStackTrace(); |
|
130 |
} catch (BadPaddingException e) { |
|
131 |
e.printStackTrace(); |
|
132 |
} |
|
133 |
return ""; |
|
134 |
} |
|
135 |
public static String encryptSHA(String source)//SHA 摘要,不可逆 |
|
136 |
{ |
|
137 |
if(source==null) |
|
138 |
return null; |
|
139 |
MessageDigest md; |
|
140 |
try { |
|
141 |
md = MessageDigest.getInstance("SHA-256"); |
|
142 |
byte[] bytes=md.digest(source.getBytes()); |
|
143 |
return byte2hexStr(bytes); |
|
144 |
} catch (NoSuchAlgorithmException e) { |
|
145 |
e.printStackTrace(); |
|
146 |
} |
|
147 |
return ""; |
|
148 |
} |
|
149 |
public static String encryptMD5(String source)//SHA 摘要,不可逆 |
|
150 |
{ |
|
151 |
if(source==null) |
|
152 |
return null; |
|
153 |
MessageDigest md; |
|
154 |
try { |
|
155 |
md = MessageDigest.getInstance("md5"); |
|
156 |
byte[] bytes=md.digest(source.getBytes("utf-8")); |
|
157 |
return byte2hexStr(bytes); |
|
158 |
} catch (NoSuchAlgorithmException e) { |
|
159 |
e.printStackTrace(); |
|
160 |
} catch (UnsupportedEncodingException e) { |
|
161 |
e.printStackTrace(); |
|
162 |
} |
|
163 |
return ""; |
|
164 |
} |
|
165 |
public static String encryptSHA1(String source)//SHA 摘要,不可逆 |
|
166 |
{ |
|
167 |
if(source==null) |
|
168 |
return null; |
|
169 |
MessageDigest md; |
|
170 |
try { |
|
171 |
md = MessageDigest.getInstance("SHA-1"); |
|
172 |
byte[] bytes=md.digest(source.getBytes()); |
|
173 |
return byte2hexStr(bytes); |
|
174 |
} catch (NoSuchAlgorithmException e) { |
|
175 |
e.printStackTrace(); |
|
176 |
} |
|
177 |
return ""; |
|
178 |
} |
|
179 |
public static boolean checkEmailFormat(String email) |
|
180 |
{ |
|
181 |
if(email==null) |
|
182 |
return false; |
|
183 |
String patterns="[a-zA-Z]+([-.][a-zA-Z0-9_-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*" +"(\\.[A-Za-z]{2,3})$"; |
|
184 |
Pattern p=Pattern.compile(patterns); |
|
185 |
Matcher m=p.matcher(email); |
|
186 |
if(m.matches()) |
|
187 |
return true; |
|
188 |
return false; |
|
189 |
} |
|
190 |
public static boolean checkvalidusername(String username) |
|
191 |
{ |
|
192 |
if(username==null) |
|
193 |
return false; |
|
194 |
String patterns="^[a-zA-Z][a-zA-Z0-9._-]{5,50}"; |
|
195 |
Pattern p=Pattern.compile(patterns); |
|
196 |
Matcher m=p.matcher(username); |
|
197 |
if(m.matches()) |
|
198 |
return true; |
|
199 |
return false; |
|
200 |
} |
|
201 |
public static boolean checkIP(String ip) |
|
202 |
{ |
|
203 |
if(ip==null) |
|
204 |
return false; |
|
205 |
String pattern="^(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])\\.(\\d{1,2}|1\\d\\d|2[0-4]\\d|25[0-5])$"; |
|
206 |
Pattern p=Pattern.compile(pattern); |
|
207 |
Matcher m=p.matcher(ip); |
|
208 |
if(m.matches()) |
|
209 |
return true; |
|
210 |
return false; |
|
211 |
} |
|
212 |
/** |
|
213 |
* 判断date是否处于当前时间-days前 |
|
214 |
* @param date |
|
215 |
* @param days |
|
216 |
* @return |
|
217 |
*/ |
|
218 |
public static boolean before(Date date, int days) |
|
219 |
{ |
|
220 |
if(date==null) |
|
221 |
return false; |
|
222 |
Date now=new Date(System.currentTimeMillis()); |
|
223 |
Calendar nowcal=Calendar.getInstance(); |
|
224 |
nowcal.setTime(now); |
|
225 |
Calendar datecal=Calendar.getInstance(); |
|
226 |
datecal.setTime(date); |
|
227 |
nowcal.add(Calendar.DAY_OF_MONTH, -days); |
|
228 |
if(datecal.before(nowcal)) |
|
229 |
return true; |
|
230 |
return false; |
|
231 |
} |
|
232 |
/** |
|
233 |
* 判断date是否处于当前时间+days后 |
|
234 |
* @param date |
|
235 |
* @param days |
|
236 |
* @return |
|
237 |
*/ |
|
238 |
public static boolean after(Date date,int days) |
|
239 |
{ |
|
240 |
if(date==null) |
|
241 |
return false; |
|
242 |
Date now=new Date(System.currentTimeMillis()); |
|
243 |
Calendar nowcal=Calendar.getInstance(); |
|
244 |
nowcal.setTime(now); |
|
245 |
Calendar datecal=Calendar.getInstance(); |
|
246 |
datecal.setTime(date); |
|
247 |
nowcal.add(Calendar.DAY_OF_MONTH, days); |
|
248 |
if(datecal.after(nowcal)) |
|
249 |
return true; |
|
250 |
return false; |
|
251 |
} |
|
252 |
/** |
|
253 |
* 讲string字符串,规整化掉sql里面的特殊字符'和\ |
|
254 |
*/ |
|
255 |
public static String sqlformat(String param) |
|
256 |
{ |
|
257 |
if(param==null) |
|
258 |
return null; |
|
259 |
param=param.trim(); |
|
260 |
String t=param.replaceAll("'", "\\\\'"); |
|
261 |
t=t.replaceAll("\"", "\\\\\""); |
|
262 |
return t; |
|
263 |
} |
|
264 |
/** |
|
265 |
* 使用Jsoup包解析html content,并提取出所有的img超链接 |
|
266 |
* @param content |
|
267 |
* @return |
|
268 |
*/ |
|
269 |
public static String parseimgs(String content) |
|
270 |
{ |
|
271 |
if(content==null) |
|
272 |
return null; |
|
273 |
Document doc=Jsoup.parse(content); |
|
274 |
Elements eles=doc.select("img"); |
|
275 |
Iterator<Element> ele=eles.iterator(); |
|
276 |
Set<String> set=new HashSet<String>(); |
|
277 |
while(ele.hasNext()) |
|
278 |
{ |
|
279 |
Element e=ele.next(); |
|
280 |
set.add(e.attr("src")); |
|
281 |
} |
|
282 |
if(set.size()>0) |
|
283 |
return Jacksonmethod.tojson(set, false); |
|
284 |
return null; |
|
285 |
} |
|
286 |
|
|
287 |
public static int countDays(Date stime,Date etime){ |
|
288 |
if(stime==null||etime==null) |
|
289 |
return 0; |
|
290 |
return (int)((etime.getTime() - stime.getTime()) / (1000L * 60L * 60L * 24L)); |
|
291 |
} |
|
292 |
public static String randomstr(int length) |
|
293 |
{ |
|
294 |
if(length<=0) |
|
295 |
return null; |
|
296 |
char[] chars = new char[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', |
|
297 |
'9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'k', 'l', |
|
298 |
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', |
|
299 |
'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', |
|
300 |
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', |
|
301 |
'W', 'X', 'Y', 'Z' }; |
|
302 |
Random r=new Random(System.currentTimeMillis()); |
|
303 |
int charlength=chars.length; |
|
304 |
StringBuffer sb=new StringBuffer(); |
|
305 |
for (int i = 0; i <length; i++) { |
|
306 |
int ri=r.nextInt(charlength); |
|
307 |
sb.append(chars[ri]); |
|
308 |
} |
|
309 |
return sb.toString(); |
|
310 |
} |
|
311 |
|
|
312 |
public static String first2Upper(String str) |
|
313 |
{ |
|
314 |
if(str==null) |
|
315 |
return null; |
|
316 |
char[] cs=str.toCharArray(); |
|
317 |
cs[0]-=32; |
|
318 |
return String.valueOf(cs); |
|
319 |
} |
|
320 |
public static String getNow(Date now,String format){ |
|
321 |
Date d=new Date(); |
|
322 |
if(now!=null){ |
|
323 |
d=now; |
|
324 |
} |
|
325 |
SimpleDateFormat sdf=new SimpleDateFormat(format==null?"yyyy-MM-dd HH:mm:ss":format); |
|
326 |
return sdf.format(d); |
|
327 |
} |
|
328 |
} |