java安全令牌生成器

SecureRandom sr = new SecureRandom();
byte[] bytes = new byte[8];
bytes = sr.generateSeed(8);
System.out.println(new String(Hex.encode(bytes)));

 其中Hex.ecode使用的外部包

也可以使用下面这个

 1 public static String byte2hex(byte[] b) {
 2         String hs = "";
 3         String stmp = "";
 4         for (int n = 0; n < b.length; n++) {
 5             stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));
 6             if (stmp.length() == 1) {
 7                 hs = hs + "0" + stmp;
 8             } else {
 9                 hs = hs + stmp ;
10             }
11         }
12         return hs;
13     }

 

posted @ 2015-02-26 11:23  哎呦喂,我的小祖宗╰つ  阅读(980)  评论(0编辑  收藏  举报