@@ -16,6 +16,7 @@ const {
1616 ObjectKeys,
1717 ObjectPrototypeHasOwnProperty,
1818 PromiseWithResolvers,
19+ SafeSet,
1920 StringPrototypeToUpperCase,
2021 Symbol,
2122 TypedArrayPrototypeGetBuffer,
@@ -718,14 +719,38 @@ function getStringOption(options, key) {
718719}
719720
720721function getUsagesUnion ( usageSet , ...usages ) {
721- const newset = [ ] ;
722+ const newset = new SafeSet ( ) ;
722723 for ( let n = 0 ; n < usages . length ; n ++ ) {
723724 if ( usageSet . has ( usages [ n ] ) )
724- ArrayPrototypePush ( newset , usages [ n ] ) ;
725+ newset . add ( usages [ n ] ) ;
725726 }
726727 return newset ;
727728}
728729
730+ const kCanonicalUsageOrder = new SafeSet ( [
731+ 'encrypt' , 'decrypt' ,
732+ 'sign' , 'verify' ,
733+ 'deriveKey' , 'deriveBits' ,
734+ 'wrapKey' , 'unwrapKey' ,
735+ 'encapsulateKey' , 'encapsulateBits' ,
736+ 'decapsulateKey' , 'decapsulateBits' ,
737+ ] ) ;
738+
739+ /**
740+ * Returns the usages from `usageSet` as an array in the canonical order
741+ * defined by {@link kCanonicalUsageOrder}.
742+ * @param {SafeSet<string> } usageSet
743+ * @returns {string[] }
744+ */
745+ function getSortedUsages ( usageSet ) {
746+ const result = [ ] ;
747+ for ( const usage of kCanonicalUsageOrder ) {
748+ if ( usageSet . has ( usage ) )
749+ ArrayPrototypePush ( result , usage ) ;
750+ }
751+ return result ;
752+ }
753+
729754function getBlockSize ( name ) {
730755 switch ( name ) {
731756 case 'SHA-1' :
@@ -841,6 +866,7 @@ module.exports = {
841866 getDigestSizeInBytes,
842867 getStringOption,
843868 getUsagesUnion,
869+ getSortedUsages,
844870 secureHeapUsed,
845871 getCachedHashId,
846872 getHashCache,
0 commit comments