crypto: make cipher/decipher accept buffer args
This commit is contained in:
parent
a55faeac18
commit
900196e135
@ -2083,9 +2083,12 @@ class Cipher : public ObjectWrap {
|
|||||||
|
|
||||||
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
|
Cipher *cipher = ObjectWrap::Unwrap<Cipher>(args.This());
|
||||||
|
|
||||||
cipher->incomplete_base64=NULL;
|
cipher->incomplete_base64 = NULL;
|
||||||
|
|
||||||
if (args.Length() <= 1 || !args[0]->IsString() || !args[1]->IsString()) {
|
if (args.Length() <= 1
|
||||||
|
|| !args[0]->IsString()
|
||||||
|
|| !(args[1]->IsString() || Buffer::HasInstance(args[1])))
|
||||||
|
{
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowException(Exception::Error(String::New(
|
||||||
"Must give cipher-type, key")));
|
"Must give cipher-type, key")));
|
||||||
}
|
}
|
||||||
@ -2121,9 +2124,13 @@ class Cipher : public ObjectWrap {
|
|||||||
|
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
cipher->incomplete_base64=NULL;
|
cipher->incomplete_base64 = NULL;
|
||||||
|
|
||||||
if (args.Length() <= 2 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) {
|
if (args.Length() <= 2
|
||||||
|
|| !args[0]->IsString()
|
||||||
|
|| !(args[1]->IsString() || Buffer::HasInstance(args[1]))
|
||||||
|
|| !(args[2]->IsString() || Buffer::HasInstance(args[2])))
|
||||||
|
{
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowException(Exception::Error(String::New(
|
||||||
"Must give cipher-type, key, and iv as argument")));
|
"Must give cipher-type, key, and iv as argument")));
|
||||||
}
|
}
|
||||||
@ -2495,10 +2502,13 @@ class Decipher : public ObjectWrap {
|
|||||||
|
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
cipher->incomplete_utf8=NULL;
|
cipher->incomplete_utf8 = NULL;
|
||||||
cipher->incomplete_hex_flag=false;
|
cipher->incomplete_hex_flag = false;
|
||||||
|
|
||||||
if (args.Length() <= 1 || !args[0]->IsString() || !args[1]->IsString()) {
|
if (args.Length() <= 1
|
||||||
|
|| !args[0]->IsString()
|
||||||
|
|| !(args[1]->IsString() || Buffer::HasInstance(args[1])))
|
||||||
|
{
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowException(Exception::Error(String::New(
|
||||||
"Must give cipher-type, key as argument")));
|
"Must give cipher-type, key as argument")));
|
||||||
}
|
}
|
||||||
@ -2533,10 +2543,14 @@ class Decipher : public ObjectWrap {
|
|||||||
|
|
||||||
HandleScope scope;
|
HandleScope scope;
|
||||||
|
|
||||||
cipher->incomplete_utf8=NULL;
|
cipher->incomplete_utf8 = NULL;
|
||||||
cipher->incomplete_hex_flag=false;
|
cipher->incomplete_hex_flag = false;
|
||||||
|
|
||||||
if (args.Length() <= 2 || !args[0]->IsString() || !args[1]->IsString() || !args[2]->IsString()) {
|
if (args.Length() <= 2
|
||||||
|
|| !args[0]->IsString()
|
||||||
|
|| !(args[1]->IsString() || Buffer::HasInstance(args[1]))
|
||||||
|
|| !(args[2]->IsString() || Buffer::HasInstance(args[2])))
|
||||||
|
{
|
||||||
return ThrowException(Exception::Error(String::New(
|
return ThrowException(Exception::Error(String::New(
|
||||||
"Must give cipher-type, key, and iv as argument")));
|
"Must give cipher-type, key, and iv as argument")));
|
||||||
}
|
}
|
||||||
|
@ -424,54 +424,77 @@ var verified = crypto.createVerify('RSA-SHA256')
|
|||||||
.verify(certPem, s2); // binary
|
.verify(certPem, s2); // binary
|
||||||
assert.strictEqual(verified, true, 'sign and verify (binary)');
|
assert.strictEqual(verified, true, 'sign and verify (binary)');
|
||||||
|
|
||||||
// Test encryption and decryption
|
|
||||||
var plaintext = 'Keep this a secret? No! Tell everyone about node.js!';
|
|
||||||
var cipher = crypto.createCipher('aes192', 'MySecretKey123');
|
|
||||||
|
|
||||||
// encrypt plaintext which is in utf8 format
|
function testCipher1(key) {
|
||||||
// to a ciphertext which will be in hex
|
// Test encryption and decryption
|
||||||
var ciph = cipher.update(plaintext, 'utf8', 'hex');
|
var plaintext = 'Keep this a secret? No! Tell everyone about node.js!';
|
||||||
// Only use binary or hex, not base64.
|
var cipher = crypto.createCipher('aes192', key);
|
||||||
ciph += cipher.final('hex');
|
|
||||||
|
|
||||||
var decipher = crypto.createDecipher('aes192', 'MySecretKey123');
|
// encrypt plaintext which is in utf8 format
|
||||||
var txt = decipher.update(ciph, 'hex', 'utf8');
|
// to a ciphertext which will be in hex
|
||||||
txt += decipher.final('utf8');
|
var ciph = cipher.update(plaintext, 'utf8', 'hex');
|
||||||
|
// Only use binary or hex, not base64.
|
||||||
|
ciph += cipher.final('hex');
|
||||||
|
|
||||||
assert.equal(txt, plaintext, 'encryption and decryption');
|
var decipher = crypto.createDecipher('aes192', key);
|
||||||
|
var txt = decipher.update(ciph, 'hex', 'utf8');
|
||||||
|
txt += decipher.final('utf8');
|
||||||
|
|
||||||
// encryption and decryption with Base64
|
assert.equal(txt, plaintext, 'encryption and decryption');
|
||||||
// reported in https://github.com/joyent/node/issues/738
|
}
|
||||||
var plaintext =
|
|
||||||
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
|
|
||||||
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJjAfaFg**';
|
|
||||||
var cipher = crypto.createCipher('aes256', '0123456789abcdef');
|
|
||||||
|
|
||||||
// encrypt plaintext which is in utf8 format
|
|
||||||
// to a ciphertext which will be in Base64
|
|
||||||
var ciph = cipher.update(plaintext, 'utf8', 'base64');
|
|
||||||
ciph += cipher.final('base64');
|
|
||||||
|
|
||||||
var decipher = crypto.createDecipher('aes256', '0123456789abcdef');
|
|
||||||
var txt = decipher.update(ciph, 'base64', 'utf8');
|
|
||||||
txt += decipher.final('utf8');
|
|
||||||
|
|
||||||
assert.equal(txt, plaintext, 'encryption and decryption with Base64');
|
|
||||||
|
|
||||||
|
|
||||||
// Test encyrption and decryption with explicit key and iv
|
function testCipher2(key) {
|
||||||
var encryption_key = '0123456789abcd0123456789';
|
// encryption and decryption with Base64
|
||||||
var iv = '12345678';
|
// reported in https://github.com/joyent/node/issues/738
|
||||||
|
var plaintext =
|
||||||
|
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
|
||||||
|
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
|
||||||
|
'jAfaFg**';
|
||||||
|
var cipher = crypto.createCipher('aes256', key);
|
||||||
|
|
||||||
var cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key, iv);
|
// encrypt plaintext which is in utf8 format
|
||||||
var ciph = cipher.update(plaintext, 'utf8', 'hex');
|
// to a ciphertext which will be in Base64
|
||||||
ciph += cipher.final('hex');
|
var ciph = cipher.update(plaintext, 'utf8', 'base64');
|
||||||
|
ciph += cipher.final('base64');
|
||||||
|
|
||||||
var decipher = crypto.createDecipheriv('des-ede3-cbc', encryption_key, iv);
|
var decipher = crypto.createDecipher('aes256', key);
|
||||||
var txt = decipher.update(ciph, 'hex', 'utf8');
|
var txt = decipher.update(ciph, 'base64', 'utf8');
|
||||||
txt += decipher.final('utf8');
|
txt += decipher.final('utf8');
|
||||||
|
|
||||||
|
assert.equal(txt, plaintext, 'encryption and decryption with Base64');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
function testCipher3(key, iv) {
|
||||||
|
// Test encyrption and decryption with explicit key and iv
|
||||||
|
var plaintext =
|
||||||
|
'32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' +
|
||||||
|
'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' +
|
||||||
|
'jAfaFg**';
|
||||||
|
var cipher = crypto.createCipheriv('des-ede3-cbc', key, iv);
|
||||||
|
var ciph = cipher.update(plaintext, 'utf8', 'hex');
|
||||||
|
ciph += cipher.final('hex');
|
||||||
|
|
||||||
|
var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv);
|
||||||
|
var txt = decipher.update(ciph, 'hex', 'utf8');
|
||||||
|
txt += decipher.final('utf8');
|
||||||
|
|
||||||
|
assert.equal(txt, plaintext, 'encryption and decryption with key and iv');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
testCipher1('MySecretKey123');
|
||||||
|
testCipher1(new Buffer('MySecretKey123'));
|
||||||
|
|
||||||
|
testCipher2('0123456789abcdef');
|
||||||
|
testCipher2(new Buffer('0123456789abcdef'));
|
||||||
|
|
||||||
|
testCipher3('0123456789abcd0123456789', '12345678');
|
||||||
|
testCipher3('0123456789abcd0123456789', new Buffer('12345678'));
|
||||||
|
testCipher3(new Buffer('0123456789abcd0123456789'), '12345678');
|
||||||
|
testCipher3(new Buffer('0123456789abcd0123456789'), new Buffer('12345678'));
|
||||||
|
|
||||||
assert.equal(txt, plaintext, 'encryption and decryption with key and iv');
|
|
||||||
|
|
||||||
// update() should only take buffers / strings
|
// update() should only take buffers / strings
|
||||||
assert.throws(function() {
|
assert.throws(function() {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user