IdGen.java 933 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. /**
  2. * Copyright &copy; 2012-2016 <a href="https://github.com/thinkgem/jeesite">JeeSite</a> All rights reserved.
  3. */
  4. package com.gxzc.zen.plug;
  5. import java.security.SecureRandom;
  6. import java.util.UUID;
  7. /**
  8. * 封装各种生成唯一性ID算法的工具类.
  9. * @author ThinkGem
  10. * @version 2013-01-15
  11. */
  12. public class IdGen {
  13. private static SecureRandom random = new SecureRandom();
  14. /**
  15. * 封装JDK自带的UUID, 通过Random数字生成, 中间无-分割.
  16. */
  17. public static String uuid() {
  18. return UUID.randomUUID().toString().replaceAll("-", "");
  19. }
  20. /**
  21. * 使用SecureRandom随机生成Long.
  22. */
  23. public static long randomLong() {
  24. return Math.abs(random.nextLong());
  25. }
  26. /**
  27. * 基于Base62编码的SecureRandom随机生成bytes.
  28. */
  29. public static String randomBase62(int length) {
  30. byte[] randomBytes = new byte[length];
  31. random.nextBytes(randomBytes);
  32. return Encodes.encodeBase62(randomBytes);
  33. }
  34. }