新增Toolkit类

补充一个小小的工具类

新增一个类,名为net.wendal.nutzbook.util.Toolkit, 内容如下, 取自老的nutzwx项目

  1. package net.wendal.nutzbook.util;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.IvParameterSpec;
  5. import javax.crypto.spec.SecretKeySpec;
  6. import net.wendal.nutzbook.bean.User;
  7. import org.nutz.lang.Lang;
  8. import org.nutz.lang.random.R;
  9. import org.nutz.lang.util.NutMap;
  10. import org.nutz.log.Log;
  11. import org.nutz.log.Logs;
  12. public class Toolkit {
  13. public static final Log log = Logs.get();
  14. public static String captcha_attr = "nutz_captcha";
  15. public static boolean checkCaptcha(String expected, String actual) {
  16. if (expected == null || actual == null || actual.length() == 0 || actual.length() > 24)
  17. return false;
  18. return actual.equalsIgnoreCase(expected);
  19. }
  20. public static String passwordEncode(String password, String slat) {
  21. String str = slat + password + slat + password.substring(4);
  22. return Lang.digest("SHA-512", str);
  23. }
  24. private static final String Iv = "\0\0\0\0\0\0\0\0";
  25. private static final String Transformation = "DESede/CBC/PKCS5Padding";
  26. public static String _3DES_encode(byte[] key, byte[] data) {
  27. SecretKey deskey = new SecretKeySpec(key, "DESede");
  28. IvParameterSpec iv = new IvParameterSpec(Iv.getBytes());
  29. try {
  30. Cipher c1 = Cipher.getInstance(Transformation);
  31. c1.init(Cipher.ENCRYPT_MODE, deskey, iv);
  32. byte[] re = c1.doFinal(data);
  33. return Lang.fixedHexString(re);
  34. } catch (Exception e) {
  35. log.info("3DES FAIL?", e);
  36. e.printStackTrace();
  37. }
  38. return null;
  39. }
  40. public static String _3DES_decode(byte[] key, byte[] data) {
  41. SecretKey deskey = new SecretKeySpec(key, "DESede");
  42. IvParameterSpec iv = new IvParameterSpec(Iv.getBytes());
  43. try {
  44. Cipher c1 = Cipher.getInstance(Transformation);
  45. c1.init(Cipher.DECRYPT_MODE, deskey, iv);
  46. byte[] re = c1.doFinal(data);
  47. return new String(re);
  48. } catch (Exception e) {
  49. log.debug("BAD 3DES decode", e);
  50. }
  51. return null;
  52. }
  53. public static NutMap kv2map(String kv) {
  54. NutMap re = new NutMap();
  55. if (kv == null || kv.length() == 0 || !kv.contains("="))
  56. return re;
  57. String[] tmps = kv.split(",");
  58. for (String tmp : tmps) {
  59. if (!tmp.contains("="))
  60. continue;
  61. String[] tmps2 = tmp.split("=", 2);
  62. re.put(tmps2[0], tmps2[1]);
  63. }
  64. return re;
  65. }
  66. public static String randomPasswd(User usr) {
  67. String passwd = R.sg(10).next();
  68. String slat = R.sg(48).next();
  69. usr.setSalt(slat);
  70. usr.setPassword(passwordEncode(passwd, slat));
  71. return passwd;
  72. }
  73. public static byte[] hexstr2bytearray(String str) {
  74. byte[] re = new byte[str.length() / 2];
  75. for (int i = 0; i < re.length; i++) {
  76. int r = Integer.parseInt(str.substring(i*2, i*2+2), 16);
  77. re[i] = (byte)r;
  78. }
  79. return re;
  80. }
  81. }
  • 3DES加密的封装
  • kv字符串转换
  • 密码加密hash