利用 Base64 缩短 UUID 至22位

UUID还是比较常用的,尤其在web应用里。

有时在URL中传播,感觉比较长,于是想对其进行缩短,查询了一些资料,发现目前最短是到 22 位(使用URL传播非转义字符,结合Base64)

 

废话少说,代码奉上:

Java代码   收藏代码
  1. public   class  UuidBase64ShortMap  implements  StringShortMap{  
  2.     /**  
  3.      *  
  4.      *把UUID 转为 22位长字符串  
  5.      */   
  6.     public  String shorter(String s) {  
  7.         char [] res = Base64.encode(asBytes(s));  
  8.         return   new  String(res, 0 ,res.length- 2 );  
  9.     }  
  10.   
  11.    /**  
  12.      *  
  13.      *把22位长字符串转为 UUID  
  14.      */   
  15.     public  String recover(String s) {  
  16.         int  len = s.length();  
  17.         char [] chars =  new   char [len+ 2 ];  
  18.         chars[len]=chars[len+1 ]= '_' ;  
  19.         for ( int  i= 0 ;i<len;i++){  
  20.             chars[i]=s.charAt(i);  
  21.         }  
  22.         return  toUUID(Base64.decode(chars)).toString();  
  23.     }  
  24.     public   static   byte [] asBytes(String id) {  
  25.         UUID uuid=UUID.fromString(id);  
  26.         long  msb = uuid.getMostSignificantBits();  
  27.         long  lsb = uuid.getLeastSignificantBits();  
  28.         byte [] buffer =  new   byte [ 16 ];  
  29.   
  30.         for  ( int  i =  0 ; i <  8 ; i++) {  
  31.                 buffer[i] = (byte ) (msb >>>  8  * ( 7  - i));  
  32.         }  
  33.         for  ( int  i =  8 ; i <  16 ; i++) {  
  34.                 buffer[i] = (byte ) (lsb >>>  8  * ( 7  - i));  
  35.         }  
  36.         return  buffer;  
  37.   
  38.     }  
  39.   
  40.     public   static  UUID toUUID( byte [] byteArray) {  
  41.         long  msb =  0 ;  
  42.         long  lsb =  0 ;  
  43.         for  ( int  i =  0 ; i <  8 ; i++)  
  44.                 msb = (msb << 8 ) | (byteArray[i] &  0xff );  
  45.         for  ( int  i =  8 ; i <  16 ; i++)  
  46.                 lsb = (lsb << 8 ) | (byteArray[i] &  0xff );  
  47.         UUID result = new  UUID(msb, lsb);  
  48.   
  49.         return  result;  
  50.     }  
  51.       
  52.     public   static   void  main(String[] args) {  
  53.                 StringShortMap shortm = new  UuidBase64ShortMap();  
  54. //        String uuid = "3b01174f-3139-4a5b-a949-bb7e80b55f91";   
  55. //        System.out.println(Base64.encode(uuid));   
  56. //        String shorter= shortm.shorter(uuid);   
  57. //        System.out.println(shorter);   
  58. //        System.out.println(shortm.recover(shorter) +"\t" + uuid);   
  59.           
  60. //        for(int i=0;i<100000;i++){   
  61. //            String Uuid = UUID.randomUUID().toString();   
  62. //            System.out.println(Uuid +"\t" + shortm.shorter(Uuid));   
  63. //        }   
  64.           
  65.         String[] uuids={"f6e0e040-be3d-4573-964c-88724a8fa7d3" , "c19b9de1-f33a-494b-afbe-f06817218d63" , "f08f0b2c-66fb-41a3-99c3-ac206089c3ad" };  
  66.         for (String s :uuids){  
  67.             System.out.println(shortm.shorter(s));  
  68.         }  
  69.     }  
  70. }  

运行结果

Java代码   收藏代码
  1. 9uDgQL49RXOWTIhySo*n0w  
  2. wZud4fM6SUuvvvBoFyGNYw  
  3. 8I8LLGb7QaOZw6wgYInDrQ  
 

有更好的方案,欢迎讨论!!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值