`
zxc3375
  • 浏览: 134522 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Java、JavaScript、php、mysql、oracle生成uuid(32位)的方法

阅读更多

Java、JavaScript、php、mysql、oracle生成uuid(32位)的方法

一、Java:

 

[java] view plaincopy
 
  1. import java.net.InetAddress;  
  2.   
  3. public class IDGenerator {  
  4.   
  5.     private String sep = "";  
  6.   
  7.     private static final int IP;  
  8.   
  9.     private static short counter = (short0;  
  10.   
  11.     private static final int JVM = (int) (System.currentTimeMillis() >>> 8);  
  12.   
  13.     private static IDGenerator uuidgen = new IDGenerator();  
  14.   
  15.     static {  
  16.         int ipadd;  
  17.         try {  
  18.             ipadd = toInt(InetAddress.getLocalHost().getAddress());  
  19.         } catch (Exception e) {  
  20.             ipadd = 0;  
  21.         }  
  22.         IP = ipadd;  
  23.     }  
  24.   
  25.     public static IDGenerator getInstance() {  
  26.         return uuidgen;  
  27.     }  
  28.   
  29.     public static int toInt(byte[] bytes) {  
  30.         int result = 0;  
  31.         for (int i = 0; i < 4; i++) {  
  32.             result = (result << 8) - Byte.MIN_VALUE + (int) bytes[i];  
  33.         }  
  34.         return result;  
  35.     }  
  36.   
  37.     protected String format(int intval) {  
  38.         String formatted = Integer.toHexString(intval);  
  39.         StringBuffer buf = new StringBuffer("00000000");  
  40.         buf.replace(8 - formatted.length(), 8, formatted);  
  41.         return buf.toString();  
  42.     }  
  43.   
  44.     protected String format(short shortval) {  
  45.         String formatted = Integer.toHexString(shortval);  
  46.         StringBuffer buf = new StringBuffer("0000");  
  47.         buf.replace(4 - formatted.length(), 4, formatted);  
  48.         return buf.toString();  
  49.     }  
  50.   
  51.     protected int getJVM() {  
  52.         return JVM;  
  53.     }  
  54.   
  55.     protected synchronized short getCount() {  
  56.         if (counter < 0) {  
  57.             counter = 0;  
  58.         }  
  59.         return counter++;  
  60.     }  
  61.   
  62.     protected int getIP() {  
  63.         return IP;  
  64.     }  
  65.   
  66.     protected short getHiTime() {  
  67.         return (short) (System.currentTimeMillis() >>> 32);  
  68.     }  
  69.   
  70.     protected int getLoTime() {  
  71.         return (int) System.currentTimeMillis();  
  72.     }  
  73.   
  74.     public String generate() {  
  75.         return new StringBuffer(36).append(format(getIP())).append(sep).append(  
  76.                 format(getJVM())).append(sep).append(format(getHiTime()))  
  77.                 .append(sep).append(format(getLoTime())).append(sep).append(  
  78.                         format(getCount())).toString();  
  79.     }  
  80. }  

二、JavaScript:

 

 

[javascript] view plaincopy
 
  1. (function(window){  
  2.     //On creation of a UUID object, set it's initial value  
  3.     function UUID() {  
  4.         this.id = this.createUUID();  
  5.     }  
  6.   
  7.     // When asked what this Object is, lie and return it's value  
  8.     UUID.prototype.valueOf = function() {  
  9.         return this.id;  
  10.     };  
  11.     UUID.prototype.toString = function() {  
  12.         return this.id;  
  13.     };  
  14.   
  15.     //  
  16.     // INSTANCE SPECIFIC METHODS  
  17.     //  
  18.     UUID.prototype.createUUID = function() {  
  19.         //  
  20.         // Loose interpretation of the specification DCE 1.1: Remote Procedure Call  
  21.         // since JavaScript doesn't allow access to internal systems, the last 48 bits   
  22.         // of the node section is made up using a series of random numbers (6 octets long).  
  23.         //    
  24.         var dg = new Date(1582, 10, 15, 0, 0, 0, 0);  
  25.         var dc = new Date();  
  26.         var t = dc.getTime() - dg.getTime();  
  27.         var tl = UUID.getIntegerBits(t, 0, 31);  
  28.         var tm = UUID.getIntegerBits(t, 32, 47);  
  29.         var thv = UUID.getIntegerBits(t, 48, 59) + '1'// version 1, security version is 2  
  30.         var csar = UUID.getIntegerBits(UUID.rand(4095), 0, 7);  
  31.         var csl = UUID.getIntegerBits(UUID.rand(4095), 0, 7);  
  32.         // since detection of anything about the machine/browser is far to buggy,   
  33.         // include some more random numbers here  
  34.         // if NIC or an IP can be obtained reliably, that should be put in  
  35.         // here instead.  
  36.         var n = UUID.getIntegerBits(UUID.rand(8191), 0, 7)  
  37.                 + UUID.getIntegerBits(UUID.rand(8191), 8, 15)  
  38.                 + UUID.getIntegerBits(UUID.rand(8191), 0, 7)  
  39.                 + UUID.getIntegerBits(UUID.rand(8191), 8, 15)  
  40.                 + UUID.getIntegerBits(UUID.rand(8191), 0, 15); // this last number is two octets long  
  41.         return tl + tm + thv + csar + csl + n;  
  42.     };  
  43.   
  44.     //Pull out only certain bits from a very large integer, used to get the time  
  45.     //code information for the first part of a UUID. Will return zero's if there   
  46.     //aren't enough bits to shift where it needs to.  
  47.     UUID.getIntegerBits = function(val, start, end) {  
  48.         var base16 = UUID.returnBase(val, 16);  
  49.         var quadArray = new Array();  
  50.         var quadString = '';  
  51.         var i = 0;  
  52.         for (i = 0; i < base16.length; i++) {  
  53.             quadArray.push(base16.substring(i, i + 1));  
  54.         }  
  55.         for (i = Math.floor(start / 4); i <= Math.floor(end / 4); i++) {  
  56.             if (!quadArray[i] || quadArray[i] == '')  
  57.                 quadString += '0';  
  58.             else  
  59.                 quadString += quadArray[i];  
  60.         }  
  61.         return quadString;  
  62.     };  
  63.   
  64.     //Replaced from the original function to leverage the built in methods in  
  65.     //JavaScript. Thanks to Robert Kieffer for pointing this one out  
  66.     UUID.returnBase = function(number, base) {  
  67.         return (number).toString(base).toUpperCase();  
  68.     };  
  69.   
  70.     //pick a random number within a range of numbers  
  71.     //int b rand(int a); where 0 <= b <= a  
  72.     UUID.rand = function(max) {  
  73.         return Math.floor(Math.random() * (max + 1));  
  74.     };  
  75.       
  76.     //变成全局函数  
  77.     window.createUUID = function(type){  
  78.         var _type = (typeof type == 'undefined' || (type!='l'&&type!='u')) ? 'l' : type;  
  79.         if(_type=='u'){  
  80.             return new UUID().toString();  
  81.         }else {  
  82.             return new UUID().toString().toLowerCase();  
  83.         }  
  84.     };  
  85. })(window);  

三、PHP:

 

 

[php] view plaincopy
 
  1. class System {  
  2.     function currentTimeMillis() {  
  3.         list ($usec$sec) = explode("  ", microtime());  
  4.         return $sec . substr($usec, 2, 3);  
  5.     }  
  6. }  
  7. class NetAddress {  
  8.     var $Name = 'localhost';  
  9.     var $IP = '127.0.0.1';  
  10.     function getLocalHost() //  static     
  11.     {  
  12.         $address = new NetAddress();  
  13.         $address->Name = $_ENV["COMPUTERNAME"];  
  14.         $address->IP = $_SERVER["SERVER_ADDR"];  
  15.         return $address;  
  16.     }  
  17.     function toString() {  
  18.         return strtolower($this->Name . '/' . $this->IP);  
  19.     }  
  20. }  
  21. class Random {  
  22.     function nextLong() {  
  23.         $tmp = rand(0, 1) ? '-' : '';  
  24.         return $tmp . rand(1000, 9999) . rand(1000, 9999) . rand(1000, 9999) . rand(100, 999) . rand(100, 999);  
  25.     }  
  26. }  
  27. //  三段     
  28. //  一段是微秒  一段是地址  一段是随机数     
  29. class Guid {  
  30.     var $valueBeforeMD5;  
  31.     var $valueAfterMD5;  
  32.     function Guid() {  
  33.         $this->getGuid();  
  34.     }  
  35.     //     
  36.     function getGuid() {  
  37.         $address = NetAddress :: getLocalHost();  
  38.         $this->valueBeforeMD5 = $address->toString() . ':' . System :: currentTimeMillis() . ':' . Random :: nextLong();  
  39.         $this->valueAfterMD5 = md5($this->valueBeforeMD5);  
  40.     }  
  41.     function newGuid() {  
  42.         $Guid = new Guid();  
  43.         return $Guid;  
  44.     }  
  45.     function toString() {  
  46.         $raw = strtoupper($this->valueAfterMD5);  
  47.         return substr($raw, 0, 8) . substr($raw, 8, 4) . substr($raw, 12, 4) . substr($raw, 16, 4) . substr($raw, 20);  
  48.     }  
  49. }  
  50.   
  51. //生成32位uuid  
  52. function createUUID(){  
  53.     $Guid = new Guid();  
  54.     $uuid = $Guid->toString();  
  55.     return strtolower($uuid);  
  56. }  


四、MySql:

 

 

[sql] view plaincopy
 
  1. SELECT UUID();  //带“-”  
  2. SELECT REPLACE(UUID(), '-''');  //不带“-”  


五、Oracle:

 

 

[sql] view plaincopy
 
  1. select sys_guid() from dual;//大写  
[sql] view plaincopy
 
  1.  select LOWER(sys_guid()) from dual;//小写  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics