@@ -14,7 +14,8 @@ const {
1414const {
1515 DiffieHellman : _DiffieHellman ,
1616 DiffieHellmanGroup : _DiffieHellmanGroup ,
17- ECDH : _ECDH
17+ ECDH : _ECDH ,
18+ ECDHConvertKey : _ECDHConvertKey
1819} = process . binding ( 'crypto' ) ;
1920const {
2021 POINT_CONVERSION_COMPRESSED ,
@@ -84,11 +85,9 @@ DiffieHellmanGroup.prototype.generateKeys =
8485 dhGenerateKeys ;
8586
8687function dhGenerateKeys ( encoding ) {
87- var keys = this . _handle . generateKeys ( ) ;
88+ const keys = this . _handle . generateKeys ( ) ;
8889 encoding = encoding || getDefaultEncoding ( ) ;
89- if ( encoding && encoding !== 'buffer' )
90- keys = keys . toString ( encoding ) ;
91- return keys ;
90+ return encode ( keys , encoding ) ;
9291}
9392
9493
@@ -100,12 +99,10 @@ function dhComputeSecret(key, inEnc, outEnc) {
10099 const encoding = getDefaultEncoding ( ) ;
101100 inEnc = inEnc || encoding ;
102101 outEnc = outEnc || encoding ;
103- var ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
102+ const ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
104103 if ( typeof ret === 'string' )
105104 throw new ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY ( ) ;
106- if ( outEnc && outEnc !== 'buffer' )
107- ret = ret . toString ( outEnc ) ;
108- return ret ;
105+ return encode ( ret , outEnc ) ;
109106}
110107
111108
@@ -114,11 +111,9 @@ DiffieHellmanGroup.prototype.getPrime =
114111 dhGetPrime ;
115112
116113function dhGetPrime ( encoding ) {
117- var prime = this . _handle . getPrime ( ) ;
114+ const prime = this . _handle . getPrime ( ) ;
118115 encoding = encoding || getDefaultEncoding ( ) ;
119- if ( encoding && encoding !== 'buffer' )
120- prime = prime . toString ( encoding ) ;
121- return prime ;
116+ return encode ( prime , encoding ) ;
122117}
123118
124119
@@ -127,11 +122,9 @@ DiffieHellmanGroup.prototype.getGenerator =
127122 dhGetGenerator ;
128123
129124function dhGetGenerator ( encoding ) {
130- var generator = this . _handle . getGenerator ( ) ;
125+ const generator = this . _handle . getGenerator ( ) ;
131126 encoding = encoding || getDefaultEncoding ( ) ;
132- if ( encoding && encoding !== 'buffer' )
133- generator = generator . toString ( encoding ) ;
134- return generator ;
127+ return encode ( generator , encoding ) ;
135128}
136129
137130
@@ -140,11 +133,9 @@ DiffieHellmanGroup.prototype.getPublicKey =
140133 dhGetPublicKey ;
141134
142135function dhGetPublicKey ( encoding ) {
143- var key = this . _handle . getPublicKey ( ) ;
136+ const key = this . _handle . getPublicKey ( ) ;
144137 encoding = encoding || getDefaultEncoding ( ) ;
145- if ( encoding && encoding !== 'buffer' )
146- key = key . toString ( encoding ) ;
147- return key ;
138+ return encode ( key , encoding ) ;
148139}
149140
150141
@@ -153,11 +144,9 @@ DiffieHellmanGroup.prototype.getPrivateKey =
153144 dhGetPrivateKey ;
154145
155146function dhGetPrivateKey ( encoding ) {
156- var key = this . _handle . getPrivateKey ( ) ;
147+ const key = this . _handle . getPrivateKey ( ) ;
157148 encoding = encoding || getDefaultEncoding ( ) ;
158- if ( encoding && encoding !== 'buffer' )
159- key = key . toString ( encoding ) ;
160- return key ;
149+ return encode ( key , encoding ) ;
161150}
162151
163152
@@ -197,7 +186,40 @@ ECDH.prototype.generateKeys = function generateKeys(encoding, format) {
197186} ;
198187
199188ECDH . prototype . getPublicKey = function getPublicKey ( encoding , format ) {
200- var f ;
189+ const f = getFormat ( format ) ;
190+ const key = this . _handle . getPublicKey ( f ) ;
191+ encoding = encoding || getDefaultEncoding ( ) ;
192+ return encode ( key , encoding ) ;
193+ } ;
194+
195+ ECDH . convertKey = function convertKey ( key , curve , inEnc , outEnc , format ) {
196+ if ( typeof key !== 'string' && ! isArrayBufferView ( key ) ) {
197+ throw new ERR_INVALID_ARG_TYPE (
198+ 'key' ,
199+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ]
200+ ) ;
201+ }
202+
203+ if ( typeof curve !== 'string' ) {
204+ throw new ERR_INVALID_ARG_TYPE ( 'curve' , 'string' ) ;
205+ }
206+
207+ const encoding = getDefaultEncoding ( ) ;
208+ inEnc = inEnc || encoding ;
209+ outEnc = outEnc || encoding ;
210+ const f = getFormat ( format ) ;
211+ const convertedKey = _ECDHConvertKey ( toBuf ( key , inEnc ) , curve , f ) ;
212+ return encode ( convertedKey , outEnc ) ;
213+ } ;
214+
215+ function encode ( buffer , encoding ) {
216+ if ( encoding && encoding !== 'buffer' )
217+ buffer = buffer . toString ( encoding ) ;
218+ return buffer ;
219+ }
220+
221+ function getFormat ( format ) {
222+ let f ;
201223 if ( format ) {
202224 if ( format === 'compressed' )
203225 f = POINT_CONVERSION_COMPRESSED ;
@@ -211,12 +233,8 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
211233 } else {
212234 f = POINT_CONVERSION_UNCOMPRESSED ;
213235 }
214- var key = this . _handle . getPublicKey ( f ) ;
215- encoding = encoding || getDefaultEncoding ( ) ;
216- if ( encoding && encoding !== 'buffer' )
217- key = key . toString ( encoding ) ;
218- return key ;
219- } ;
236+ return f ;
237+ }
220238
221239module . exports = {
222240 DiffieHellman,
0 commit comments