22
33const {
44 FunctionPrototypeCall,
5- Promise,
65} = primordials ;
76
87const { Buffer } = require ( 'buffer' ) ;
@@ -18,7 +17,6 @@ const {
1817 validateInt32,
1918 validateInteger,
2019 validateString,
21- validateUint32,
2220} = require ( 'internal/validators' ) ;
2321
2422const { ERR_MISSING_OPTION } = require ( 'internal/errors' ) . codes ;
@@ -32,6 +30,7 @@ const {
3230
3331const {
3432 lazyDOMException,
33+ promisify,
3534} = require ( 'internal/util' ) ;
3635
3736function pbkdf2 ( password , salt , iterations , keylen , digest , callback ) {
@@ -100,6 +99,7 @@ function check(password, salt, iterations, keylen, digest) {
10099 return { password, salt, iterations, keylen, digest } ;
101100}
102101
102+ const pbkdf2Promise = promisify ( pbkdf2 ) ;
103103async function pbkdf2DeriveBits ( algorithm , baseKey , length ) {
104104 const { iterations } = algorithm ;
105105 let { hash } = algorithm ;
@@ -116,27 +116,26 @@ async function pbkdf2DeriveBits(algorithm, baseKey, length) {
116116
117117 const raw = baseKey [ kKeyObject ] . export ( ) ;
118118
119- let byteLength = 64 ; // the default
120- if ( length !== undefined ) {
121- if ( length === 0 )
122- throw lazyDOMException ( 'length cannot be zero' , 'OperationError' ) ;
123- if ( length === null )
124- throw lazyDOMException ( 'length cannot be null' , 'OperationError' ) ;
125- validateUint32 ( length , 'length' ) ;
126- if ( length % 8 ) {
127- throw lazyDOMException (
128- 'length must be a multiple of 8' ,
129- 'OperationError' ) ;
130- }
131- byteLength = length / 8 ;
119+ if ( length === 0 )
120+ throw lazyDOMException ( 'length cannot be zero' , 'OperationError' ) ;
121+ if ( length === null )
122+ throw lazyDOMException ( 'length cannot be null' , 'OperationError' ) ;
123+ if ( length % 8 ) {
124+ throw lazyDOMException (
125+ 'length must be a multiple of 8' ,
126+ 'OperationError' ) ;
127+ }
128+
129+ let result ;
130+ try {
131+ result = await pbkdf2Promise ( raw , salt , iterations , length / 8 , hash ) ;
132+ } catch ( err ) {
133+ throw lazyDOMException (
134+ 'The operation failed for an operation-specific reason' ,
135+ { name : 'OperationError' , cause : err } ) ;
132136 }
133137
134- return new Promise ( ( resolve , reject ) => {
135- pbkdf2 ( raw , salt , iterations , byteLength , hash , ( err , result ) => {
136- if ( err ) return reject ( err ) ;
137- resolve ( result . buffer ) ;
138- } ) ;
139- } ) ;
138+ return result . buffer ;
140139}
141140
142141module . exports = {
0 commit comments