File tree Expand file tree Collapse file tree 1 file changed +23
-0
lines changed
Expand file tree Collapse file tree 1 file changed +23
-0
lines changed Original file line number Diff line number Diff line change @@ -60,3 +60,26 @@ export function fromBuffer(buffer: Buffer): string {
6060 hex . slice ( 20 , 32 )
6161 ) ;
6262}
63+
64+ export function v5 ( name : string , namespace : string ) : string {
65+ const namespaceBuffer = Buffer . from ( namespace . replace ( / - / g, "" ) , "hex" ) ;
66+ const nameBuffer = Buffer . from ( name , "utf8" ) ;
67+
68+ const hash = crypto . createHash ( "sha1" ) ;
69+ hash . update ( namespaceBuffer ) ;
70+ hash . update ( nameBuffer ) ;
71+ const digest = hash . digest ( ) ;
72+
73+ // Set version and variant bits for UUID v5
74+ digest [ 6 ] = ( digest [ 6 ] & 0x0f ) | 0x50 ; // Version 5
75+ digest [ 8 ] = ( digest [ 8 ] & 0x3f ) | 0x80 ; // Variant 1
76+
77+ // Format as UUID string
78+ return [
79+ digest . subarray ( 0 , 4 ) . toString ( "hex" ) ,
80+ digest . subarray ( 4 , 6 ) . toString ( "hex" ) ,
81+ digest . subarray ( 6 , 8 ) . toString ( "hex" ) ,
82+ digest . subarray ( 8 , 10 ) . toString ( "hex" ) ,
83+ digest . subarray ( 10 , 16 ) . toString ( "hex" ) ,
84+ ] . join ( "-" ) ;
85+ }
You can’t perform that action at this time.
0 commit comments