pull/1/head
rekoe 9 years ago
parent 5f52558309
commit ecd4213e7c

@ -0,0 +1,118 @@
package com.rekoe.utils;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
/**
*
*
* @author <a href="mailto:yuanhuiwu@gmail.com">Huiwu Yuan</a>
* @since 1.0
*/
public class EncryptUtil {
/**
*
*/
private static final String cvt = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789#@$";
/**
*
*/
private static final int fillchar = '*';
/**
*
*
* @param str
*
* @return
*/
public static String encrypt(String str) {
byte[] data = str.getBytes();
int c;
int len = data.length;
StringBuffer ret = new StringBuffer(((len / 3) + 1) * 4);
for (int i = 0; i < len; ++i) {
c = (data[i] >> 2) & 0x3f;
ret.append(cvt.charAt(c));
c = (data[i] << 4) & 0x3f;
if (++i < len) {
c |= (data[i] >> 4) & 0x0f;
}
ret.append(cvt.charAt(c));
if (i < len) {
c = (data[i] << 2) & 0x3f;
if (++i < len) {
c |= (data[i] >> 6) & 0x03;
}
ret.append(cvt.charAt(c));
} else {
++i;
ret.append((char) fillchar);
}
if (i < len) {
c = data[i] & 0x3f;
ret.append(cvt.charAt(c));
} else {
ret.append((char) fillchar);
}
}
return ret.toString();
}
/**
*
*
* @param str
*
* @return
*/
public static String decrypt(String str) {
byte[] data = str.getBytes();
int c, c1;
int len = data.length;
StringBuffer ret = new StringBuffer((len * 3) / 4);
for (int i = 0; i < len; ++i) {
c = cvt.indexOf(data[i]);
++i;
c1 = cvt.indexOf(data[i]);
c = ((c << 2) | ((c1 >> 4) & 0x3));
ret.append((char) c);
if (++i < len) {
c = data[i];
if (fillchar == c) {
break;
}
c = cvt.indexOf((char) c);
c1 = ((c1 << 4) & 0xf0) | ((c >> 2) & 0xf);
ret.append((char) c1);
}
if (++i < len) {
c1 = data[i];
if (fillchar == c1) {
break;
}
c1 = cvt.indexOf((char) c1);
c = ((c << 6) & 0xc0) | c1;
ret.append((char) c);
}
}
return ret.toString();
}
/**
* apache SHA1
*
* @param str
*
* @return
*/
public static String encriptSHA1(String str) {
try {
return new sun.misc.BASE64Encoder().encode(MessageDigest.getInstance("SHA1").digest(str.getBytes()));
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

@ -0,0 +1,43 @@
/**
*
*/
package com.rekoe.utils;
import com.rekoe.domain.Usr;
/**
* 线
*
*/
public class UsrProvider {
private static final ThreadLocal<Usr> USR_THREAD_LOCAL = new ThreadLocal<Usr>();
/**
* @return
*/
public static Usr getCurrentUsr() {
Usr usr = USR_THREAD_LOCAL.get();
if (usr == null) {
throw new RuntimeException("当前线程没有设置用户!");
}
return usr;
}
/**
* 线
*
* @param usr
*
*/
public static void setUsr(Usr usr) {
USR_THREAD_LOCAL.set(usr);
}
/**
* 线
*/
public static void removeUsr() {
USR_THREAD_LOCAL.remove();
}
}
Loading…
Cancel
Save