@@ -14,7 +14,8 @@ const {
const {
DiffieHellman : _DiffieHellman ,
DiffieHellmanGroup : _DiffieHellmanGroup ,
ECDH : _ECDH
ECDH : _ECDH ,
ECDHConvertKey : _ECDHConvertKey
} = process . binding ( 'crypto' ) ;
const {
POINT_CONVERSION_COMPRESSED ,
@@ -84,11 +85,9 @@ DiffieHellmanGroup.prototype.generateKeys =
dhGenerateKeys ;
function dhGenerateKeys ( encoding ) {
var keys = this . _handle . generateKeys ( ) ;
const keys = this . _handle . generateKeys ( ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
keys = keys . toString ( encoding ) ;
return keys ;
return encode ( keys , encoding ) ;
}
@@ -100,12 +99,10 @@ function dhComputeSecret(key, inEnc, outEnc) {
const encoding = getDefaultEncoding ( ) ;
inEnc = inEnc || encoding ;
outEnc = outEnc || encoding ;
var ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
const ret = this . _handle . computeSecret ( toBuf ( key , inEnc ) ) ;
if ( typeof ret === 'string' )
throw new ERR_CRYPTO_ECDH_INVALID_PUBLIC_KEY ( ) ;
if ( outEnc && outEnc !== 'buffer' )
ret = ret . toString ( outEnc ) ;
return ret ;
return encode ( ret , outEnc ) ;
}
@@ -114,11 +111,9 @@ DiffieHellmanGroup.prototype.getPrime =
dhGetPrime ;
function dhGetPrime ( encoding ) {
var prime = this . _handle . getPrime ( ) ;
const prime = this . _handle . getPrime ( ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
prime = prime . toString ( encoding ) ;
return prime ;
return encode ( prime , encoding ) ;
}
@@ -127,11 +122,9 @@ DiffieHellmanGroup.prototype.getGenerator =
dhGetGenerator ;
function dhGetGenerator ( encoding ) {
var generator = this . _handle . getGenerator ( ) ;
const generator = this . _handle . getGenerator ( ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
generator = generator . toString ( encoding ) ;
return generator ;
return encode ( generator , encoding ) ;
}
@@ -140,11 +133,9 @@ DiffieHellmanGroup.prototype.getPublicKey =
dhGetPublicKey ;
function dhGetPublicKey ( encoding ) {
var key = this . _handle . getPublicKey ( ) ;
const key = this . _handle . getPublicKey ( ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
key = key . toString ( encoding ) ;
return key ;
return encode ( key , encoding ) ;
}
@@ -153,11 +144,9 @@ DiffieHellmanGroup.prototype.getPrivateKey =
dhGetPrivateKey ;
function dhGetPrivateKey ( encoding ) {
var key = this . _handle . getPrivateKey ( ) ;
const key = this . _handle . getPrivateKey ( ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
key = key . toString ( encoding ) ;
return key ;
return encode ( key , encoding ) ;
}
@@ -197,7 +186,40 @@ ECDH.prototype.generateKeys = function generateKeys(encoding, format) {
} ;
ECDH . prototype . getPublicKey = function getPublicKey ( encoding , format ) {
var f ;
const f = getFormat ( format ) ;
const key = this . _handle . getPublicKey ( f ) ;
encoding = encoding || getDefaultEncoding ( ) ;
return encode ( key , encoding ) ;
} ;
ECDH . convertKey = function convertKey ( key , curve , inEnc , outEnc , format ) {
if ( typeof key !== 'string' && !isArrayBufferView ( key ) ) {
throw new ERR_INVALID_ARG_TYPE (
'key' ,
[ 'string' , 'Buffer' , 'TypedArray' , 'DataView' ]
) ;
}
if ( typeof curve !== 'string' ) {
throw new ERR_INVALID_ARG_TYPE ( 'curve' , 'string' ) ;
}
const encoding = getDefaultEncoding ( ) ;
inEnc = inEnc || encoding ;
outEnc = outEnc || encoding ;
const f = getFormat ( format ) ;
const convertedKey = _ECDHConvertKey ( toBuf ( key , inEnc ) , curve , f ) ;
return encode ( convertedKey , outEnc ) ;
} ;
function encode ( buffer , encoding ) {
if ( encoding && encoding !== 'buffer' )
buffer = buffer . toString ( encoding ) ;
return buffer ;
}
function getFormat ( format ) {
let f ;
if ( format ) {
if ( format === 'compressed' )
f = POINT_CONVERSION_COMPRESSED ;
@@ -211,12 +233,8 @@ ECDH.prototype.getPublicKey = function getPublicKey(encoding, format) {
} else {
f = POINT_CONVERSION_UNCOMPRESSED ;
}
var key = this . _handle . getPublicKey ( f ) ;
encoding = encoding || getDefaultEncoding ( ) ;
if ( encoding && encoding !== 'buffer' )
key = key . toString ( encoding ) ;
return key ;
} ;
return f ;
}
module . exports = {
DiffieHellman,