2009-08-24 135 views
14

我需要在瀏覽器中生成唯一的ID。目前,我使用的是這樣的:生成唯一ID客戶端(使用Javascript)的最佳方法

Math.floor(Math.random() * 10000000000000001) 

我想使用當前的UNIX時間((new Date).getTime()),但我很擔心,如果兩個客戶端在確切同時生成的ID,他們止跌不是唯一的。

我可以使用當前的UNIX時間嗎(我想因爲那種方式ID會存儲更多的信息)?如果沒有,什麼是做到這一點的最好辦法

回答

16

您可以使用以下鏈接創建一個GUID(也許UNIX時間+ 2張的隨機數?):

http://softwareas.com/guid0-a-javascript-guid-generator

Create GUID/UUID in JavaScript?

這將最大化你的「獨特性」的機會。

或者,如果它是安全頁面,則可以將日期/時間與用戶名連接起來以防止同時生成多個值。

+2

我會去的最後一個建議,連接具有日期/時間與用戶名。 – kayteen 2009-08-24 05:19:49

+1

+1「用連接用戶名連接日期/時間以防止多個同時生成的值。」。 – Imagist 2009-08-24 05:37:30

8

https://github.com/broofa/node-uuid提供基於時間戳或隨機#的符合RFC的UUID。沒有依賴關係的單一文件,支持時間戳或隨機的基於UUID,使用本機API加密質量的隨機數(如果可用)以及其他好東西。

1
var c = 1; 
function cuniq() { 
    var d = new Date(), 
     m = d.getMilliseconds() + "", 
     u = ++d + m + (++c === 10000 ? (c = 1) : c); 

    return u; 
} 
+1

不錯的一段代碼,但c未定義 – lolol 2013-10-14 18:01:24

+0

apologies @lolol – 2013-10-28 11:15:55

+0

爲什麼'c'在'cuniq()'中的變量聲明之外?另外,你能解釋一下這個算法的邏輯嗎? – vsync 2016-10-20 13:35:19

1

這是我的javascript代碼來生成guid。它快速六角映射和非常高效:

AuthenticationContext.prototype._guid = function() { 
    // RFC4122: The version 4 UUID is meant for generating UUIDs from truly-random or 
    // pseudo-random numbers. 
    // The algorithm is as follows: 
    //  Set the two most significant bits (bits 6 and 7) of the 
    //  clock_seq_hi_and_reserved to zero and one, respectively. 
    //  Set the four most significant bits (bits 12 through 15) of the 
    //  time_hi_and_version field to the 4-bit version number from 
    //  Section 4.1.3. Version4 
    //  Set all the other bits to randomly (or pseudo-randomly) chosen 
    //  values. 
    // UUID     = time-low "-" time-mid "-"time-high-and-version "-"clock-seq-reserved and low(2hexOctet)"-" node 
    // time-low    = 4hexOctet 
    // time-mid    = 2hexOctet 
    // time-high-and-version = 2hexOctet 
    // clock-seq-and-reserved = hexOctet: 
    // clock-seq-low   = hexOctet 
    // node     = 6hexOctet 
    // Format: xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx 
    // y could be 1000, 1001, 1010, 1011 since most significant two bits needs to be 10 
    // y values are 8, 9, A, B 
    var guidHolder = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'; 
    var hex = 'abcdef'; 
    var r = 0; 
    var guidResponse = ""; 
    for (var i = 0; i < 36; i++) { 
     if (guidHolder[i] !== '-' && guidHolder[i] !== '4') { 
      // each x and y needs to be random 
      r = Math.random() * 16 | 0; 
     } 

     if (guidHolder[i] === 'x') { 
      guidResponse += hex[r]; 
     } else if (guidHolder[i] === 'y') { 
      // clock-seq-and-reserved first hex is filtered and remaining hex values are random 
      r &= 0x3; // bit and with 0011 to set pos 2 to zero ?0?? 
      r |= 0x8; // set pos 3 to 1 as 1??? 
      guidResponse += hex[r]; 
     } else { 
      guidResponse += guidHolder[i]; 
     } 
    } 

    return guidResponse; 
}; 
2

在現代瀏覽器,您可以使用crypto

var array = new Uint32Array(1); 
window.crypto.getRandomValues(array); 
console.log(array); 
+0

太棒了,因爲它使用的系統熵高得多,那麼你可以進入瀏覽器。 – 2017-10-30 15:25:45

相關問題