diff --git a/doc/api/crypto.markdown b/doc/api/crypto.markdown index e3ce72a96da..b17544ea29b 100644 --- a/doc/api/crypto.markdown +++ b/doc/api/crypto.markdown @@ -376,6 +376,10 @@ Asynchronous PBKDF2 applies pseudorandom function HMAC-SHA1 to derive a key of given length from the given password, salt and iterations. The callback gets two arguments `(err, derivedKey)`. +## crypto.pbkdf2Sync(password, salt, iterations, keylen) + +Synchronous PBKDF2 function. Returns derivedKey or throws error. + ## crypto.randomBytes(size, [callback]) Generates cryptographically strong pseudo-random data. Usage: diff --git a/lib/crypto.js b/lib/crypto.js index 50e6af58de1..3f85b6a6b74 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -23,7 +23,6 @@ try { var binding = process.binding('crypto'); var SecureContext = binding.SecureContext; - var PBKDF2 = binding.PBKDF2; var randomBytes = binding.randomBytes; var pseudoRandomBytes = binding.pseudoRandomBytes; var getCiphers = binding.getCiphers; @@ -112,10 +111,17 @@ exports.createCredentials = function(options, context) { } if (options.pfx) { - if (options.passphrase) { - c.context.loadPKCS12(options.pfx, options.passphrase); + var pfx = options.pfx; + var passphrase = options.passphrase; + // legacy + if (typeof pfx === 'string') + pfx = new Buffer(pfx, 'binary'); + if (passphrase && typeof passphrase === 'string') + passphrase = new Buffer(passphrase, 'binary'); + if (passphrase) { + c.context.loadPKCS12(pfx, passphrase); } else { - c.context.loadPKCS12(options.pfx); + c.context.loadPKCS12(pfx); } } @@ -153,8 +159,11 @@ function Hmac(hmac, key) { if (!(this instanceof Hmac)) return new Hmac(hmac, key); this._binding = new binding.Hmac(); + // legacy + if (typeof key === 'string') + key = new Buffer(key, 'binary'); this._binding.init(hmac, key); -}; +} Hmac.prototype.update = Hash.prototype.update; Hmac.prototype.digest = Hash.prototype.digest; @@ -172,9 +181,14 @@ function Cipher(cipher, password) { if (!(this instanceof Cipher)) return new Cipher(cipher, password); this._binding = new binding.Cipher; + + // legacy. + if (typeof password === 'string') + password = new Buffer(password, 'binary'); + this._binding.init(cipher, password); this._decoder = null; -}; +} Cipher.prototype.update = function(data, inputEncoding, outputEncoding) { if (inputEncoding && inputEncoding !== 'buffer') @@ -212,6 +226,11 @@ exports.createCipheriv = exports.Cipheriv = Cipheriv; function Cipheriv(cipher, key, iv) { if (!(this instanceof Cipheriv)) return new Cipheriv(cipher, key, iv); + // legacy + if (typeof key === 'string') + key = new Buffer(key, 'binary'); + if (typeof iv === 'string') + iv = new Buffer(iv, 'binary'); this._binding = new binding.Cipher(); this._binding.initiv(cipher, key, iv); this._decoder = null; @@ -226,10 +245,15 @@ exports.createDecipher = exports.Decipher = Decipher; function Decipher(cipher, password) { if (!(this instanceof Decipher)) return new Decipher(cipher, password); - this._binding = new binding.Decipher + + // legacy. + if (typeof password === 'string') + password = new Buffer(password, 'binary'); + + 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; @@ -241,10 +265,15 @@ exports.createDecipheriv = exports.Decipheriv = Decipheriv; function Decipheriv(cipher, key, iv) { if (!(this instanceof Decipheriv)) return new Decipheriv(cipher, key, iv); + // legacy + if (typeof key === 'string') + key = new Buffer(key, 'binary'); + if (typeof iv === 'string') + iv = new Buffer(iv, 'binary'); this._binding = new binding.Decipher; this._binding.initiv(cipher, key, iv); this._decoder = null; -}; +} Decipheriv.prototype.update = Cipher.prototype.update; Decipheriv.prototype.final = Cipher.prototype.final; @@ -258,11 +287,15 @@ function Sign(algorithm) { return new Sign(algorithm); this._binding = new binding.Sign(); this._binding.init(algorithm); -}; +} Sign.prototype.update = Hash.prototype.update; Sign.prototype.sign = function(key, encoding) { + // legacy. + if (typeof key === 'string') + key = new Buffer(key, 'binary'); + var ret = this._binding.sign(key, 'buffer'); if (encoding && encoding !== 'buffer') ret = ret.toString(encoding); @@ -282,10 +315,15 @@ function Verify(algorithm) { Verify.prototype.update = Hash.prototype.update; Verify.prototype.verify = function(object, signature, sigEncoding) { + // legacy. + if (typeof object === 'string') + object = new Buffer(object, 'binary'); + if (sigEncoding === 'buffer') sigEncoding = null; if (sigEncoding || typeof signature === 'string') signature = new Buffer(signature, sigEncoding); + return this._binding.verify(object, signature, 'buffer'); }; @@ -375,34 +413,45 @@ DiffieHellman.prototype.setPrivateKey = function(key, encoding) { exports.DiffieHellmanGroup = - exports.createDiffieHellmanGroup = - exports.getDiffieHellman = 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; + DiffieHellman.prototype.generateKeys; DiffieHellmanGroup.prototype.computeSecret = - DiffieHellman.prototype.computeSecret; + DiffieHellman.prototype.computeSecret; DiffieHellmanGroup.prototype.getPrime = - DiffieHellman.prototype.getPrime; + DiffieHellman.prototype.getPrime; DiffieHellmanGroup.prototype.getGenerator = - DiffieHellman.prototype.getGenerator; + DiffieHellman.prototype.getGenerator; DiffieHellmanGroup.prototype.getPublicKey = - DiffieHellman.prototype.getPublicKey; + DiffieHellman.prototype.getPublicKey; DiffieHellmanGroup.prototype.getPrivateKey = - DiffieHellman.prototype.getPrivateKey; + DiffieHellman.prototype.getPrivateKey; + +exports.pbkdf2 = function(password, salt, iterations, keylen, callback) { + if (typeof password === 'string') + password = new Buffer(password, 'binary'); + if (typeof salt === 'string') + salt = new Buffer(salt, 'binary'); + return binding.PBKDF2(password, salt, iterations, keylen, callback); +}; + +exports.pbkdf2Sync = function(password, salt, iterations, keylen) { + return exports.pbkdf2(password, salt, iterations, keylen); +}; -exports.pbkdf2 = PBKDF2; exports.randomBytes = randomBytes; exports.pseudoRandomBytes = pseudoRandomBytes; diff --git a/src/node_crypto.cc b/src/node_crypto.cc index f026bb13f68..2012b7df159 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -41,9 +41,9 @@ # define OPENSSL_CONST #endif -#define ASSERT_IS_STRING_OR_BUFFER(val) \ - if (!val->IsString() && !Buffer::HasInstance(val)) { \ - return ThrowException(Exception::TypeError(String::New("Not a string or buffer"))); \ +#define ASSERT_IS_BUFFER(val) \ + if (!Buffer::HasInstance(val)) { \ + return ThrowException(Exception::TypeError(String::New("Not a buffer"))); \ } static const char PUBLIC_KEY_PFX[] = "-----BEGIN PUBLIC KEY-----"; @@ -657,9 +657,9 @@ Handle SecureContext::LoadPKCS12(const Arguments& args) { } if (args.Length() >= 2) { - ASSERT_IS_STRING_OR_BUFFER(args[1]); + ASSERT_IS_BUFFER(args[1]); - int passlen = DecodeBytes(args[1], BINARY); + int passlen = Buffer::Length(args[1]->ToObject()); if (passlen < 0) { BIO_free(in); return ThrowException(Exception::TypeError( @@ -1627,8 +1627,8 @@ Handle Connection::SetSession(const Arguments& args) { return ThrowException(exception); } - ASSERT_IS_STRING_OR_BUFFER(args[0]); - ssize_t slen = DecodeBytes(args[0], BINARY); + ASSERT_IS_BUFFER(args[0]); + ssize_t slen = Buffer::Length(args[0]->ToObject()); if (slen < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -2005,137 +2005,6 @@ Handle Connection::SetSNICallback(const Arguments& args) { } #endif -static void HexEncode(unsigned char *md_value, - int md_len, - char** md_hexdigest, - int* md_hex_len) { - *md_hex_len = (2*(md_len)); - *md_hexdigest = new char[*md_hex_len + 1]; - - char* buff = *md_hexdigest; - const int len = *md_hex_len; - for (int i = 0; i < len; i += 2) { - // nibble nibble - const int index = i / 2; - const char msb = (md_value[index] >> 4) & 0x0f; - const char lsb = md_value[index] & 0x0f; - - buff[i] = (msb < 10) ? msb + '0' : (msb - 10) + 'a'; - buff[i + 1] = (lsb < 10) ? lsb + '0' : (lsb - 10) + 'a'; - } - // null terminator - buff[*md_hex_len] = '\0'; -} - -#define hex2i(c) ((c) <= '9' ? ((c) - '0') : (c) <= 'Z' ? ((c) - 'A' + 10) \ - : ((c) - 'a' + 10)) - -static void HexDecode(unsigned char *input, - int length, - char** buf64, - int* buf64_len) { - *buf64_len = (length/2); - *buf64 = new char[length/2 + 1]; - char *b = *buf64; - for(int i = 0; i < length-1; i+=2) { - b[i/2] = (hex2i(input[i])<<4) | (hex2i(input[i+1])); - } -} - - -void base64(unsigned char *input, int length, char** buf64, int* buf64_len) { - BIO *b64 = BIO_new(BIO_f_base64()); - BIO *bmem = BIO_new(BIO_s_mem()); - b64 = BIO_push(b64, bmem); - BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); - int len = BIO_write(b64, input, length); - assert(len == length); - int r = BIO_flush(b64); - assert(r == 1); - - BUF_MEM *bptr; - BIO_get_mem_ptr(b64, &bptr); - - *buf64_len = bptr->length; - *buf64 = new char[*buf64_len+1]; - memcpy(*buf64, bptr->data, *buf64_len); - char* b = *buf64; - b[*buf64_len] = 0; - - BIO_free_all(b64); -} - - -void unbase64(unsigned char *input, - int length, - char** buffer, - int* buffer_len) { - BIO *b64, *bmem; - *buffer = new char[length]; - memset(*buffer, 0, length); - - b64 = BIO_new(BIO_f_base64()); - BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); - bmem = BIO_new_mem_buf(input, length); - bmem = BIO_push(b64, bmem); - - *buffer_len = BIO_read(bmem, *buffer, length); - BIO_free_all(bmem); -} - - -// LengthWithoutIncompleteUtf8 from V8 d8-posix.cc -// see http://v8.googlecode.com/svn/trunk/src/d8-posix.cc -static int LengthWithoutIncompleteUtf8(char* buffer, int len) { - int answer = len; - // 1-byte encoding. - static const int kUtf8SingleByteMask = 0x80; - static const int kUtf8SingleByteValue = 0x00; - // 2-byte encoding. - static const int kUtf8TwoByteMask = 0xe0; - static const int kUtf8TwoByteValue = 0xc0; - // 3-byte encoding. - static const int kUtf8ThreeByteMask = 0xf0; - static const int kUtf8ThreeByteValue = 0xe0; - // 4-byte encoding. - static const int kUtf8FourByteMask = 0xf8; - static const int kUtf8FourByteValue = 0xf0; - // Subsequent bytes of a multi-byte encoding. - static const int kMultiByteMask = 0xc0; - static const int kMultiByteValue = 0x80; - int multi_byte_bytes_seen = 0; - while (answer > 0) { - int c = buffer[answer - 1]; - // Ends in valid single-byte sequence? - if ((c & kUtf8SingleByteMask) == kUtf8SingleByteValue) return answer; - // Ends in one or more subsequent bytes of a multi-byte value? - if ((c & kMultiByteMask) == kMultiByteValue) { - multi_byte_bytes_seen++; - answer--; - } else { - if ((c & kUtf8TwoByteMask) == kUtf8TwoByteValue) { - if (multi_byte_bytes_seen >= 1) { - return answer + 2; - } - return answer - 1; - } else if ((c & kUtf8ThreeByteMask) == kUtf8ThreeByteValue) { - if (multi_byte_bytes_seen >= 2) { - return answer + 3; - } - return answer - 1; - } else if ((c & kUtf8FourByteMask) == kUtf8FourByteValue) { - if (multi_byte_bytes_seen >= 3) { - return answer + 4; - } - return answer - 1; - } else { - return answer; // Malformed UTF-8. - } - } - } - return 0; -} - class Cipher : public ObjectWrap { public: @@ -2252,8 +2121,6 @@ class Cipher : public ObjectWrap { Cipher *cipher = ObjectWrap::Unwrap(args.This()); - cipher->incomplete_base64 = NULL; - if (args.Length() <= 1 || !args[0]->IsString() || !(args[1]->IsString() || Buffer::HasInstance(args[1]))) @@ -2262,8 +2129,8 @@ class Cipher : public ObjectWrap { "Must give cipher-type, key"))); } - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t key_buf_len = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t key_buf_len = Buffer::Length(args[1]->ToObject()); if (key_buf_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -2293,7 +2160,6 @@ class Cipher : public ObjectWrap { HandleScope scope; - cipher->incomplete_base64 = NULL; if (args.Length() <= 2 || !args[0]->IsString() @@ -2304,16 +2170,16 @@ class Cipher : public ObjectWrap { "Must give cipher-type, key, and iv as argument"))); } - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t key_len = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t key_len = Buffer::Length(args[1]->ToObject()); if (key_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - ASSERT_IS_STRING_OR_BUFFER(args[2]); - ssize_t iv_len = DecodeBytes(args[2], BINARY); + ASSERT_IS_BUFFER(args[2]); + ssize_t iv_len = Buffer::Length(args[2]->ToObject()); if (iv_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -2347,31 +2213,15 @@ class Cipher : public ObjectWrap { HandleScope scope; - ASSERT_IS_STRING_OR_BUFFER(args[0]); - - enum encoding enc = ParseEncoding(args[1]); - ssize_t len = DecodeBytes(args[0], enc); - - if (len < 0) { - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); unsigned char *out=0; int out_len=0, r; - if (Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); - r = cipher->CipherUpdate(buffer_data, buffer_length, &out, &out_len); - } else { - char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], enc); - assert(written == len); - r = cipher->CipherUpdate(buf, len,&out,&out_len); - delete [] buf; - } + r = cipher->CipherUpdate(buffer_data, buffer_length, &out, &out_len); if (!r) { delete [] out; @@ -2380,50 +2230,7 @@ class Cipher : public ObjectWrap { } Local outString; - char* out_hexdigest; - int out_hex_len; - enum encoding out_enc = ParseEncoding(args[2], BINARY); - if (out_enc == HEX) { - // Hex encoding - HexEncode(out, out_len, &out_hexdigest, &out_hex_len); - outString = Encode(out_hexdigest, out_hex_len, BINARY); - delete [] out_hexdigest; - } else if (out_enc == BASE64) { - // Base64 encoding - // Check to see if we need to add in previous base64 overhang - if (cipher->incomplete_base64!=NULL){ - unsigned char* complete_base64 = new unsigned char[out_len+cipher->incomplete_base64_len+1]; - memcpy(complete_base64, cipher->incomplete_base64, cipher->incomplete_base64_len); - memcpy(&complete_base64[cipher->incomplete_base64_len], out, out_len); - delete [] out; - - delete [] cipher->incomplete_base64; - cipher->incomplete_base64=NULL; - - out=complete_base64; - out_len += cipher->incomplete_base64_len; - } - - // Check to see if we need to trim base64 stream - if (out_len%3!=0){ - cipher->incomplete_base64_len = out_len%3; - cipher->incomplete_base64 = new char[cipher->incomplete_base64_len+1]; - memcpy(cipher->incomplete_base64, - &out[out_len-cipher->incomplete_base64_len], - cipher->incomplete_base64_len); - out_len -= cipher->incomplete_base64_len; - out[out_len]=0; - } - - base64(out, out_len, &out_hexdigest, &out_hex_len); - outString = Encode(out_hexdigest, out_hex_len, BINARY); - delete [] out_hexdigest; - } else if (out_enc == BINARY || out_enc == BUFFER) { - outString = Encode(out, out_len, out_enc); - } else { - fprintf(stderr, "node-crypto : Cipher .update encoding " - "can be binary, buffer, hex or base64\n"); - } + outString = Encode(out, out_len, BUFFER); if (out) delete [] out; @@ -2446,8 +2253,6 @@ class Cipher : public ObjectWrap { unsigned char* out_value = NULL; int out_len = -1; - char* out_hexdigest; - int out_hex_len; Local outString ; int r = cipher->CipherFinal(&out_value, &out_len); @@ -2466,35 +2271,7 @@ class Cipher : public ObjectWrap { } } - enum encoding enc = ParseEncoding(args[0], BINARY); - if (enc == HEX) { - // Hex encoding - HexEncode(out_value, out_len, &out_hexdigest, &out_hex_len); - outString = Encode(out_hexdigest, out_hex_len, BINARY); - delete [] out_hexdigest; - } else if (enc == BASE64) { - // Check to see if we need to add in previous base64 overhang - if (cipher->incomplete_base64!=NULL){ - unsigned char* complete_base64 = new unsigned char[out_len+cipher->incomplete_base64_len+1]; - memcpy(complete_base64, cipher->incomplete_base64, cipher->incomplete_base64_len); - memcpy(&complete_base64[cipher->incomplete_base64_len], out_value, out_len); - delete [] out_value; - - delete [] cipher->incomplete_base64; - cipher->incomplete_base64=NULL; - - out_value=complete_base64; - out_len += cipher->incomplete_base64_len; - } - base64(out_value, out_len, &out_hexdigest, &out_hex_len); - outString = Encode(out_hexdigest, out_hex_len, BINARY); - delete [] out_hexdigest; - } else if (enc == BINARY || enc == BUFFER) { - outString = Encode(out_value, out_len, enc); - } else { - fprintf(stderr, "node-crypto : Cipher .final encoding " - "can be binary, buffer, hex or base64\n"); - } + outString = Encode(out_value, out_len, BUFFER); delete [] out_value; return scope.Close(outString); @@ -2516,9 +2293,6 @@ class Cipher : public ObjectWrap { EVP_CIPHER_CTX ctx; /* coverity[member_decl] */ const EVP_CIPHER *cipher; /* coverity[member_decl] */ bool initialised_; - char* incomplete_base64; /* coverity[member_decl] */ - int incomplete_base64_len; /* coverity[member_decl] */ - }; @@ -2660,9 +2434,6 @@ class Decipher : public ObjectWrap { HandleScope scope; - cipher->incomplete_utf8 = NULL; - cipher->incomplete_hex_flag = false; - if (args.Length() <= 1 || !args[0]->IsString() || !(args[1]->IsString() || Buffer::HasInstance(args[1]))) @@ -2671,8 +2442,8 @@ class Decipher : public ObjectWrap { "Must give cipher-type, key as argument"))); } - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t key_len = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t key_len = Buffer::Length(args[1]->ToObject()); if (key_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -2701,9 +2472,6 @@ class Decipher : public ObjectWrap { HandleScope scope; - cipher->incomplete_utf8 = NULL; - cipher->incomplete_hex_flag = false; - if (args.Length() <= 2 || !args[0]->IsString() || !(args[1]->IsString() || Buffer::HasInstance(args[1])) @@ -2713,16 +2481,16 @@ class Decipher : public ObjectWrap { "Must give cipher-type, key, and iv as argument"))); } - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t key_len = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t key_len = Buffer::Length(args[1]->ToObject()); if (key_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); return ThrowException(exception); } - ASSERT_IS_STRING_OR_BUFFER(args[2]); - ssize_t iv_len = DecodeBytes(args[2], BINARY); + ASSERT_IS_BUFFER(args[2]); + ssize_t iv_len = Buffer::Length(args[2]->ToObject()); if (iv_len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -2756,79 +2524,19 @@ class Decipher : public ObjectWrap { Decipher *cipher = ObjectWrap::Unwrap(args.This()); - ASSERT_IS_STRING_OR_BUFFER(args[0]); + ASSERT_IS_BUFFER(args[0]); - ssize_t len = DecodeBytes(args[0], BINARY); - if (len < 0) { - return ThrowException(Exception::Error(String::New( - "node`DecodeBytes() failed"))); - } + ssize_t len; char* buf; // if alloc_buf then buf must be deleted later bool alloc_buf = false; - if (Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); - buf = buffer_data; - len = buffer_length; - } else { - alloc_buf = true; - buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], BINARY); - assert(written == len); - } - - char* ciphertext; - int ciphertext_len; - - enum encoding enc = ParseEncoding(args[1], BINARY); - if (enc == HEX) { - // Hex encoding - // Do we have a previous hex carry over? - if (cipher->incomplete_hex_flag) { - char* complete_hex = new char[len+2]; - memcpy(complete_hex, &cipher->incomplete_hex, 1); - memcpy(complete_hex+1, buf, len); - if (alloc_buf) delete [] buf; - alloc_buf = true; - buf = complete_hex; - len += 1; - } - // Do we have an incomplete hex stream? - if ((len>0) && (len % 2 !=0)) { - len--; - cipher->incomplete_hex=buf[len]; - cipher->incomplete_hex_flag=true; - buf[len]=0; - } - HexDecode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len); - - if (alloc_buf) { - delete [] buf; - } - buf = ciphertext; - len = ciphertext_len; - alloc_buf = true; - - } else if (enc == BASE64) { - unbase64((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len); - if (alloc_buf) { - delete [] buf; - } - buf = ciphertext; - len = ciphertext_len; - alloc_buf = true; - - } else if (enc == BINARY || enc == BUFFER) { - // Binary - do nothing - - } else { - fprintf(stderr, "node-crypto : Decipher .update encoding " - "can be binary, buffer, hex or base64\n"); - } + buf = buffer_data; + len = buffer_length; unsigned char *out=0; int out_len=0; @@ -2841,32 +2549,7 @@ class Decipher : public ObjectWrap { } Local outString; - enum encoding out_enc = ParseEncoding(args[2], BINARY); - if (out_enc == UTF8) { - // See if we have any overhang from last utf8 partial ending - if (cipher->incomplete_utf8!=NULL) { - char* complete_out = new char[cipher->incomplete_utf8_len + out_len]; - memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); - memcpy((char *)complete_out+cipher->incomplete_utf8_len, out, out_len); - delete [] out; - - delete [] cipher->incomplete_utf8; - cipher->incomplete_utf8 = NULL; - - out = (unsigned char*)complete_out; - out_len += cipher->incomplete_utf8_len; - } - // Check to see if we have a complete utf8 stream - int utf8_len = LengthWithoutIncompleteUtf8((char *)out, out_len); - if (utf8_lenincomplete_utf8_len = out_len-utf8_len; - cipher->incomplete_utf8 = new unsigned char[cipher->incomplete_utf8_len+1]; - memcpy(cipher->incomplete_utf8, &out[utf8_len], cipher->incomplete_utf8_len); - } - outString = Encode(out, utf8_len, out_enc); - } else { - outString = Encode(out, out_len, out_enc); - } + outString = Encode(out, out_len, BUFFER); if (out) delete [] out; @@ -2908,29 +2591,7 @@ class Decipher : public ObjectWrap { } } - if (args.Length() == 0 || !args[0]->IsString()) { - outString = Encode(out_value, out_len, BINARY); - } else { - enum encoding enc = ParseEncoding(args[0], BINARY); - if (enc == UTF8) { - // See if we have any overhang from last utf8 partial ending - if (cipher->incomplete_utf8!=NULL) { - char* complete_out = new char[cipher->incomplete_utf8_len + out_len]; - memcpy(complete_out, cipher->incomplete_utf8, cipher->incomplete_utf8_len); - memcpy((char *)complete_out+cipher->incomplete_utf8_len, out_value, out_len); - - delete [] cipher->incomplete_utf8; - cipher->incomplete_utf8=NULL; - - outString = Encode(complete_out, cipher->incomplete_utf8_len+out_len, enc); - delete [] complete_out; - } else { - outString = Encode(out_value, out_len, enc); - } - } else { - outString = Encode(out_value, out_len, enc); - } - } + outString = Encode(out_value, out_len, BUFFER); delete [] out_value; return scope.Close(outString); } @@ -2950,10 +2611,6 @@ class Decipher : public ObjectWrap { EVP_CIPHER_CTX ctx; const EVP_CIPHER *cipher_; bool initialised_; - unsigned char* incomplete_utf8; - int incomplete_utf8_len; - char incomplete_hex; - bool incomplete_hex_flag; }; @@ -3024,8 +2681,8 @@ class Hmac : public ObjectWrap { "Must give hashtype string as argument"))); } - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t len = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t len = Buffer::Length(args[1]->ToObject()); if (len < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -3064,30 +2721,15 @@ class Hmac : public ObjectWrap { HandleScope scope; - ASSERT_IS_STRING_OR_BUFFER(args[0]); - enum encoding enc = ParseEncoding(args[1]); - ssize_t len = DecodeBytes(args[0], enc); - - if (len < 0) { - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); int r; - if( Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); - r = hmac->HmacUpdate(buffer_data, buffer_length); - } else { - char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], enc); - assert(written == len); - r = hmac->HmacUpdate(buf, len); - delete [] buf; - } + r = hmac->HmacUpdate(buffer_data, buffer_length); if (!r) { Local exception = Exception::TypeError(String::New("HmacUpdate fail")); @@ -3104,8 +2746,6 @@ class Hmac : public ObjectWrap { unsigned char* md_value = NULL; unsigned int md_len = 0; - char* md_hexdigest; - int md_hex_len; Local outString; int r = hmac->HmacDigest(&md_value, &md_len); @@ -3114,22 +2754,8 @@ class Hmac : public ObjectWrap { md_len = 0; } - enum encoding enc = ParseEncoding(args[0], BINARY); - if (enc == HEX) { - // Hex encoding - HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BASE64) { - base64(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BINARY || enc == BUFFER) { - outString = Encode(md_value, md_len, enc); - } else { - fprintf(stderr, "node-crypto : Hmac .digest encoding " - "can be binary, buffer, hex or base64\n"); - } + outString = Encode(md_value, md_len, BUFFER); + delete [] md_value; return scope.Close(outString); } @@ -3211,29 +2837,14 @@ class Hash : public ObjectWrap { Hash *hash = ObjectWrap::Unwrap(args.This()); - ASSERT_IS_STRING_OR_BUFFER(args[0]); - enum encoding enc = ParseEncoding(args[1]); - ssize_t len = DecodeBytes(args[0], enc); - - if (len < 0) { - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); int r; - if (Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); - r = hash->HashUpdate(buffer_data, buffer_length); - } else { - char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], enc); - assert(written == len); - r = hash->HashUpdate(buf, len); - delete[] buf; - } + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); + r = hash->HashUpdate(buffer_data, buffer_length); if (!r) { Local exception = Exception::TypeError(String::New("HashUpdate fail")); @@ -3261,26 +2872,7 @@ class Hash : public ObjectWrap { Local outString; - enum encoding enc = ParseEncoding(args[0], BINARY); - if (enc == HEX) { - // Hex encoding - char* md_hexdigest; - int md_hex_len; - HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BASE64) { - char* md_hexdigest; - int md_hex_len; - base64(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BINARY || enc == BUFFER) { - outString = Encode(md_value, md_len, enc); - } else { - fprintf(stderr, "node-crypto : Hash .digest encoding " - "can be binary, buffer, hex or base64\n"); - } + outString = Encode(md_value, md_len, BUFFER); return scope.Close(outString); } @@ -3398,30 +2990,15 @@ class Sign : public ObjectWrap { HandleScope scope; - ASSERT_IS_STRING_OR_BUFFER(args[0]); - enum encoding enc = ParseEncoding(args[1]); - ssize_t len = DecodeBytes(args[0], enc); - - if (len < 0) { - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); int r; - if (Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); - r = sign->SignUpdate(buffer_data, buffer_length); - } else { - char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], enc); - assert(written == len); - r = sign->SignUpdate(buf, len); - delete [] buf; - } + r = sign->SignUpdate(buffer_data, buffer_length); if (!r) { Local exception = Exception::TypeError(String::New("SignUpdate fail")); @@ -3438,24 +3015,16 @@ class Sign : public ObjectWrap { unsigned char* md_value; unsigned int md_len; - char* md_hexdigest; - int md_hex_len; Local outString; md_len = 8192; // Maximum key size is 8192 bits md_value = new unsigned char[md_len]; - ASSERT_IS_STRING_OR_BUFFER(args[0]); - ssize_t len = DecodeBytes(args[0], BINARY); - - if (len < 0) { - delete [] md_value; - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); + ssize_t len = Buffer::Length(args[0]->ToObject()); char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], BINARY); + ssize_t written = DecodeWrite(buf, len, args[0], BUFFER); assert(written == len); int r = sign->SignFinal(&md_value, &md_len, buf, len); @@ -3466,23 +3035,7 @@ class Sign : public ObjectWrap { delete [] buf; - enum encoding enc = ParseEncoding(args[1], BINARY); - if (enc == HEX) { - // Hex encoding - HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BASE64) { - base64(md_value, md_len, &md_hexdigest, &md_hex_len); - outString = Encode(md_hexdigest, md_hex_len, BINARY); - delete [] md_hexdigest; - } else if (enc == BINARY || enc == BUFFER) { - outString = Encode(md_value, md_len, enc); - } else { - outString = String::New(""); - fprintf(stderr, "node-crypto : Sign .sign encoding " - "can be binary, buffer, hex or base64\n"); - } + outString = Encode(md_value, md_len, BUFFER); delete [] md_value; return scope.Close(outString); @@ -3649,30 +3202,15 @@ class Verify : public ObjectWrap { Verify *verify = ObjectWrap::Unwrap(args.This()); - ASSERT_IS_STRING_OR_BUFFER(args[0]); - enum encoding enc = ParseEncoding(args[1]); - ssize_t len = DecodeBytes(args[0], enc); - - if (len < 0) { - Local exception = Exception::TypeError(String::New("Bad argument")); - return ThrowException(exception); - } + ASSERT_IS_BUFFER(args[0]); int r; - if(Buffer::HasInstance(args[0])) { - Local buffer_obj = args[0]->ToObject(); - char *buffer_data = Buffer::Data(buffer_obj); - size_t buffer_length = Buffer::Length(buffer_obj); + Local buffer_obj = args[0]->ToObject(); + char *buffer_data = Buffer::Data(buffer_obj); + size_t buffer_length = Buffer::Length(buffer_obj); - r = verify->VerifyUpdate(buffer_data, buffer_length); - } else { - char* buf = new char[len]; - ssize_t written = DecodeWrite(buf, len, args[0], enc); - assert(written == len); - r = verify->VerifyUpdate(buf, len); - delete [] buf; - } + r = verify->VerifyUpdate(buffer_data, buffer_length); if (!r) { Local exception = Exception::TypeError(String::New("VerifyUpdate fail")); @@ -3688,8 +3226,8 @@ class Verify : public ObjectWrap { Verify *verify = ObjectWrap::Unwrap(args.This()); - ASSERT_IS_STRING_OR_BUFFER(args[0]); - ssize_t klen = DecodeBytes(args[0], BINARY); + ASSERT_IS_BUFFER(args[0]); + ssize_t klen = Buffer::Length(args[0]->ToObject()); if (klen < 0) { Local exception = Exception::TypeError(String::New("Bad argument")); @@ -3700,8 +3238,8 @@ class Verify : public ObjectWrap { ssize_t kwritten = DecodeWrite(kbuf, klen, args[0], BINARY); assert(kwritten == klen); - ASSERT_IS_STRING_OR_BUFFER(args[1]); - ssize_t hlen = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + ssize_t hlen = Buffer::Length(args[1]->ToObject()); if (hlen < 0) { delete [] kbuf; @@ -3712,28 +3250,10 @@ class Verify : public ObjectWrap { unsigned char* hbuf = new unsigned char[hlen]; ssize_t hwritten = DecodeWrite((char *)hbuf, hlen, args[1], BINARY); assert(hwritten == hlen); - unsigned char* dbuf; - int dlen; int r=-1; - enum encoding enc = ParseEncoding(args[2], BINARY); - if (enc == HEX) { - // Hex encoding - HexDecode(hbuf, hlen, (char **)&dbuf, &dlen); - r = verify->VerifyFinal(kbuf, klen, dbuf, dlen); - delete [] dbuf; - } else if (enc == BASE64) { - // Base64 encoding - unbase64(hbuf, hlen, (char **)&dbuf, &dlen); - r = verify->VerifyFinal(kbuf, klen, dbuf, dlen); - delete [] dbuf; - } else if (enc == BINARY || enc == BUFFER) { - r = verify->VerifyFinal(kbuf, klen, hbuf, hlen); - } else { - fprintf(stderr, "node-crypto : Verify .verify encoding " - "can be binary, buffer, hex or base64\n"); - } + r = verify->VerifyFinal(kbuf, klen, hbuf, hlen); delete [] kbuf; delete [] hbuf; @@ -3864,30 +3384,10 @@ class DiffieHellman : public ObjectWrap { if (args[0]->IsInt32()) { initialized = diffieHellman->Init(args[0]->Int32Value()); } else { - if (args[0]->IsString()) { - char* buf; - int len; - if (args.Length() > 1 && args[1]->IsString()) { - len = DecodeWithEncoding(args[0], args[1], &buf); - } else { - len = DecodeBinary(args[0], &buf); - } - - if (len == -1) { - delete[] buf; - return ThrowException(Exception::Error( - String::New("Invalid argument"))); - } else { - initialized = diffieHellman->Init( - reinterpret_cast(buf), len); - delete[] buf; - } - } else if (Buffer::HasInstance(args[0])) { - Local buffer = args[0]->ToObject(); - initialized = diffieHellman->Init( - reinterpret_cast(Buffer::Data(buffer)), - Buffer::Length(buffer)); - } + Local buffer = args[0]->ToObject(); + initialized = diffieHellman->Init( + reinterpret_cast(Buffer::Data(buffer)), + Buffer::Length(buffer)); } } @@ -3924,11 +3424,7 @@ class DiffieHellman : public ObjectWrap { BN_bn2bin(diffieHellman->dh->pub_key, reinterpret_cast(data)); - if (args.Length() > 0 && args[0]->IsString()) { - outString = EncodeWithEncoding(args[0], data, dataSize); - } else { - outString = Encode(data, dataSize, BINARY); - } + outString = Encode(data, dataSize, BUFFER); delete[] data; return scope.Close(outString); @@ -3950,11 +3446,7 @@ class DiffieHellman : public ObjectWrap { Local outString; - if (args.Length() > 0 && args[0]->IsString()) { - outString = EncodeWithEncoding(args[0], data, dataSize); - } else { - outString = Encode(data, dataSize, BINARY); - } + outString = Encode(data, dataSize, BUFFER); delete[] data; @@ -3977,11 +3469,7 @@ class DiffieHellman : public ObjectWrap { Local outString; - if (args.Length() > 0 && args[0]->IsString()) { - outString = EncodeWithEncoding(args[0], data, dataSize); - } else { - outString = Encode(data, dataSize, BINARY); - } + outString = Encode(data, dataSize, BUFFER); delete[] data; @@ -4010,11 +3498,7 @@ class DiffieHellman : public ObjectWrap { Local outString; - if (args.Length() > 0 && args[0]->IsString()) { - outString = EncodeWithEncoding(args[0], data, dataSize); - } else { - outString = Encode(data, dataSize, BINARY); - } + outString = Encode(data, dataSize, BUFFER); delete[] data; @@ -4043,11 +3527,7 @@ class DiffieHellman : public ObjectWrap { Local outString; - if (args.Length() > 0 && args[0]->IsString()) { - outString = EncodeWithEncoding(args[0], data, dataSize); - } else { - outString = Encode(data, dataSize, BINARY); - } + outString = Encode(data, dataSize, BUFFER); delete[] data; @@ -4070,30 +3550,11 @@ class DiffieHellman : public ObjectWrap { return ThrowException(Exception::Error( String::New("First argument must be other party's public key"))); } else { - if (args[0]->IsString()) { - char* buf; - int len; - if (args.Length() > 1) { - len = DecodeWithEncoding(args[0], args[1], &buf); - } else { - len = DecodeBinary(args[0], &buf); - } - if (len == -1) { - delete[] buf; - return ThrowException(Exception::Error( - String::New("Invalid argument"))); - } - key = BN_bin2bn(reinterpret_cast(buf), len, 0); - delete[] buf; - } else if (Buffer::HasInstance(args[0])) { - Local buffer = args[0]->ToObject(); - key = BN_bin2bn( - reinterpret_cast(Buffer::Data(buffer)), - Buffer::Length(buffer), 0); - } else { - return ThrowException(Exception::Error( - String::New("First argument must be other party's public key"))); - } + ASSERT_IS_BUFFER(args[0]); + Local buffer = args[0]->ToObject(); + key = BN_bin2bn( + reinterpret_cast(Buffer::Data(buffer)), + Buffer::Length(buffer), 0); } int dataSize = DH_size(diffieHellman->dh); @@ -4146,7 +3607,7 @@ class DiffieHellman : public ObjectWrap { } else if (args.Length() > 1 && args[1]->IsString()) { outString = EncodeWithEncoding(args[1], data, dataSize); } else { - outString = Encode(data, dataSize, BINARY); + outString = Encode(data, dataSize, BUFFER); } delete[] data; @@ -4167,32 +3628,12 @@ class DiffieHellman : public ObjectWrap { return ThrowException(Exception::Error( String::New("First argument must be public key"))); } else { - if (args[0]->IsString()) { - char* buf; - int len; - if (args.Length() > 1) { - len = DecodeWithEncoding(args[0], args[1], &buf); - } else { - len = DecodeBinary(args[0], &buf); - } - if (len == -1) { - delete[] buf; - return ThrowException(Exception::Error( - String::New("Invalid argument"))); - } - diffieHellman->dh->pub_key = - BN_bin2bn(reinterpret_cast(buf), len, 0); - delete[] buf; - } else if (Buffer::HasInstance(args[0])) { - Local buffer = args[0]->ToObject(); - diffieHellman->dh->pub_key = - BN_bin2bn( - reinterpret_cast(Buffer::Data(buffer)), - Buffer::Length(buffer), 0); - } else { - return ThrowException(Exception::Error( - String::New("First argument must be public key"))); - } + ASSERT_IS_BUFFER(args[0]); + Local buffer = args[0]->ToObject(); + diffieHellman->dh->pub_key = + BN_bin2bn( + reinterpret_cast(Buffer::Data(buffer)), + Buffer::Length(buffer), 0); } return args.This(); @@ -4213,32 +3654,12 @@ class DiffieHellman : public ObjectWrap { return ThrowException(Exception::Error( String::New("First argument must be private key"))); } else { - if (args[0]->IsString()) { - char* buf; - int len; - if (args.Length() > 1) { - len = DecodeWithEncoding(args[0], args[1], &buf); - } else { - len = DecodeBinary(args[0], &buf); - } - if (len == -1) { - delete[] buf; - return ThrowException(Exception::Error( - String::New("Invalid argument"))); - } - diffieHellman->dh->priv_key = - BN_bin2bn(reinterpret_cast(buf), len, 0); - delete[] buf; - } else if (Buffer::HasInstance(args[0])) { - Local buffer = args[0]->ToObject(); - diffieHellman->dh->priv_key = - BN_bin2bn( - reinterpret_cast(Buffer::Data(buffer)), - Buffer::Length(buffer), 0); - } else { - return ThrowException(Exception::Error( - String::New("First argument must be private key"))); - } + ASSERT_IS_BUFFER(args[0]); + Local buffer = args[0]->ToObject(); + diffieHellman->dh->priv_key = + BN_bin2bn( + reinterpret_cast(Buffer::Data(buffer)), + Buffer::Length(buffer), 0); } return args.This(); @@ -4266,76 +3687,6 @@ class DiffieHellman : public ObjectWrap { return true; } - static int DecodeBinary(Handle str, char** buf) { - int len = DecodeBytes(str); - *buf = new char[len]; - int written = DecodeWrite(*buf, len, str, BINARY); - if (written != len) { - return -1; - } - return len; - } - - static int DecodeWithEncoding(Handle str, Handle encoding_v, - char** buf) { - int len = DecodeBinary(str, buf); - if (len == -1) { - return len; - } - enum encoding enc = ParseEncoding(encoding_v, (enum encoding) -1); - char* retbuf = 0; - int retlen; - - if (enc == HEX) { - HexDecode((unsigned char*)*buf, len, &retbuf, &retlen); - - } else if (enc == BASE64) { - unbase64((unsigned char*)*buf, len, &retbuf, &retlen); - - } else if (enc == BINARY) { - // Binary - do nothing - } else { - fprintf(stderr, "node-crypto : Diffie-Hellman parameter encoding " - "can be binary, buffer, hex or base64\n"); - } - - if (retbuf != 0) { - delete [] *buf; - *buf = retbuf; - len = retlen; - } - - return len; - } - - static Local EncodeWithEncoding(Handle encoding_v, char* buf, - int len) { - HandleScope scope; - - Local outString; - enum encoding enc = ParseEncoding(encoding_v, (enum encoding) -1); - char* retbuf; - int retlen; - - if (enc == HEX) { - // Hex encoding - HexEncode(reinterpret_cast(buf), len, &retbuf, &retlen); - outString = Encode(retbuf, retlen, BINARY); - delete [] retbuf; - } else if (enc == BASE64) { - base64(reinterpret_cast(buf), len, &retbuf, &retlen); - outString = Encode(retbuf, retlen, BINARY); - delete [] retbuf; - } else if (enc == BINARY || enc == BUFFER) { - outString = Encode(buf, len, enc); - } else { - fprintf(stderr, "node-crypto : Diffie-Hellman parameter encoding " - "can be binary, buffer, hex or base64\n"); - } - - return scope.Close(outString); - } - bool initialised_; DH* dh; }; @@ -4378,7 +3729,7 @@ void EIO_PBKDF2(uv_work_t* work_req) { void EIO_PBKDF2After(pbkdf2_req* req, Local argv[2]) { if (req->err) { argv[0] = Local::New(Undefined()); - argv[1] = Encode(req->key, req->keylen, BINARY); + argv[1] = Encode(req->key, req->keylen, BUFFER); memset(req->key, 0, req->keylen); } else { argv[0] = Exception::Error(String::New("PBKDF2 error")); @@ -4423,8 +3774,8 @@ Handle PBKDF2(const Arguments& args) { goto err; } - ASSERT_IS_STRING_OR_BUFFER(args[0]); - passlen = DecodeBytes(args[0], BINARY); + ASSERT_IS_BUFFER(args[0]); + passlen = Buffer::Length(args[0]->ToObject()); if (passlen < 0) { type_error = "Bad password"; goto err; @@ -4434,8 +3785,8 @@ Handle PBKDF2(const Arguments& args) { pass_written = DecodeWrite(pass, passlen, args[0], BINARY); assert(pass_written == passlen); - ASSERT_IS_STRING_OR_BUFFER(args[1]); - saltlen = DecodeBytes(args[1], BINARY); + ASSERT_IS_BUFFER(args[1]); + saltlen = Buffer::Length(args[1]->ToObject()); if (saltlen < 0) { type_error = "Bad salt"; goto err; diff --git a/test/simple/test-crypto.js b/test/simple/test-crypto.js index 0b70ece4001..314bf1f293a 100644 --- a/test/simple/test-crypto.js +++ b/test/simple/test-crypto.js @@ -536,7 +536,7 @@ testCipher4(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); // update() should only take buffers / strings assert.throws(function() { crypto.createHash('sha1').update({foo: 'bar'}); -}, /string or buffer/); +}, /buffer/); // Test Diffie-Hellman with two parties sharing a secret, @@ -670,11 +670,11 @@ assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); // Test PBKDF2 with RFC 6070 test vectors (except #4) // function testPBKDF2(password, salt, iterations, keylen, expected) { - var actual = crypto.pbkdf2(password, salt, iterations, keylen); - assert.equal(actual, expected); + var actual = crypto.pbkdf2Sync(password, salt, iterations, keylen); + assert.equal(actual.toString('binary'), expected); crypto.pbkdf2(password, salt, iterations, keylen, function(err, actual) { - assert.equal(actual, expected); + assert.equal(actual.toString('binary'), expected); }); }