Skip to content
Browse files

Accept buffers as key and iv

  • Loading branch information...
1 parent 0fb4fb4 commit eb668d22b2eff2658f317d31ecc8bc841a565ed6 @thiagoarrais committed Mar 15, 2012
Showing with 32 additions and 9 deletions.
  1. +23 −9 src/node_crypto.cc
  2. +9 −0 test/simple/test-crypto.js
View
32 src/node_crypto.cc
@@ -2037,7 +2037,7 @@ class Cipher : public ObjectWrap {
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(
"Must give cipher-type, key, and iv as argument")));
}
@@ -2058,20 +2058,34 @@ class Cipher : public ObjectWrap {
return ThrowException(exception);
}
- char* key_buf = new char[key_len];
- ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
- assert(key_written == key_len);
+ char* key_buf;
+ if (Buffer::HasInstance(args[1])) {
+ Local<Object> buffer_key = args[1]->ToObject();
+ key_buf = Buffer::Data(buffer_key);
+ key_len = Buffer::Length(buffer_key);
+ } else {
+ key_buf = new char[key_len];
+ ssize_t key_written = DecodeWrite(key_buf, key_len, args[1], BINARY);
+ assert(key_written == key_len);
+ }
- char* iv_buf = new char[iv_len];
- ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
- assert(iv_written == iv_len);
+ char* iv_buf;
+ if (Buffer::HasInstance(args[2])) {
+ Local<Object> buffer_iv = args[2]->ToObject();
+ iv_buf = Buffer::Data(buffer_iv);
+ iv_len = Buffer::Length(buffer_iv);
+ } else {
+ iv_buf = new char[iv_len];
+ ssize_t iv_written = DecodeWrite(iv_buf, iv_len, args[2], BINARY);
+ assert(iv_written == iv_len);
+ }
String::Utf8Value cipherType(args[0]->ToString());
bool r = cipher->CipherInitIv(*cipherType, key_buf,key_len,iv_buf,iv_len);
- delete [] key_buf;
- delete [] iv_buf;
+ if (args[1]->IsString()) delete [] key_buf;
+ if (args[2]->IsString()) delete [] iv_buf;
if (!r) {
return ThrowException(Exception::Error(String::New("CipherInitIv error")));
View
9 test/simple/test-crypto.js
@@ -456,6 +456,15 @@ txt += decipher.final('utf8');
assert.equal(txt, plaintext, 'encryption and decryption with key and iv');
+var encryption_key_buf = new Buffer('303132333435363738396162636430313233343536373839', 'hex'); //0123456789abcd0123456789
+var iv_buf = new Buffer('3132333435363738', 'hex'); //12345678
+
+var cipher = crypto.createCipheriv('des-ede3-cbc', encryption_key_buf, iv_buf);
+var buf_ciph = cipher.update(plaintext, 'utf8', 'hex');
+buf_ciph += cipher.final('hex');
+
+assert.equal(ciph, buf_ciph, 'key and iv as strings and as buffers');
+
// update() should only take buffers / strings
assert.throws(function() {
crypto.createHash('sha1').update({foo: 'bar'});

0 comments on commit eb668d2

Please sign in to comment.
Something went wrong with that request. Please try again.