@@ -805,13 +805,37 @@ function joinRegExp(patterns) {
805805 return new RegExp ( sources . join ( '|' ) , 'i' ) ;
806806}
807807
808- // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
809808function uuid4 ( ) {
810- return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
811- var r = Math . random ( ) * 16 | 0 ,
812- v = c == 'x' ? r : ( r & 0x3 | 0x8 ) ;
813- return v . toString ( 16 ) ;
814- } ) ;
809+ var crypto = window . crypto || window . msCrypto ;
810+
811+ if ( ! isUndefined ( crypto ) && crypto . getRandomValues ) {
812+ // Use window.crypto API if available
813+ var arr = new Uint16Array ( 8 ) ;
814+ crypto . getRandomValues ( arr ) ;
815+
816+ // set 4 in byte 7
817+ arr [ 3 ] = arr [ 3 ] & 0xFFF | 0x4000 ;
818+ // set 2 most significant bits of byte 9 to '10'
819+ arr [ 4 ] = arr [ 4 ] & 0x3FFF | 0x8000 ;
820+
821+ var pad = function ( num ) {
822+ var v = num . toString ( 16 ) ;
823+ while ( v . length < 4 ) {
824+ v = '0' + v ;
825+ }
826+ return v ;
827+ } ;
828+
829+ return ( pad ( arr [ 0 ] ) + pad ( arr [ 1 ] ) + pad ( arr [ 2 ] ) + pad ( arr [ 3 ] ) + pad ( arr [ 4 ] ) +
830+ pad ( arr [ 5 ] ) + pad ( arr [ 6 ] ) + pad ( arr [ 7 ] ) ) ;
831+ } else {
832+ // http://stackoverflow.com/questions/105034/how-to-create-a-guid-uuid-in-javascript/2117523#2117523
833+ return 'xxxxxxxxxxxx4xxxyxxxxxxxxxxxxxxx' . replace ( / [ x y ] / g, function ( c ) {
834+ var r = Math . random ( ) * 16 | 0 ,
835+ v = c == 'x' ? r : ( r & 0x3 | 0x8 ) ;
836+ return v . toString ( 16 ) ;
837+ } ) ;
838+ }
815839}
816840
817841function logDebug ( level , message ) {
0 commit comments