`

js uuid 算法 转

阅读更多
/*

uuid.js - Version 0.3
JavaScript Class to create a UUID like identifier

Copyright (C) 2006-2008, Erik Giberti (AF-Design), All rights reserved.

This program is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free Software
Foundation; either version 2 of the License, or (at your option) any later
version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with
this program; if not, write to the Free Software Foundation, Inc., 59 Temple
Place, Suite 330, Boston, MA 02111-1307 USA

The latest version of this file can be downloaded from
http://www.af-design.com/resources/javascript_uuid.php

HISTORY:
6/5/06     - Initial Release
5/22/08 - Updated code to run faster, removed randrange(min,max) in favor of
          a simpler rand(max) function. Reduced overhead by using getTime()
          method of date class (suggestion by James Hall).
9/5/08    - Fixed a bug with rand(max) and additional efficiencies pointed out
      by Robert Kieffer http://broofa.com/

KNOWN ISSUES:
- Still no way to get MAC address in JavaScript
- Research into other versions of UUID show promising possibilities
  (more research needed)
- Documentation needs improvement

*/

// 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
    // described at http://www.opengroup.org/onlinepubs/009629399/apdxa.htm#tagtcjh_37
    // 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;
}


//
// GENERAL METHODS (Not instance specific)
//


// 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));
}

// end of UUID class file
/*
for(var i = 0; i < 10; i++){
    document.write(new UUID().id);
    document.write("<br/>");
}
*/
分享到:
评论

相关推荐

    javascript生成uuid的js库文件

    常见的JavaScript UUID库有`uuid-js`、`uuid`和`shortid`等。 1. `uuid-js`库:这是一个基于RFC4122标准实现的JavaScript库,提供了V1到V5的所有版本UUID生成方法。其中,V1基于时间戳和MAC地址生成,V4完全是随机...

    javascript UUID---js

    UUID有五个版本,每个版本有不同的生成算法: - **V1**:基于时间戳和MAC地址。 - **V2**:与V1类似,但加入了POSIX用户ID或组ID。 - **V3**:基于命名空间和MD5散列。 - **V4**:完全随机生成,最常用于JavaScript...

    Math.uuid.js

    《JavaScript中的UUID生成:深入理解Math.uuid.js》 在JavaScript编程中,UUID(Universally Unique Identifier)是一种广泛应用的全局唯一标识符,它主要用于创建对象的唯一ID,尤其是在分布式环境中。Math.uuid....

    JS生成UUID

    如果`uuid.js`不是使用`uuid`库,那么它可能包含了一个自定义的UUID生成算法。这可能涉及使用`Math.random()`生成随机数字,结合当前时间戳和其他因素,以确保生成的字符串的唯一性。 4. **测试与应用**: 在`...

    uuid生成器

    - 无意义性:UUID是随机或基于特定算法生成的,因此它没有直接关联到任何特定的信息,这有助于保护数据隐私。 综上所述,UUID生成器是一个便捷的工具,用于快速、简单地获取UUID,广泛应用于各种IT场景,以保证数据...

    UUID生成模块源码,支持版本1,3,4,5

    提到的GitHub项目`uuidjs/uuid`和`google/uuid`都是开源的JavaScript库,提供了对UUID生成的支持,特别是对版本1、3、4、5的实现。这些库可以方便地在JavaScript应用中生成和操作UUID。`@mole`可能是对某个开发者或...

    Js Snowflake(雪花算法)生成随机ID的实现方法

    【Js Snowflake算法详解】 Js Snowflake算法,也称为雪花算法,是一种分布式系统中用于生成全局唯一ID的算法。该算法由Twitter开源,其生成的ID由64位的二进制数字组成,分为以下几个部分: 1. **时间戳**(41位)...

    pure-uuid:基于纯JavaScript的通用唯一标识符(UUID)

    此实现的要点(与许多其他实现相比)是:首先,虽然在内部需要 32/64 位无符号整数算法和 MD5/SHA-1 摘要算法,但此 UUID 实现是完全独立且无依赖的. 其次,此实现围绕Uint8Array 、 Buffer或Array标准类,这种方式...

    uuid 资料包

    在编程语言中,UUID的使用非常广泛,例如在Java、Python、JavaScript等语言中都有对应的库或内置支持。在数据库设计中,UUID常用于主键,因为它可以避免在分布式环境中出现主键冲突。在分布式服务和微服务架构中,...

    利用mysql实现的雪花算法案例

    需要注意的是,由于JavaScript的最大整数限制在53位,所以如果要与前端交互,建议调整算法,使得ID不超过53位。例如,使用32位存储秒级时间戳,5位存储机器码,16位存储序列号,这样既能满足JS处理,又能保证足够的...

    用JS生成UUID的方法实例

    在JavaScript(JS)中,生成UUID通常用于创建唯一的ID,比如在数据库操作、网页交互或者网络通信中。下面我们将详细讨论如何在JavaScript中生成UUID,以及在实际应用中的价值。 首先,我们来看一下提供的JS代码实例...

    JS实现点击生成UUID的方法完整实例【基于jQuery】

    1. **JavaScript生成UUID的概念**: UUID是通用唯一识别码(Universally Unique Identifier)的缩写,它是一个128位的值,用于创建唯一标识符。在计算机系统中,UUID常用于生成具有唯一性的标识,如数据库记录、API...

    tx充值QB页面的mobile-save接口中的encrypt-msg算法

    tx充值QB页面的mobile_save接口中的encrypt_msg算法 易语言纯算法腾讯充值页面encrypt_msg算法易语言算法,成品算法tx充值QB页面的mobile_save接口中的encrypt_msg值算法。 本帖学习研究探讨 目标网站地址 ...

    易语言-UUID生成模块源码,支持版本1,3,4,5

    版本3的UUID是基于命名空间和MD5散列算法生成的,通过特定的命名策略和算法确保唯一性,同时保留了一定的可预测性。 版本4的UUID是完全随机生成的,提供最纯粹的唯一性,但没有版本3的可追溯性。 版本5的UUID与...

    zazu-uuid-generator:简单的Zazu插件可生成UUID

    8. **Node.js**: Node.js是JavaScript的服务器端运行环境,它使得JavaScript可以用于构建命令行工具和桌面应用,"zazu-uuid-generator"就依赖于Node.js运行。 9. **软件设计模式**: 这个插件展示了命令行工具如何...

    shortuuid:基于JDK UUID的shortuuid生成器

    它通过使用自定义的编码算法,将UUID的128位二进制数据转化为一个更短的字符串,同时保持其唯一性。这种短UUID通常比标准UUID短得多,更适合在URL、数据库键或者文件名等场景中使用。 使用shortuuid库非常简单。...

    Javascript生成全局唯一标识符(GUID,UUID)的方法

    全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) 。 GUID是一种由算法生成的二进制长度为128位的数字标识符。GUID 的格式为“xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx...

    uuidgenerator

    对于开发者来说,通过阅读源码,可以学习到如何在JavaScript中实现UUID生成算法,以及如何组织和维护一个开源项目。如果项目包括测试用例,还可以了解单元测试和集成测试的方法。 在实际应用中,UUID在Web开发中的...

    JavaScript生成GUID的多种算法小结

    ### JavaScript生成GUID的多种算法详解 #### 一、概述 全局唯一标识符(Global Unique Identifier,简称GUID)或称为通用唯一标识符(Universally Unique Identifier,简称UUID),是一种由算法生成的二进制长度为...

Global site tag (gtag.js) - Google Analytics