File tree Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Expand file tree Collapse file tree 3 files changed +43
-0
lines changed Original file line number Diff line number Diff line change 11'use strict'
22
33exports . randomBytes = exports . rng = exports . pseudoRandomBytes = exports . prng = require ( 'randombytes' )
4+
5+ exports . getRandomValues = function ( abv ) {
6+ var l = abv . length
7+ while ( l -- ) {
8+ var bytes = exports . randomBytes ( 7 )
9+ var randomFloat = ( bytes [ 0 ] % 32 ) / 32
10+
11+ for ( var i = 0 ; i < bytes . length ; i ++ ) {
12+ var byte = bytes [ i ]
13+ randomFloat = ( randomFloat + byte ) / 256
14+ }
15+
16+ abv [ l ] = Math . floor ( randomFloat * 256 )
17+ }
18+ return abv
19+ }
20+
21+ exports . randomUUID = function ( ) {
22+ return '10000000-1000-4000-8000-100000000000' . replace ( / [ 0 1 8 ] / g, function ( c ) {
23+ return ( c ^ ( exports . getRandomValues ( new Uint8Array ( 1 ) ) [ 0 ] & ( 15 >> ( c / 4 ) ) ) ) . toString ( 16 )
24+ } )
25+ }
26+
427exports . createHash = exports . Hash = require ( 'create-hash' )
528exports . createHmac = exports . Hmac = require ( 'create-hmac' )
629
Original file line number Diff line number Diff line change @@ -5,6 +5,9 @@ if (!process.browser) {
55}
66
77require ( './pbkdf2' )
8+
9+ require ( './random-uuid' )
10+
811try {
912 require ( 'randombytes' ) ( 8 )
1013 require ( './ecdh' )
Original file line number Diff line number Diff line change 1+ var test = require ( 'tape' )
2+ var crypto = require ( '../' )
3+
4+ test ( 'randomuuid' , function ( t ) {
5+ var uuid1 = crypto . randomUUID ( )
6+ var uuid2 = crypto . randomUUID ( )
7+
8+ t . ok ( uuid1 , 'first uuid truthy' )
9+ t . ok ( uuid2 , 'second uuid truthy' )
10+
11+ t . notEqual ( uuid1 , uuid2 , 'consecutive uuid\'s do not match' )
12+
13+ t . match ( uuid1 , / ^ [ 0 - 9 A - F ] { 8 } - [ 0 - 9 A - F ] { 4 } - [ 4 ] [ 0 - 9 A - F ] { 3 } - [ 8 9 A B ] [ 0 - 9 A - F ] { 3 } - [ 0 - 9 A - F ] { 12 } $ / i, 'first uuid matches uuid regex' )
14+ t . match ( uuid2 , / ^ [ 0 - 9 A - F ] { 8 } - [ 0 - 9 A - F ] { 4 } - [ 4 ] [ 0 - 9 A - F ] { 3 } - [ 8 9 A B ] [ 0 - 9 A - F ] { 3 } - [ 0 - 9 A - F ] { 12 } $ / i, 'second uuid matches uuid regex' )
15+
16+ t . end ( )
17+ } )
You can’t perform that action at this time.
0 commit comments