33const {
44 ArrayBufferIsView,
55 ArrayBufferPrototypeGetByteLength,
6+ ArrayFrom,
67 ArrayPrototypeIncludes,
78 ArrayPrototypePush,
89 BigInt,
@@ -16,6 +17,7 @@ const {
1617 ObjectKeys,
1718 ObjectPrototypeHasOwnProperty,
1819 PromiseWithResolvers,
20+ SafeSet,
1921 StringPrototypeToUpperCase,
2022 Symbol,
2123 TypedArrayPrototypeGetBuffer,
@@ -718,14 +720,40 @@ function getStringOption(options, key) {
718720}
719721
720722function getUsagesUnion ( usageSet , ...usages ) {
721- const newset = [ ] ;
723+ const newset = new SafeSet ( ) ;
722724 for ( let n = 0 ; n < usages . length ; n ++ ) {
723725 if ( usageSet . has ( usages [ n ] ) )
724- ArrayPrototypePush ( newset , usages [ n ] ) ;
726+ newset . add ( usages [ n ] ) ;
725727 }
726728 return newset ;
727729}
728730
731+ const kCanonicalUsageOrder = new SafeSet ( [
732+ 'encrypt' , 'decrypt' ,
733+ 'sign' , 'verify' ,
734+ 'deriveKey' , 'deriveBits' ,
735+ 'wrapKey' , 'unwrapKey' ,
736+ 'encapsulateKey' , 'encapsulateBits' ,
737+ 'decapsulateKey' , 'decapsulateBits' ,
738+ ] ) ;
739+
740+ /**
741+ * Returns the usages from `usageSet` as an array in the canonical order
742+ * defined by {@link kCanonicalUsageOrder}.
743+ * @param {SafeSet<string> } usageSet
744+ * @returns {string[] }
745+ */
746+ function getSortedUsages ( usageSet ) {
747+ if ( usageSet . size <= 1 ) {
748+ return ArrayFrom ( usageSet ) ;
749+ }
750+ const result = [ ] ;
751+ for ( const usage of kCanonicalUsageOrder ) {
752+ if ( usageSet . has ( usage ) ) ArrayPrototypePush ( result , usage ) ;
753+ }
754+ return result ;
755+ }
756+
729757function getBlockSize ( name ) {
730758 switch ( name ) {
731759 case 'SHA-1' :
@@ -842,6 +870,7 @@ module.exports = {
842870 getDigestSizeInBytes,
843871 getStringOption,
844872 getUsagesUnion,
873+ getSortedUsages,
845874 secureHeapUsed,
846875 getCachedHashId,
847876 getHashCache,
0 commit comments