Javascript生成UUID(方法2)

可以先直接在线生成一个uuid,预览一下,

这个网站不错,可以选择uuid version,还可以直接去掉中划线,好用的不得了啊,哈哈
http://www.uuid.online/

//On creation of a UUID object, set it‘s initial value
function UUID(){
    this.id = this.createUUID();
}

// When asked what this Object is, lie and return it‘s value
UUID.prototype.valueOf = function(){ return this.id; };
UUID.prototype.toString = function(){ return this.id; };

//
// INSTANCE SPECIFIC METHODS
//
UUID.prototype.createUUID = function(){
    //
    // Loose interpretation of the specification DCE 1.1: Remote Procedure Call
    // since JavaScript doesn‘t allow access to internal systems, the last 48 bits
    // of the node section is made up using a series of random numbers (6 octets long).
    //
    var dg = new Date(1582, 10, 15, 0, 0, 0, 0);
    var dc = new Date();
    var t = dc.getTime() - dg.getTime();
    var tl = UUID.getIntegerBits(t,0,31);
    var tm = UUID.getIntegerBits(t,32,47);
    var thv = UUID.getIntegerBits(t,48,59) + ‘1‘; // version 1, security version is 2
    var csar = UUID.getIntegerBits(UUID.rand(4095),0,7);
    var csl = UUID.getIntegerBits(UUID.rand(4095),0,7);

    // since detection of anything about the machine/browser is far to buggy,
    // include some more random numbers here
    // if NIC or an IP can be obtained reliably, that should be put in
    // here instead.
    var n = UUID.getIntegerBits(UUID.rand(8191),0,7) +
            UUID.getIntegerBits(UUID.rand(8191),8,15) +
            UUID.getIntegerBits(UUID.rand(8191),0,7) +
            UUID.getIntegerBits(UUID.rand(8191),8,15) +
            UUID.getIntegerBits(UUID.rand(8191),0,15); // this last number is two octets long
    return tl + tm  + thv  + csar + csl + n;
};

//Pull out only certain bits from a very large integer, used to get the time
//code information for the first part of a UUID. Will return zero‘s if there
//aren‘t enough bits to shift where it needs to.
UUID.getIntegerBits = function(val,start,end){
    var base16 = UUID.returnBase(val,16);
    var quadArray = new Array();
    var quadString = ‘‘;
    var i = 0;
    for(i=0;i<base16.length;i++){
        quadArray.push(base16.substring(i,i+1));
    }
    for(i=Math.floor(start/4);i<=Math.floor(end/4);i++){
        if(!quadArray[i] || quadArray[i] == ‘‘) quadString += ‘0‘;
        else quadString += quadArray[i];
    }
    return quadString;
};

//Replaced from the original function to leverage the built in methods in
//JavaScript. Thanks to Robert Kieffer for pointing this one out
UUID.returnBase = function(number, base){
    return (number).toString(base).toUpperCase();
};

//pick a random number within a range of numbers
//int b rand(int a); where 0 <= b <= a
UUID.rand = function(max){
    return Math.floor(Math.random() * (max + 1));
};

调用方法:UUID.prototype.createUUID()

时间: 2024-10-19 02:20:18

Javascript生成UUID(方法2)的相关文章

SQL语句中生成UUID方法

SQL语句中生成UUID方法为UUID() 生成带横线UUID: select UUID()                         形如:abaffaca-fd55-11e5-b3d0-d2c510923c15 生成32位不带横线UUID: select replace(UUID?(),'-','')      形如:804a9aa2fd5311e5b3d0d2c510923c15? 原文地址:https://www.cnblogs.com/tianmh/p/9088709.html

javascript 生成UUID(方法1)

可以先直接在线生成一个uuid,预览一下, 这个网站不错,可以选择uuid version,还可以直接去掉中划线,好用的不得了啊,哈哈http://www.uuid.online/ /*! Math.uuid.js (v1.4) http://www.broofa.com mailto:[email protected] Copyright (c) 2010 Robert Kieffer Dual licensed under the MIT and GPL licenses. */ /* *

hibernate 用注解方式生成uuid方法

//配置uuid,本来jpa是不支持uuid的,但借用hibernate的方法可以实现. @GeneratedValue(generator = "uuid") @GeneratedValue(generator = "uuid") @GenericGenerator(name = "uuid", strategy = "uuid") 加在id的get方法上面 下面具体操作下: 1.同样的 先新建一个java project.

javascript 生成UUID

代码一: /*! Math.uuid.js (v1.4) http://www.broofa.com mailto:[email protected] Copyright (c) 2010 Robert Kieffer Dual licensed under the MIT and GPL licenses. */ /* * Generate a random uuid. * * USAGE: Math.uuid(length, radix) * length - the desired num

javascript 生成 uuid

全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度为128位的数字标识符.GUID 的格式为"xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx",其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数.在理想情况下,任何计算机和计算机集群都不会生成两个相同的GUID. GUID 的总数达到了2^128(3

Java,Javascript,Oracle开发生成UUID或GUID方法总结

1.Java开发生成UUID或GUID方法 // 36位的,带中划线 java.util.UUID.randomUUID().toString(); // 32位的,去掉中划线 java.util.UUID.randomUUID().toString().replaceAll("-", ""); 2.Javascript开发生成UUID或GUID方法 /* 参数middle_line true:带中划线 false不带中划线*/ function newGuid(m

Oracle中生成uuid的方法

Oracle中生成uuid的方法 下载LOFTER客户端 在Oracle SQL 提供了一个生成uuid的函数sys_guid: http://download.oracle.com/docs/cd/E11882_01/server.112/e17118/functions187.htm#i79194 http://en.wikipedia.org/wiki/Universally_unique_identifier SQL> select sys_guid() from dual ; SYS_

JS生成 UUID的四种方法

全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度为128位的数字标识符.GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx”,其中的 x 是 0-9 或 a-f 范围内的一个32位十六进制数.在理想情况下,任何计算机和计算机集群都不会生成两个相同的GUID. GUID 的总数达到了2^128(3.4×10

java、c#、oracle生成uuid的方法

1.java生成uuid string a = UUID.randomUUID().toString() 参考:https://blog.csdn.net/qq_36411874/article/details/80360207 2.C#生成uuid string a = Guid.NewGuid(); 参考:https://cloud.tencent.com/developer/ask/90572 3.oracle生成uuid select sys_guid() from dual 参考:ht