@ -23,14 +23,6 @@
try {
var binding = process . binding ( 'crypto' ) ;
var SecureContext = binding . SecureContext ;
var Hmac = binding . Hmac ;
var Hash = binding . Hash ;
var Cipher = binding . Cipher ;
var Decipher = binding . Decipher ;
var Sign = binding . Sign ;
var Verify = binding . Verify ;
var DiffieHellman = binding . DiffieHellman ;
var DiffieHellmanGroup = binding . DiffieHellmanGroup ;
var PBKDF2 = binding . PBKDF2 ;
var randomBytes = binding . randomBytes ;
var pseudoRandomBytes = binding . pseudoRandomBytes ;
@ -42,6 +34,8 @@ try {
var crypto = false ;
}
var assert = require ( 'assert' ) ;
var StringDecoder = require ( 'string_decoder' ) . StringDecoder ;
function Credentials ( secureProtocol , flags , context ) {
if ( ! ( this instanceof Credentials ) ) {
@ -129,65 +123,285 @@ exports.createCredentials = function(options, context) {
} ;
exports . Hash = Hash ;
exports . createHash = function ( hash ) {
return new Hash ( hash ) ;
exports . createHash = exports . Hash = Hash ;
function Hash ( algorithm ) {
if ( ! ( this instanceof Hash ) )
return new Hash ( algorithm ) ;
this . _ binding = new binding . Hash ( algorithm ) ;
}
Hash . prototype . update = function ( data , encoding ) {
if ( encoding === 'buffer' )
encoding = null ;
if ( encoding || typeof data === 'string' )
data = new Buffer ( data , encoding ) ;
this . _ binding . update ( data ) ;
return this ;
} ;
Hash . prototype . digest = function ( outputEncoding ) {
var result = this . _ binding . digest ( 'buffer' ) ;
if ( outputEncoding && outputEncoding !== 'buffer' )
result = result . toString ( outputEncoding ) ;
return result ;
} ;
exports . Hmac = Hmac ;
exports . createHmac = function ( hmac , key ) {
return ( new Hmac ) . init ( hmac , key ) ;
exports . createHmac = exports . Hmac = Hmac ;
function Hmac ( hmac , key ) {
if ( ! ( this instanceof Hmac ) )
return new Hmac ( hmac , key ) ;
this . _ binding = new binding . Hmac ( ) ;
this . _ binding . init ( hmac , key ) ;
} ;
Hmac . prototype . update = Hash . prototype . update ;
Hmac . prototype . digest = Hash . prototype . digest ;
function getDecoder ( decoder , encoding ) {
decoder = decoder || new StringDecoder ( encoding ) ;
assert ( decoder . encoding === encoding , 'Cannot change encoding' ) ;
return decoder ;
}
exports . Cipher = Cipher ;
exports . createCipher = function ( cipher , password ) {
return ( new Cipher ) . init ( cipher , password ) ;
exports . createCipher = exports . Cipher = Cipher ;
function Cipher ( cipher , password ) {
if ( ! ( this instanceof Cipher ) )
return new Cipher ( cipher , password ) ;
this . _ binding = new binding . Cipher ;
this . _ binding . init ( cipher , password ) ;
this . _ decoder = null ;
} ;
Cipher . prototype . update = function ( data , inputEncoding , outputEncoding ) {
if ( inputEncoding && inputEncoding !== 'buffer' )
data = new Buffer ( data , inputEncoding ) ;
var ret = this . _ binding . update ( data , 'buffer' , 'buffer' ) ;
if ( outputEncoding && outputEncoding !== 'buffer' ) {
this . _ decoder = getDecoder ( this . _ decoder , outputEncoding ) ;
ret = this . _ decoder . write ( ret ) ;
}
exports . createCipheriv = function ( cipher , key , iv ) {
return ( new Cipher ) . initiv ( cipher , key , iv ) ;
return ret ;
} ;
Cipher . prototype . final = function ( outputEncoding ) {
var ret = this . _ binding . final ( 'buffer' ) ;
if ( outputEncoding && outputEncoding !== 'buffer' ) {
this . _ decoder = getDecoder ( this . _ decoder , outputEncoding ) ;
ret = this . _ decoder . write ( ret ) ;
}
return ret ;
} ;
exports . Decipher = Decipher ;
exports . createDecipher = function ( cipher , password ) {
return ( new Decipher ) . init ( cipher , password ) ;
Cipher . prototype . setAutoPadding = function ( ap ) {
this . _ binding . setAutoPadding ( ap ) ;
return this ;
} ;
exports . createDecipheriv = function ( cipher , key , iv ) {
return ( new Decipher ) . initiv ( cipher , key , iv ) ;
exports . createCipheriv = exports . Cipheriv = Cipheriv ;
function Cipheriv ( cipher , key , iv ) {
if ( ! ( this instanceof Cipheriv ) )
return new Cipheriv ( cipher , key , iv ) ;
this . _ binding = new binding . Cipher ( ) ;
this . _ binding . initiv ( cipher , key , iv ) ;
this . _ decoder = null ;
}
Cipheriv . prototype . update = Cipher . prototype . update ;
Cipheriv . prototype . final = Cipher . prototype . final ;
Cipheriv . prototype . setAutoPadding = Cipher . prototype . setAutoPadding ;
exports . createDecipher = exports . Decipher = Decipher ;
function Decipher ( cipher , password ) {
if ( ! ( this instanceof Decipher ) )
return new Decipher ( cipher , password ) ;
this . _ binding = new binding . Decipher
this . _ binding . init ( cipher , password ) ;
this . _ decoder = null ;
} ;
Decipher . prototype . update = Cipher . prototype . update ;
Decipher . prototype . final = Cipher . prototype . final ;
Decipher . prototype . finaltol = Cipher . prototype . final ;
Decipher . prototype . setAutoPadding = Cipher . prototype . setAutoPadding ;
exports . Sign = Sign ;
exports . createSign = function ( algorithm ) {
return ( new Sign ) . init ( algorithm ) ;
exports . createDecipheriv = exports . Decipheriv = Decipheriv ;
function Decipheriv ( cipher , key , iv ) {
if ( ! ( this instanceof Decipheriv ) )
return new Decipheriv ( cipher , key , iv ) ;
this . _ binding = new binding . Decipher ;
this . _ binding . initiv ( cipher , key , iv ) ;
this . _ decoder = null ;
} ;
exports . Verify = Verify ;
exports . createVerify = function ( algorithm ) {
return ( new Verify ) . init ( algorithm ) ;
Decipheriv . prototype . update = Cipher . prototype . update ;
Decipheriv . prototype . final = Cipher . prototype . final ;
Decipheriv . prototype . finaltol = Cipher . prototype . final ;
Decipheriv . prototype . setAutoPadding = Cipher . prototype . setAutoPadding ;
exports . createSign = exports . Sign = Sign ;
function Sign ( algorithm ) {
if ( ! ( this instanceof Sign ) )
return new Sign ( algorithm ) ;
this . _ binding = new binding . Sign ( ) ;
this . _ binding . init ( algorithm ) ;
} ;
exports . DiffieHellman = DiffieHellman ;
exports . createDiffieHellman = function ( size_or_key , enc ) {
if ( ! size_or_key ) {
return new DiffieHellman ( ) ;
} else if ( ! enc ) {
return new DiffieHellman ( size_or_key ) ;
} else {
return new DiffieHellman ( size_or_key , enc ) ;
Sign . prototype . update = Hash . prototype . update ;
Sign . prototype . sign = function ( key , encoding ) {
var ret = this . _ binding . sign ( key , 'buffer' ) ;
if ( encoding && encoding !== 'buffer' )
ret = ret . toString ( encoding ) ;
return ret ;
} ;
exports . createVerify = exports . Verify = Verify ;
function Verify ( algorithm ) {
if ( ! ( this instanceof Verify ) )
return new Verify ( algorithm ) ;
this . _ binding = new binding . Verify ;
this . _ binding . init ( algorithm ) ;
}
Verify . prototype . update = Hash . prototype . update ;
Verify . prototype . verify = function ( object , signature , sigEncoding ) {
if ( sigEncoding === 'buffer' )
sigEncoding = null ;
if ( sigEncoding || typeof signature === 'string' )
signature = new Buffer ( signature , sigEncoding ) ;
return this . _ binding . verify ( object , signature , 'buffer' ) ;
} ;
exports . createDiffieHellman = exports . DiffieHellman = DiffieHellman ;
function DiffieHellman ( sizeOrKey , encoding ) {
if ( ! ( this instanceof DiffieHellman ) )
return new DiffieHellman ( sizeOrKey , encoding ) ;
if ( ! sizeOrKey )
this . _ binding = new binding . DiffieHellman ( ) ;
else {
if ( encoding === 'buffer' )
encoding = null ;
if ( encoding || typeof sizeOrKey === 'string' )
sizeOrKey = new Buffer ( sizeOrKey , encoding ) ;
this . _ binding = new binding . DiffieHellman ( sizeOrKey , 'buffer' ) ;
}
}
DiffieHellman . prototype . generateKeys = function ( encoding ) {
var keys = this . _ binding . generateKeys ( 'buffer' ) ;
if ( encoding )
keys = keys . toString ( encoding ) ;
return keys ;
} ;
DiffieHellman . prototype . computeSecret = function ( key , inEnc , outEnc ) {
if ( inEnc === 'buffer' )
inEnc = null ;
if ( outEnc === 'buffer' )
outEnc = null ;
if ( inEnc || typeof key === 'string' )
key = new Buffer ( key , inEnc ) ;
var ret = this . _ binding . computeSecret ( key , 'buffer' , 'buffer' ) ;
if ( outEnc )
ret = ret . toString ( outEnc ) ;
return ret ;
} ;
DiffieHellman . prototype . getPrime = function ( encoding ) {
var prime = this . _ binding . getPrime ( 'buffer' ) ;
if ( encoding && encoding !== 'buffer' )
prime = prime . toString ( encoding ) ;
return prime ;
} ;
DiffieHellman . prototype . getGenerator = function ( encoding ) {
var generator = this . _ binding . getGenerator ( 'buffer' ) ;
if ( encoding && encoding !== 'buffer' )
generator = generator . toString ( encoding ) ;
return generator ;
} ;
DiffieHellman . prototype . getPublicKey = function ( encoding ) {
var key = this . _ binding . getPublicKey ( 'buffer' ) ;
if ( encoding && encoding !== 'buffer' )
key = key . toString ( encoding ) ;
return key ;
} ;
exports . getDiffieHellman = function ( group_name ) {
return new DiffieHellmanGroup ( group_name ) ;
DiffieHellman . prototype . getPrivateKey = function ( encoding ) {
var key = this . _ binding . getPrivateKey ( 'buffer' ) ;
if ( encoding && encoding !== 'buffer' )
key = key . toString ( encoding ) ;
return key ;
} ;
DiffieHellman . prototype . setPublicKey = function ( key , encoding ) {
if ( encoding === 'buffer' )
encoding = null ;
if ( encoding || typeof key === 'string' )
key = new Buffer ( key , encoding ) ;
this . _ binding . setPublicKey ( key , 'buffer' ) ;
return this ;
} ;
DiffieHellman . prototype . setPrivateKey = function ( key , encoding ) {
if ( encoding === 'buffer' )
encoding = null ;
if ( encoding || typeof key === 'string' )
key = new Buffer ( key , encoding ) ;
this . _ binding . setPrivateKey ( key , 'buffer' ) ;
return this ;
} ;
exports . DiffieHellmanGroup =
exports . createDiffieHellmanGroup =
exports . getDiffieHellman = DiffieHellmanGroup ;
function DiffieHellmanGroup ( name ) {
if ( ! ( this instanceof DiffieHellmanGroup ) )
return new DiffieHellmanGroup ( name ) ;
this . _ binding = new binding . DiffieHellmanGroup ( name ) ;
} ;
DiffieHellmanGroup . prototype . generateKeys =
DiffieHellman . prototype . generateKeys ;
DiffieHellmanGroup . prototype . computeSecret =
DiffieHellman . prototype . computeSecret ;
DiffieHellmanGroup . prototype . getPrime =
DiffieHellman . prototype . getPrime ;
DiffieHellmanGroup . prototype . getGenerator =
DiffieHellman . prototype . getGenerator ;
DiffieHellmanGroup . prototype . getPublicKey =
DiffieHellman . prototype . getPublicKey ;
DiffieHellmanGroup . prototype . getPrivateKey =
DiffieHellman . prototype . getPrivateKey ;
exports . pbkdf2 = PBKDF2 ;
exports . randomBytes = randomBytes ;