Browse Source

add

pull/1/head
rekoe 9 years ago
parent
commit
ecd4213e7c
  1. 118
      src/main/java/com/rekoe/utils/EncryptUtil.java
  2. 43
      src/main/java/com/rekoe/utils/UsrProvider.java

118
src/main/java/com/rekoe/utils/EncryptUtil.java

@ -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);
}
}
}

43
src/main/java/com/rekoe/utils/UsrProvider.java

@ -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