@@ -10,7 +10,8 @@ const {
1010const {
1111 DiffieHellman : _DiffieHellman ,
1212 DiffieHellmanGroup : _DiffieHellmanGroup ,
13- ECDH : _ECDH
13+ ECDH : _ECDH ,
14+ ECDHConvertKey : _ECDHConvertKey
1415} = process . binding ( 'crypto' ) ;
1516const {
1617 POINT_CONVERSION_COMPRESSED ,
@@ -79,11 +80,9 @@ DiffieHellmanGroup.prototype.generateKeys =
7980 dhGenerateKeys ;
8081
8182function dhGenerateKeys ( encoding ) {
82- var keys = this . _handle . generateKeys ( ) ;
83+ const keys = this . _handle . generateKeys ( ) ;
8384 encoding = encoding || getDefaultEncoding ( ) ;
84- if ( encoding && encoding !== 'buffer' )
85- keys = keys . toString ( encoding ) ;
86- return keys ;
85+ return encode ( keys , encoding ) ;
8786}
8887
8988
@@ -95,10 +94,8 @@ function dhComputeSecret(key, inEnc, outEnc) {
9594 const encoding = getDefaultEncoding ( ) ;
9695 inEnc = inEnc || encoding ;
9796 outEnc = outEnc || encoding ;
98- var ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
99- if ( outEnc && outEnc !== 'buffer' )
100- ret = ret . toString ( outEnc ) ;
101- return ret ;
97+ const ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
98+ return encode ( ret , outEnc ) ;
10299}
103100
104101
@@ -107,11 +104,9 @@ DiffieHellmanGroup.prototype.getPrime =
107104 dhGetPrime ;
108105
109106function dhGetPrime ( encoding ) {
110- var prime = this . _handle . getPrime ( ) ;
107+ const prime = this . _handle . getPrime ( ) ;
111108 encoding = encoding || getDefaultEncoding ( ) ;
112- if ( encoding && encoding !== 'buffer' )
113- prime = prime . toString ( encoding ) ;
114- return prime ;
109+ return encode ( prime , encoding ) ;
115110}
116111
117112
@@ -120,11 +115,9 @@ DiffieHellmanGroup.prototype.getGenerator =
120115 dhGetGenerator ;
121116
122117function dhGetGenerator ( encoding ) {
123- var generator = this . _handle . getGenerator ( ) ;
118+ const generator = this . _handle . getGenerator ( ) ;
124119 encoding = encoding || getDefaultEncoding ( ) ;
125- if ( encoding && encoding !== 'buffer' )
126- generator = generator . toString ( encoding ) ;
127- return generator ;
120+ return encode ( generator , encoding ) ;
128121}
129122
130123
@@ -133,11 +126,9 @@ DiffieHellmanGroup.prototype.getPublicKey =
133126 dhGetPublicKey ;
134127
135128function dhGetPublicKey ( encoding ) {
136- var key = this . _handle . getPublicKey ( ) ;
129+ const key = this . _handle . getPublicKey ( ) ;
137130 encoding = encoding || getDefaultEncoding ( ) ;
138- if ( encoding && encoding !== 'buffer' )
139- key = key . toString ( encoding ) ;
140- return key ;
131+ return encode ( key , encoding ) ;
141132}
142133
143134
@@ -146,11 +137,9 @@ DiffieHellmanGroup.prototype.getPrivateKey =
146137 dhGetPrivateKey ;
147138
148139function dhGetPrivateKey ( encoding ) {
149- var key = this . _handle . getPrivateKey ( ) ;
140+ const key = this . _handle . getPrivateKey ( ) ;
150141 encoding = encoding || getDefaultEncoding ( ) ;
151- if ( encoding && encoding !== 'buffer' )
152- key = key . toString ( encoding ) ;
153- return key ;
142+ return encode ( key , encoding ) ;
154143}
155144
156145
@@ -190,7 +179,41 @@ ECDH.prototype.generateKeys = function generateKeys(encoding, format) {
190179} ;
191180
192181ECDH . prototype . getPublicKey = function getPublicKey ( encoding , format ) {
193- var f ;
182+ const f = getFormat ( format ) ;
183+ const key = this . _handle . getPublicKey ( f ) ;
184+ encoding = encoding || getDefaultEncoding ( ) ;
185+ return encode ( key , encoding ) ;
186+ } ;
187+
188+ ECDH . convertKey = function convertKey ( key , curve , inEnc , outEnc , format ) {
189+ if ( typeof key !== 'string' && ! isArrayBufferView ( key ) ) {
190+ throw new errors . TypeError (
191+ 'ERR_INVALID_ARG_TYPE' ,
192+ 'key' ,
193+ [ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ]
194+ ) ;
195+ }
196+
197+ if ( typeof curve !== 'string' ) {
198+ throw new errors . TypeError ( 'ERR_INVALID_ARG_TYPE' , 'curve' , 'string' ) ;
199+ }
200+
201+ const encoding = getDefaultEncoding ( ) ;
202+ inEnc = inEnc || encoding ;
203+ outEnc = outEnc || encoding ;
204+ const f = getFormat ( format ) ;
205+ const convertedKey = _ECDHConvertKey ( toBuf ( key , inEnc ) , curve , f ) ;
206+ return encode ( convertedKey , outEnc ) ;
207+ } ;
208+
209+ function encode ( buffer , encoding ) {
210+ if ( encoding && encoding !== 'buffer' )
211+ buffer = buffer . toString ( encoding ) ;
212+ return buffer ;
213+ }
214+
215+ function getFormat ( format ) {
216+ let f ;
194217 if ( format ) {
195218 if ( format === 'compressed' )
196219 f = POINT_CONVERSION_COMPRESSED ;
@@ -204,12 +227,8 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
204227 } else {
205228 f = POINT_CONVERSION_UNCOMPRESSED ;
206229 }
207- var key = this . _handle . getPublicKey ( f ) ;
208- encoding = encoding || getDefaultEncoding ( ) ;
209- if ( encoding && encoding !== 'buffer' )
210- key = key . toString ( encoding ) ;
211- return key ;
212- } ;
230+ return f ;
231+ }
213232
214233module . exports = {
215234 DiffieHellman,
0 commit comments