2009-08-24 78 views
14

ブラウザで一意のIDを生成する必要があります。現在、私はこれを使用しています:ユニークなIDを生成する最良の方法クライアントサイド(JavaScriptを使用)

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

私は、現在のUNIX時間((new Date).getTime())を使用したいのですが、私は心配している2つのクライアントが正確同時にIDを生成する場合、彼らはwouldnこと一意ではありません。

私は現在のUNIX時間を使用できますか(その方法でIDが詳細情報を保存するためです)。そうでない場合は、これを行うための最善の方法何

答えて

16

あなたは以下のリンクを使用してGUIDを作成することができます(多分UNIXタイム+ 2桁のランダムな数字は?):

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

Create GUID/UUID in JavaScript?

この意志を"独自性"を最大限に引き出す

セキュリティで保護されたページの場合は、複数の同時生成された値を防ぐために、日付/時刻をユーザー名と連結することもできます。

+2

を。 – kayteen

+1

+1 "複数の同時生成された値を防ぐために、ユーザー名と日付/時刻を連結します。" – Imagist

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

+0

apologies @lolol –

+0

なぜ 'c'は' cuniq() 'の変数宣言の外側ですか?また、このアルゴリズムの背後にあるロジックについて説明してください。 – vsync

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使用することができます。私は、ユーザー名とDATE/TIMEで連結し、最後の提案となるだろう

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

これはシステムのエントロピーを使っているので、ブラウザの中に入ることができます。 –

関連する問題