2010-05-04 22:49:00 +02:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
try {
|
|
|
|
var binding = process.binding('crypto');
|
|
|
|
var SecureContext = binding.SecureContext;
|
|
|
|
var SecureStream = binding.SecureStream;
|
2010-05-04 00:37:49 +02:00
|
|
|
var Hmac = binding.Hmac;
|
2010-04-12 22:25:16 +02:00
|
|
|
var Hash = binding.Hash;
|
2010-05-04 00:37:49 +02:00
|
|
|
var Cipher = binding.Cipher;
|
|
|
|
var Decipher = binding.Decipher;
|
2010-04-12 22:25:16 +02:00
|
|
|
var Sign = binding.Sign;
|
|
|
|
var Verify = binding.Verify;
|
|
|
|
var crypto = true;
|
|
|
|
} catch (e) {
|
2010-06-30 08:12:46 +02:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
var crypto = false;
|
|
|
|
}
|
2010-05-04 22:49:00 +02:00
|
|
|
|
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
function Credentials (method) {
|
2010-12-01 00:59:59 +01:00
|
|
|
if (!(this instanceof Credentials)) {
|
|
|
|
return new Credentials(method);
|
|
|
|
}
|
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
if (!crypto) {
|
2010-12-01 00:59:59 +01:00
|
|
|
throw new Error('node.js not compiled with openssl crypto support.');
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
this.context = new SecureContext();
|
2010-11-29 08:20:59 +01:00
|
|
|
|
|
|
|
if (method) {
|
|
|
|
this.context.init(method);
|
|
|
|
} else {
|
|
|
|
this.context.init();
|
|
|
|
}
|
|
|
|
|
2010-05-04 22:49:00 +02:00
|
|
|
this.shouldVerify = false;
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
|
|
|
|
2010-12-01 00:59:59 +01:00
|
|
|
exports.Credentials = Credentials;
|
|
|
|
|
|
|
|
|
|
|
|
exports.createCredentials = function (options) {
|
|
|
|
if (!options) options = {};
|
|
|
|
var c = new Credentials(options.method);
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-12-01 00:59:59 +01:00
|
|
|
if (options.key) c.context.setKey(options.key);
|
|
|
|
|
|
|
|
if (options.cert) c.context.setCert(options.cert);
|
|
|
|
|
|
|
|
if (options.ca) {
|
2010-05-04 22:49:00 +02:00
|
|
|
c.shouldVerify = true;
|
2010-12-01 00:59:59 +01:00
|
|
|
if ( (typeof(options.ca) == 'object') && options.ca.length ) {
|
|
|
|
for (var i = 0, len = options.ca.length; i < len; i++) {
|
|
|
|
c.context.addCACert(options.ca[i]);
|
|
|
|
}
|
2010-04-12 22:25:16 +02:00
|
|
|
} else {
|
2010-12-01 00:59:59 +01:00
|
|
|
c.context.addCACert(options.ca);
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-05-04 22:49:00 +02:00
|
|
|
} else {
|
2010-12-01 01:13:05 +01:00
|
|
|
c.context.addRootCerts();
|
2010-04-12 22:25:16 +02:00
|
|
|
}
|
2010-12-01 00:59:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
return c;
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
exports.Hash = Hash;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createHash = function (hash) {
|
2010-06-14 22:10:23 +02:00
|
|
|
return new Hash(hash);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Hmac = Hmac;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createHmac = function (hmac, key) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Hmac).init(hmac, key);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Cipher = Cipher;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createCipher = function (cipher, key) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Cipher).init(cipher, key);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
|
|
|
exports.createCipheriv = function (cipher, key, iv) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Cipher).initiv(cipher, key, iv);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-05-04 00:37:49 +02:00
|
|
|
exports.Decipher = Decipher;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createDecipher = function (cipher, key) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Decipher).init(cipher, key);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
|
|
|
exports.createDecipheriv = function (cipher, key, iv) {
|
2010-05-04 00:37:49 +02:00
|
|
|
return (new Decipher).initiv(cipher, key, iv);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 00:37:49 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-04-12 22:25:16 +02:00
|
|
|
exports.Sign = Sign;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createSign = function (algorithm) {
|
2010-04-12 22:25:16 +02:00
|
|
|
return (new Sign).init(algorithm);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-04-12 22:25:16 +02:00
|
|
|
|
|
|
|
exports.Verify = Verify;
|
2010-11-29 08:20:59 +01:00
|
|
|
exports.createVerify = function (algorithm) {
|
2010-04-12 22:25:16 +02:00
|
|
|
return (new Verify).init(algorithm);
|
2010-10-07 05:05:23 +02:00
|
|
|
};
|
2010-05-04 22:49:00 +02:00
|
|
|
|
2010-11-29 08:20:59 +01:00
|
|
|
|
2010-10-25 23:50:34 +02:00
|
|
|
var securepair = require('securepair');
|
|
|
|
exports.createPair = securepair.createSecurePair;
|