/*
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/>");
}
*/
分享到:
相关推荐
UUID.js, 用于JavaScript的符合RFC的UUID生成器 电子邮件名称UUID.js - 适用于JavaScript的兼容UUID生成器概要<!-- HTML5 -->[removed][removed]&
通过这种方式生成的UUID虽然满足了唯一性要求,但并不完全符合RFC 4122标准的UUID,因为标准的UUID有特定的版本和结构。然而,在许多情况下,这种简化的实现已经足够使用。在实际项目中,可以考虑使用如`...
UUID.js-JavaScript的RFC兼容UUID生成器 概要 <!-- HTML5 --> < script src =" src/uuid.js " > </ script > < script > var uuid = UUID . generate ( ) ; </ script > // Node.js...
在`uuidTest.html`中,开发者可能会创建一些按钮或事件监听器,用于触发`uuid.js`中的UUID生成函数,并将生成的UUID显示在页面上,或者将其存储在某个数据结构中,以便于检查其唯一性。 总的来说,JS生成UUID是...
《JavaScript中的UUID生成:深入理解Math.uuid.js》 在JavaScript编程中,UUID(Universally Unique Identifier)是一种广泛应用的全局唯一标识符,它主要用于创建对象的唯一ID,尤其是在分布式环境中。Math.uuid....
'-uuid UUID v4 生成器和验证器(符合 RFC4122) 名称用户名版本0.1.1 尺寸1 KB / > 1 KB (gzipped) 环境浏览器,Node.js安装通过 npm install lil-uuid 通过bower install lil-uuid 通过component install lil-js/...
在上述代码示例中,我们看到一个简单的UUID生成器类`UUIDGenerator`: 1. 类`UUIDGenerator`包含两个方法:`getUUID()`和`getUUID(int number)`。 - `getUUID()`方法生成一个UUID,并将其转换为字符串形式。然后,...
当您包含crypto库时,Angular.js 中最安全的 uuid 生成器 angular . module ( 'yourApp' , [ 'ngUUID' ] ) . run ( function ( $uuid , $log ) { var id = $uuid ( ) ; $log . info ( 'Generated uuid: ' + id ) ...
在 JavaScript 中,可以使用 `uuid` 库来生成这些 ID,这有助于避免用户记录之间的冲突。 3. **密码加密**: 为了保护用户信息安全,密码通常不以明文形式存储。本项目中可能使用了某种加密方法,如 `bcrypt` 或 `...
【Paw REST客户端的UUID生成器】是一款专为Paw设计的扩展插件,它能够帮助用户在构建RESTful API请求时动态生成UUID,从而在请求中使用唯一标识符。UUID,即通用唯一识别码(Universally Unique Identifier),在...
Webpack和rollup.js模块捆绑器 安全-加密强度高的随机值 小型-零依赖性,占用空间小,可与“摇树”包装机一起使用 CLI-包括实用程序 从uuid@3.x升级? 您的代码可能还不错,但请查看获取详细信息。 快速开始 要创建...
在UUID生成过程中,使用了`Math.random`函数生成0到1之间的随机浮点数。通过这个随机数乘以字符集的长度`radix`,再通过取整操作(`Math.floor`或`0|`),得到一个介于0和字符集长度减1之间的索引值,用以从字符集中...
ChanceJavaScript的随机生成器助手是JavaScript开发者的一个强大工具,它为创建各种随机数据提供了便利。这个库被设计成轻量级且易于使用,能够帮助开发者在测试、填充数据库或者进行模拟时快速生成随机字符串、数字...
在JavaScript中,我们可以通过自定义函数来生成UUID。以下将详细讲解如何用JS生成UUID的方法实例。 首先,我们需要理解UUID的格式。标准的UUID是一个128位的数字,通常用16进制表示,形式为8-4-4-4-12的32个字符,...
@ lukeed / uuid 适用于Node和浏览器的微型(〜230B)和UUID(v4)生成器。 该模块提供两种供您选择: 默认值为“非安全”,它使用Math.random生成UUID。 “安全”模式使用当前环境的crypto模块生成加密安全(CSPRNG...
UUID V4是UUID标准的一个版本,它通过随机数生成器来创建UUID。在UUID V4中,大部分位数(122位)是随机生成的,确保了全局的唯一性。 在IT行业中,UUID V4广泛应用于生成不重复的标识符,特别是在分布式系统、...
以下是一个简单的UUID生成器示例: ```javascript function generateUUID() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { let r = Math.random() * 16 | 0, v = c == 'x' ? ...
Airtable Record UUID生成器 通过cli或通过在节点中要求它来生成airtable记录uuid。 受启发 用法: 如果NPM> = 5.0 npx airtable - uuid 或者 npm i - g airtable - uuid airtable - uuid // recl2mht2j000VGSD ...
该模块是一个生成器,验证器和转换器,可以将UUID转换为人类可读的Base32-Crockford编码的API密钥。 变成这样: 9b3ac4c9-0228-4e42-a244-927059b1a5ea 到这个: KCXC9JD-08M4WGH-M9294W1-B6RTBTH 有关Base32-...