diff --git a/doc/api/buffer.markdown b/doc/api/buffer.markdown index 16cc82150a447c..076137406c0202 100644 --- a/doc/api/buffer.markdown +++ b/doc/api/buffer.markdown @@ -66,10 +66,6 @@ The character encodings currently supported by Node.js include: this encoding will also correctly accept "URL and Filename Safe Alphabet" as specified in [RFC 4648, Section 5]. -* `'binary'` - A way of encoding the buffer into a one-byte (`latin-1`) - encoded string. The string `'latin-1'` is not supported. Instead, pass - `'binary'` to use `'latin-1'` encoding. - * `'hex'` - Encode each byte as two hexadecimal characters. ## Buffers and TypedArray diff --git a/doc/api/crypto.markdown b/doc/api/crypto.markdown index dd31ae7ac3b92b..8837ae210387ca 100644 --- a/doc/api/crypto.markdown +++ b/doc/api/crypto.markdown @@ -145,7 +145,7 @@ console.log(encrypted); ### cipher.final([output_encoding]) Returns any remaining enciphered contents. If `output_encoding` -parameter is one of `'binary'`, `'base64'` or `'hex'`, a string is returned. +parameter is one of `'base64'` or `'hex'`, a string is returned. If an `output_encoding` is not provided, a [`Buffer`][] is returned. Once the `cipher.final()` method has been called, the `Cipher` object can no @@ -183,13 +183,13 @@ The `cipher.setAutoPadding()` method must be called before [`cipher.final()`][]. ### cipher.update(data[, input_encoding][, output_encoding]) Updates the cipher with `data`. If the `input_encoding` argument is given, -it's value must be one of `'utf8'`, `'ascii'`, or `'binary'` and the `data` +it's value must be one of `'utf8'`, or `'ascii'` and the `data` argument is a string using the specified encoding. If the `input_encoding` argument is not given, `data` must be a [`Buffer`][]. If `data` is a [`Buffer`][] then `input_encoding` is ignored. The `output_encoding` specifies the output format of the enciphered -data, and can be `'binary'`, `'base64'` or `'hex'`. If the `output_encoding` +data, and can be `'base64'` or `'hex'`. If the `output_encoding` is specified, a string using the specified encoding is returned. If no `output_encoding` is provided, a [`Buffer`][] is returned. @@ -262,7 +262,7 @@ console.log(decrypted); ### decipher.final([output_encoding]) Returns any remaining deciphered contents. If `output_encoding` -parameter is one of `'binary'`, `'base64'` or `'hex'`, a string is returned. +parameter is one of `'base64'` or `'hex'`, a string is returned. If an `output_encoding` is not provided, a [`Buffer`][] is returned. Once the `decipher.final()` method has been called, the `Decipher` object can @@ -298,13 +298,13 @@ The `decipher.setAutoPadding()` method must be called before ### decipher.update(data[, input_encoding][, output_encoding]) Updates the decipher with `data`. If the `input_encoding` argument is given, -it's value must be one of `'binary'`, `'base64'`, or `'hex'` and the `data` +it's value must be one of `'base64'`, or `'hex'` and the `data` argument is a string using the specified encoding. If the `input_encoding` argument is not given, `data` must be a [`Buffer`][]. If `data` is a [`Buffer`][] then `input_encoding` is ignored. The `output_encoding` specifies the output format of the enciphered -data, and can be `'binary'`, `'ascii'` or `'utf8'`. If the `output_encoding` +data, and can be `'ascii'` or `'utf8'`. If the `output_encoding` is specified, a string using the specified encoding is returned. If no `output_encoding` is provided, a [`Buffer`][] is returned. @@ -346,7 +346,7 @@ Computes the shared secret using `other_public_key` as the other party's public key and returns the computed shared secret. The supplied key is interpreted using the specified `input_encoding`, and secret is encoded using specified `output_encoding`. Encodings can be -`'binary'`, `'hex'`, or `'base64'`. If the `input_encoding` is not +`'hex'`, or `'base64'`. If the `input_encoding` is not provided, `other_public_key` is expected to be a [`Buffer`][]. If `output_encoding` is given a string is returned; otherwise, a @@ -356,45 +356,45 @@ If `output_encoding` is given a string is returned; otherwise, a Generates private and public Diffie-Hellman key values, and returns the public key in the specified `encoding`. This key should be -transferred to the other party. Encoding can be `'binary'`, `'hex'`, +transferred to the other party. Encoding can be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### diffieHellman.getGenerator([encoding]) Returns the Diffie-Hellman generator in the specified `encoding`, which can -be `'binary'`, `'hex'`, or `'base64'`. If `encoding` is provided a string is +be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### diffieHellman.getPrime([encoding]) Returns the Diffie-Hellman prime in the specified `encoding`, which can -be `'binary'`, `'hex'`, or `'base64'`. If `encoding` is provided a string is +be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### diffieHellman.getPrivateKey([encoding]) Returns the Diffie-Hellman private key in the specified `encoding`, -which can be `'binary'`, `'hex'`, or `'base64'`. If `encoding` is provided a +which can be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### diffieHellman.getPublicKey([encoding]) Returns the Diffie-Hellman public key in the specified `encoding`, which -can be `'binary'`, `'hex'`, or `'base64'`. If `encoding` is provided a +can be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### diffieHellman.setPrivateKey(private_key[, encoding]) Sets the Diffie-Hellman private key. If the `encoding` argument is provided -and is either `'binary'`, `'hex'`, or `'base64'`, `private_key` is expected +and is either `'hex'`, or `'base64'`, `private_key` is expected to be a string. If no `encoding` is provided, `private_key` is expected to be a [`Buffer`][]. ### diffieHellman.setPublicKey(public_key[, encoding]) Sets the Diffie-Hellman public key. If the `encoding` argument is provided -and is either `'binary'`, `'hex'` or `'base64'`, `public_key` is expected +and is either `'hex'` or `'base64'`, `public_key` is expected to be a string. If no `encoding` is provided, `public_key` is expected to be a [`Buffer`][]. @@ -445,7 +445,7 @@ Computes the shared secret using `other_public_key` as the other party's public key and returns the computed shared secret. The supplied key is interpreted using specified `input_encoding`, and the returned secret is encoded using the specified `output_encoding`. Encodings can be -`'binary'`, `'hex'`, or `'base64'`. If the `input_encoding` is not +`'hex'`, or `'base64'`. If the `input_encoding` is not provided, `other_public_key` is expected to be a [`Buffer`][]. If `output_encoding` is given a string will be returned; otherwise a @@ -461,14 +461,14 @@ The `format` arguments specifies point encoding and can be `'compressed'`, `'uncompressed'`, or `'hybrid'`. If `format` is not specified, the point will be returned in `'uncompressed'` format. -The `encoding` argument can be `'binary'`, `'hex'`, or `'base64'`. If +The `encoding` argument can be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### ecdh.getPrivateKey([encoding]) Returns the EC Diffie-Hellman private key in the specified `encoding`, -which can be `'binary'`, `'hex'`, or `'base64'`. If `encoding` is provided +which can be `'hex'`, or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned. ### ecdh.getPublicKey([encoding[, format]]) @@ -480,13 +480,13 @@ The `format` argument specifies point encoding and can be `'compressed'`, `'uncompressed'`, or `'hybrid'`. If `format` is not specified the point will be returned in `'uncompressed'` format. -The `encoding` argument can be `'binary'`, `'hex'`, or `'base64'`. If +The `encoding` argument can be `'hex'`, or `'base64'`. If `encoding` is specified, a string is returned; otherwise a [`Buffer`][] is returned. ### ecdh.setPrivateKey(private_key[, encoding]) -Sets the EC Diffie-Hellman private key. The `encoding` can be `'binary'`, +Sets the EC Diffie-Hellman private key. The `encoding` can be `'hex'` or `'base64'`. If `encoding` is provided, `private_key` is expected to be a string; otherwise `private_key` is expected to be a [`Buffer`][]. If `private_key` is not valid for the curve specified when the `ECDH` object was @@ -497,7 +497,7 @@ public point (key) is also generated and set in the ECDH object. Stability: 0 - Deprecated -Sets the EC Diffie-Hellman public key. Key encoding can be `'binary'`, +Sets the EC Diffie-Hellman public key. Key encoding can be `'hex'` or `'base64'`. If `encoding` is provided `public_key` is expected to be a string; otherwise a [`Buffer`][] is expected. @@ -589,7 +589,7 @@ console.log(hash.digest('hex')); ### hash.digest([encoding]) Calculates the digest of all of the data passed to be hashed (using the -[`hash.update()`][] method). The `encoding` can be `'hex'`, `'binary'` or +[`hash.update()`][] method). The `encoding` can be `'hex'` or `'base64'`. If `encoding` is provided a string will be returned; otherwise a [`Buffer`][] is returned. @@ -599,9 +599,9 @@ called. Multiple calls will cause an error to be thrown. ### hash.update(data[, input_encoding]) Updates the hash content with the given `data`, the encoding of which -is given in `input_encoding` and can be `'utf8'`, `'ascii'` or -`'binary'`. If `encoding` is not provided, and the `data` is a string, an -encoding of `'binary'` is enforced. If `data` is a [`Buffer`][] then +is given in `input_encoding` and can be `'utf8'` or `'ascii'`. +If `encoding` is not provided, and the `data` is a string, an +encoding of `'buffer'` is enforced. If `data` is a [`Buffer`][] then `input_encoding` is ignored. This can be called many times with new data as it is streamed. @@ -663,7 +663,7 @@ console.log(hmac.digest('hex')); ### hmac.digest([encoding]) Calculates the HMAC digest of all of the data passed using [`hmac.update()`][]. -The `encoding` can be `'hex'`, `'binary'` or `'base64'`. If `encoding` is +The `encoding` can be `'hex'` or `'base64'`. If `encoding` is provided a string is returned; otherwise a [`Buffer`][] is returned; The `Hmac` object can not be used again after `hmac.digest()` has been @@ -726,7 +726,7 @@ object, it is interpreted as a hash containing two properties: * `key` : {String} - PEM encoded private key * `passphrase` : {String} - passphrase for the private key -The `output_format` can specify one of `'binary'`, `'hex'` or `'base64'`. If +The `output_format` can specify one of `'hex'` or `'base64'`. If `output_format` is provided a string is returned; otherwise a [`Buffer`][] is returned. @@ -791,7 +791,7 @@ Verifies the provided data using the given `object` and `signature`. The `object` argument is a string containing a PEM encoded object, which can be one an RSA public key, a DSA public key, or an X.509 certificate. The `signature` argument is the previously calculated signature for the data, in -the `signature_format` which can be `'binary'`, `'hex'` or `'base64'`. +the `signature_format` which can be `'hex'` or `'base64'`. If a `signature_format` is specified, the `signature` is expected to be a string; otherwise `signature` is expected to be a [`Buffer`][]. @@ -811,7 +811,7 @@ or [buffers][`Buffer`]. The default value is `'buffer'`, which makes methods default to [`Buffer`][] objects. The `crypto.DEFAULT_ENCODING` mechanism is provided for backwards compatibility -with legacy programs that expect `'binary'` to be the default encoding. +with legacy programs that expect `'buffer'` to be the default encoding. New applications should expect the default to be `'buffer'`. This property may become deprecated in a future Node.js release. @@ -831,7 +831,7 @@ recent OpenSSL releases, `openssl list-cipher-algorithms` will display the available cipher algorithms. The `password` is used to derive the cipher key and initialization vector (IV). -The value must be either a `'binary'` encoded string or a [`Buffer`[]. +The value must be a [`Buffer`[]. The implementation of `crypto.createCipher()` derives keys using the OpenSSL function [`EVP_BytesToKey`][] with the digest algorithm set to MD5, one @@ -855,7 +855,7 @@ recent OpenSSL releases, `openssl list-cipher-algorithms` will display the available cipher algorithms. The `key` is the raw key used by the `algorithm` and `iv` is an -[initialization vector][]. Both arguments must be `'binary'` encoded strings or +[initialization vector][]. Both arguments must be a [buffers][`Buffer`]. ### crypto.createCredentials(details) @@ -910,7 +910,7 @@ recent OpenSSL releases, `openssl list-cipher-algorithms` will display the available cipher algorithms. The `key` is the raw key used by the `algorithm` and `iv` is an -[initialization vector][]. Both arguments must be `'binary'` encoded strings or +[initialization vector][]. Both arguments must be a [buffers][`Buffer`]. ### crypto.createDiffieHellman(prime[, prime_encoding][, generator][, generator_encoding]) @@ -921,7 +921,7 @@ optional specific `generator`. The `generator` argument can be a number, string, or [`Buffer`][]. If `generator` is not specified, the value `2` is used. -The `prime_encoding` and `generator_encoding` arguments can be `'binary'`, +The `prime_encoding` and `generator_encoding` arguments can be `'hex'`, or `'base64'`. If `prime_encoding` is specified, `prime` is expected to be a string; otherwise diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index ec5e078412768b..8c80bc24bdd5f3 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -130,7 +130,7 @@ OutgoingMessage.prototype._send = function(data, encoding, callback) { data = this._header + data; } else { this.output.unshift(this._header); - this.outputEncodings.unshift('binary'); + this.outputEncodings.unshift('utf8'); this.outputCallbacks.unshift(null); this.outputSize += this._header.length; if (typeof this._onPendingData === 'function') @@ -452,8 +452,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) { if (this.chunkedEncoding) { if (typeof chunk === 'string' && encoding !== 'hex' && - encoding !== 'base64' && - encoding !== 'binary') { + encoding !== 'base64') { len = Buffer.byteLength(chunk, encoding); chunk = len.toString(16) + CRLF + chunk + CRLF; ret = this._send(chunk, encoding, callback); @@ -468,7 +467,7 @@ OutgoingMessage.prototype.write = function(chunk, encoding, callback) { this.connection.cork(); process.nextTick(connectionCorkNT, this.connection); } - this._send(len.toString(16), 'binary', null); + this._send(len.toString(16), 'utf8', null); this._send(crlf_buf, null, null); this._send(chunk, encoding, null); ret = this._send(crlf_buf, null, callback); @@ -582,10 +581,10 @@ OutgoingMessage.prototype.end = function(data, encoding, callback) { } if (this._hasBody && this.chunkedEncoding) { - ret = this._send('0\r\n' + this._trailer + '\r\n', 'binary', finish); + ret = this._send('0\r\n' + this._trailer + '\r\n', 'utf8', finish); } else { // Force a flush, HACK. - ret = this._send('', 'binary', finish); + ret = this._send('', 'utf8', finish); } if (this.connection && data) diff --git a/lib/_tls_wrap.js b/lib/_tls_wrap.js index cbeb48c072f00c..0fc6c816304961 100644 --- a/lib/_tls_wrap.js +++ b/lib/_tls_wrap.js @@ -602,7 +602,7 @@ TLSSocket.prototype.setServername = function(name) { TLSSocket.prototype.setSession = function(session) { if (typeof session === 'string') - session = new Buffer(session, 'binary'); + session = new Buffer(session, 'utf8'); this._handle.setSession(session); }; diff --git a/lib/buffer.js b/lib/buffer.js index 766a82c2f21f0e..74f42aa5af1e9f 100644 --- a/lib/buffer.js +++ b/lib/buffer.js @@ -192,7 +192,6 @@ Buffer.isEncoding = function(encoding) { case 'utf8': case 'utf-8': case 'ascii': - case 'binary': case 'base64': case 'ucs2': case 'ucs-2': @@ -267,7 +266,6 @@ function byteLength(string, encoding) { for (;;) { switch (encoding) { case 'ascii': - case 'binary': return len; case 'utf8': @@ -368,9 +366,6 @@ function slowToString(encoding, start, end) { case 'ascii': return this.asciiSlice(start, end); - case 'binary': - return this.binarySlice(start, end); - case 'base64': return this.base64Slice(start, end); @@ -447,7 +442,6 @@ function slowIndexOf(buffer, val, byteOffset, encoding) { case 'ucs-2': case 'utf16le': case 'utf-16le': - case 'binary': return binding.indexOfString(buffer, val, byteOffset, encoding); case 'base64': @@ -600,9 +594,6 @@ Buffer.prototype.write = function(string, offset, length, encoding) { case 'ascii': return this.asciiWrite(string, offset, length); - case 'binary': - return this.binaryWrite(string, offset, length); - case 'base64': // Warning: maxLength not taken into account in base64Write return this.base64Write(string, offset, length); diff --git a/lib/crypto.js b/lib/crypto.js index cf92eff1eec0bc..3a2079846b35d5 100644 --- a/lib/crypto.js +++ b/lib/crypto.js @@ -1,6 +1,3 @@ -// Note: In 0.8 and before, crypto functions all defaulted to using -// binary-encoded strings rather than buffers. - 'use strict'; exports.DEFAULT_ENCODING = 'buffer'; @@ -26,14 +23,14 @@ const LazyTransform = require('internal/streams/lazy_transform'); const DH_GENERATOR = 2; -// This is here because many functions accepted binary strings without +// This is here because many functions accepted utf8 strings without // any explicit encoding in older versions of node, and we don't want // to break them unnecessarily. function toBuf(str, encoding) { - encoding = encoding || 'binary'; + encoding = encoding || 'utf8'; if (typeof str === 'string') { if (encoding === 'buffer') - encoding = 'binary'; + encoding = 'utf8'; str = new Buffer(str, encoding); } return str; @@ -68,7 +65,7 @@ Hash.prototype._flush = function(callback) { Hash.prototype.update = function(data, encoding) { encoding = encoding || exports.DEFAULT_ENCODING; if (encoding === 'buffer' && typeof data === 'string') - encoding = 'binary'; + encoding = 'utf8'; this._handle.update(data, encoding); return this; }; diff --git a/lib/internal/streams/lazy_transform.js b/lib/internal/streams/lazy_transform.js index 7e290b16cf5cae..f8ea813820fa6b 100644 --- a/lib/internal/streams/lazy_transform.js +++ b/lib/internal/streams/lazy_transform.js @@ -22,7 +22,7 @@ util.inherits(LazyTransform, stream.Transform); get: function() { stream.Transform.call(this, this._options); this._writableState.decodeStrings = false; - this._writableState.defaultEncoding = 'binary'; + this._writableState.defaultEncoding = 'utf8'; return this[prop]; }, set: function(val) { diff --git a/lib/net.js b/lib/net.js index f75c9b25b86fea..9badce4e56216c 100644 --- a/lib/net.js +++ b/lib/net.js @@ -700,9 +700,6 @@ Socket.prototype._write = function(data, encoding, cb) { function createWriteReq(req, handle, data, encoding) { switch (encoding) { - case 'binary': - return handle.writeBinaryString(req, data); - case 'buffer': return handle.writeBuffer(req, data); diff --git a/src/node.cc b/src/node.cc index c0e8dbcd44db21..d98bb5de96ad1b 100644 --- a/src/node.cc +++ b/src/node.cc @@ -1309,16 +1309,8 @@ enum encoding ParseEncoding(const char* encoding, } break; case 'b': - // binary - if (encoding[1] == 'i') { - if (strncmp(encoding + 2, "nary", 4) == 0) - return BINARY; - - // buffer - } else if (encoding[1] == 'u') { - if (strncmp(encoding + 2, "ffer", 4) == 0) - return BUFFER; - } + if (strncmp(encoding + 1, "uffer", 5) == 0) + return BUFFER; break; case '\0': return default_encoding; @@ -1342,8 +1334,6 @@ enum encoding ParseEncoding(const char* encoding, return UCS2; } else if (strcasecmp(encoding, "utf-16le") == 0) { return UCS2; - } else if (strcasecmp(encoding, "binary") == 0) { - return BINARY; } else if (strcasecmp(encoding, "buffer") == 0) { return BUFFER; } else if (strcasecmp(encoding, "hex") == 0) { @@ -1385,7 +1375,7 @@ ssize_t DecodeBytes(Isolate* isolate, if (val->IsArray()) { fprintf(stderr, "'raw' encoding (array of integers) has been removed. " - "Use 'binary'.\n"); + "Use 'utf8'.\n"); UNREACHABLE(); return -1; } diff --git a/src/node.h b/src/node.h index fcbb45085c5439..f862413d82c79e 100644 --- a/src/node.h +++ b/src/node.h @@ -268,15 +268,15 @@ inline void NODE_SET_PROTOTYPE_METHOD(v8::Local recv, } #define NODE_SET_PROTOTYPE_METHOD node::NODE_SET_PROTOTYPE_METHOD -enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER}; +enum encoding {ASCII, UTF8, BASE64, UCS2, HEX, BUFFER}; NODE_EXTERN enum encoding ParseEncoding( v8::Isolate* isolate, v8::Local encoding_v, - enum encoding default_encoding = BINARY); + enum encoding default_encoding = UTF8); NODE_DEPRECATED("Use ParseEncoding(isolate, ...)", inline enum encoding ParseEncoding( v8::Local encoding_v, - enum encoding default_encoding = BINARY) { + enum encoding default_encoding = UTF8) { return ParseEncoding(v8::Isolate::GetCurrent(), encoding_v, default_encoding); }) @@ -292,7 +292,7 @@ NODE_DEPRECATED("Use FatalException(isolate, ...)", NODE_EXTERN v8::Local Encode(v8::Isolate* isolate, const char* buf, size_t len, - enum encoding encoding = BINARY); + enum encoding encoding = UTF8); // The input buffer should be in host endianness. NODE_EXTERN v8::Local Encode(v8::Isolate* isolate, @@ -303,7 +303,7 @@ NODE_DEPRECATED("Use Encode(isolate, ...)", inline v8::Local Encode( const void* buf, size_t len, - enum encoding encoding = BINARY) { + enum encoding encoding = UTF8) { v8::Isolate* isolate = v8::Isolate::GetCurrent(); if (encoding == UCS2) { assert(reinterpret_cast(buf) % sizeof(uint16_t) == 0 && @@ -317,11 +317,11 @@ NODE_DEPRECATED("Use Encode(isolate, ...)", // Returns -1 if the handle was not valid for decoding NODE_EXTERN ssize_t DecodeBytes(v8::Isolate* isolate, v8::Local, - enum encoding encoding = BINARY); + enum encoding encoding = UTF8); NODE_DEPRECATED("Use DecodeBytes(isolate, ...)", inline ssize_t DecodeBytes( v8::Local val, - enum encoding encoding = BINARY) { + enum encoding encoding = UTF8) { return DecodeBytes(v8::Isolate::GetCurrent(), val, encoding); }) @@ -330,12 +330,12 @@ NODE_EXTERN ssize_t DecodeWrite(v8::Isolate* isolate, char* buf, size_t buflen, v8::Local, - enum encoding encoding = BINARY); + enum encoding encoding = UTF8); NODE_DEPRECATED("Use DecodeWrite(isolate, ...)", inline ssize_t DecodeWrite(char* buf, size_t buflen, v8::Local val, - enum encoding encoding = BINARY) { + enum encoding encoding = UTF8) { return DecodeWrite(v8::Isolate::GetCurrent(), buf, buflen, val, encoding); }) diff --git a/src/node_buffer.cc b/src/node_buffer.cc index 026a04028a1eb8..4e3e25583b3ccb 100644 --- a/src/node_buffer.cc +++ b/src/node_buffer.cc @@ -502,11 +502,6 @@ void StringSlice(const FunctionCallbackInfo& args) { } -void BinarySlice(const FunctionCallbackInfo& args) { - StringSlice(args); -} - - void AsciiSlice(const FunctionCallbackInfo& args) { StringSlice(args); } @@ -703,11 +698,6 @@ void Base64Write(const FunctionCallbackInfo& args) { } -void BinaryWrite(const FunctionCallbackInfo& args) { - StringWrite(args); -} - - void Utf8Write(const FunctionCallbackInfo& args) { StringWrite(args); } @@ -899,8 +889,7 @@ void IndexOfString(const FunctionCallbackInfo& args) { const char* haystack = ts_obj_data; const size_t haystack_length = ts_obj_length; // Extended latin-1 characters are 2 bytes in Utf8. - const size_t needle_length = - enc == BINARY ? needle->Length() : needle->Utf8Length(); + const size_t needle_length = needle->Utf8Length(); if (needle_length == 0 || haystack_length == 0) { @@ -967,20 +956,6 @@ void IndexOfString(const FunctionCallbackInfo& args) { reinterpret_cast(*needle_value), needle_length, offset); - } else if (enc == BINARY) { - uint8_t* needle_data = static_cast(malloc(needle_length)); - if (needle_data == nullptr) { - return args.GetReturnValue().Set(-1); - } - needle->WriteOneByte( - needle_data, 0, needle_length, String::NO_NULL_TERMINATION); - - result = SearchString(reinterpret_cast(haystack), - haystack_length, - needle_data, - needle_length, - offset); - free(needle_data); } args.GetReturnValue().Set( @@ -1093,14 +1068,12 @@ void SetupBufferJS(const FunctionCallbackInfo& args) { env->SetMethod(proto, "asciiSlice", AsciiSlice); env->SetMethod(proto, "base64Slice", Base64Slice); - env->SetMethod(proto, "binarySlice", BinarySlice); env->SetMethod(proto, "hexSlice", HexSlice); env->SetMethod(proto, "ucs2Slice", Ucs2Slice); env->SetMethod(proto, "utf8Slice", Utf8Slice); env->SetMethod(proto, "asciiWrite", AsciiWrite); env->SetMethod(proto, "base64Write", Base64Write); - env->SetMethod(proto, "binaryWrite", BinaryWrite); env->SetMethod(proto, "hexWrite", HexWrite); env->SetMethod(proto, "ucs2Write", Ucs2Write); env->SetMethod(proto, "utf8Write", Utf8Write); diff --git a/src/node_crypto.cc b/src/node_crypto.cc index 986fe80db449f4..bd6e8f6e678fea 100644 --- a/src/node_crypto.cc +++ b/src/node_crypto.cc @@ -3370,7 +3370,7 @@ void CipherBase::Update(const FunctionCallbackInfo& args) { // Only copy the data if we have to, because it's a string if (args[0]->IsString()) { StringBytes::InlineDecoder decoder; - if (!decoder.Decode(env, args[0].As(), args[1], BINARY)) + if (!decoder.Decode(env, args[0].As(), args[1], UTF8)) return; r = cipher->Update(decoder.out(), decoder.size(), &out, &out_len); } else { @@ -3549,7 +3549,7 @@ void Hmac::HmacUpdate(const FunctionCallbackInfo& args) { bool r; if (args[0]->IsString()) { StringBytes::InlineDecoder decoder; - if (!decoder.Decode(env, args[0].As(), args[1], BINARY)) + if (!decoder.Decode(env, args[0].As(), args[1], UTF8)) return; r = hmac->HmacUpdate(decoder.out(), decoder.size()); } else { @@ -3667,7 +3667,7 @@ void Hash::HashUpdate(const FunctionCallbackInfo& args) { bool r; if (args[0]->IsString()) { StringBytes::InlineDecoder decoder; - if (!decoder.Decode(env, args[0].As(), args[1], BINARY)) + if (!decoder.Decode(env, args[0].As(), args[1], UTF8)) return; r = hash->HashUpdate(decoder.out(), decoder.size()); } else { @@ -3819,7 +3819,7 @@ void Sign::SignUpdate(const FunctionCallbackInfo& args) { Error err; if (args[0]->IsString()) { StringBytes::InlineDecoder decoder; - if (!decoder.Decode(env, args[0].As(), args[1], BINARY)) + if (!decoder.Decode(env, args[0].As(), args[1], UTF8)) return; err = sign->SignUpdate(decoder.out(), decoder.size()); } else { @@ -4021,7 +4021,7 @@ void Verify::VerifyUpdate(const FunctionCallbackInfo& args) { Error err; if (args[0]->IsString()) { StringBytes::InlineDecoder decoder; - if (!decoder.Decode(env, args[0].As(), args[1], BINARY)) + if (!decoder.Decode(env, args[0].As(), args[1], UTF8)) return; err = verify->VerifyUpdate(decoder.out(), decoder.size()); } else { @@ -4120,12 +4120,11 @@ void Verify::VerifyFinal(const FunctionCallbackInfo& args) { THROW_AND_RETURN_IF_NOT_STRING_OR_BUFFER(args[1]); - // BINARY works for both buffers and binary strings. - enum encoding encoding = BINARY; + enum encoding encoding = UTF8; if (args.Length() >= 3) { encoding = ParseEncoding(env->isolate(), args[2]->ToString(env->isolate()), - BINARY); + UTF8); } ssize_t hlen = StringBytes::Size(env->isolate(), args[1], encoding); diff --git a/src/stream_base-inl.h b/src/stream_base-inl.h index 81114a265e9a08..3d22b4162ebc51 100644 --- a/src/stream_base-inl.h +++ b/src/stream_base-inl.h @@ -61,9 +61,6 @@ void StreamBase::AddMethods(Environment* env, env->SetProtoMethod(t, "writeUcs2String", JSMethod >); - env->SetProtoMethod(t, - "writeBinaryString", - JSMethod >); } diff --git a/src/stream_base.cc b/src/stream_base.cc index 27ae0fee7b1309..2c44ed568709f8 100644 --- a/src/stream_base.cc +++ b/src/stream_base.cc @@ -33,8 +33,6 @@ template int StreamBase::WriteString( const FunctionCallbackInfo& args); template int StreamBase::WriteString( const FunctionCallbackInfo& args); -template int StreamBase::WriteString( - const FunctionCallbackInfo& args); int StreamBase::ReadStart(const FunctionCallbackInfo& args) { diff --git a/src/string_bytes.cc b/src/string_bytes.cc index a916caf75e8960..864ff544c18408 100644 --- a/src/string_bytes.cc +++ b/src/string_bytes.cc @@ -367,7 +367,6 @@ size_t StringBytes::Write(Isolate* isolate, switch (encoding) { case ASCII: - case BINARY: case BUFFER: if (is_extern && str->IsOneByte()) { memcpy(buf, data, nbytes); @@ -472,14 +471,13 @@ size_t StringBytes::StorageSize(Isolate* isolate, size_t data_size = 0; bool is_buffer = Buffer::HasInstance(val); - if (is_buffer && (encoding == BUFFER || encoding == BINARY)) { + if (is_buffer && encoding == BUFFER) { return Buffer::Length(val); } Local str = val->ToString(isolate); switch (encoding) { - case BINARY: case BUFFER: case ASCII: data_size = str->Length(); @@ -521,7 +519,7 @@ size_t StringBytes::Size(Isolate* isolate, size_t data_size = 0; bool is_buffer = Buffer::HasInstance(val); - if (is_buffer && (encoding == BUFFER || encoding == BINARY)) + if (is_buffer && encoding == BUFFER) return Buffer::Length(val); const char* data; @@ -531,7 +529,6 @@ size_t StringBytes::Size(Isolate* isolate, Local str = val->ToString(isolate); switch (encoding) { - case BINARY: case BUFFER: case ASCII: data_size = str->Length(); @@ -799,13 +796,6 @@ Local StringBytes::Encode(Isolate* isolate, buflen); break; - case BINARY: - if (buflen < EXTERN_APEX) - val = OneByteString(isolate, buf, buflen); - else - val = ExternOneByteString::NewFromCopy(isolate, buf, buflen); - break; - case BASE64: { size_t dlen = base64_encoded_size(buflen); char* dst = static_cast(malloc(dlen)); diff --git a/test/parallel/test-buffer-bytelength.js b/test/parallel/test-buffer-bytelength.js index ef68097157b1d8..b46f21dd59f506 100644 --- a/test/parallel/test-buffer-bytelength.js +++ b/test/parallel/test-buffer-bytelength.js @@ -5,9 +5,7 @@ var assert = require('assert'); var Buffer = require('buffer').Buffer; // coerce values to string -assert.equal(Buffer.byteLength(32, 'binary'), 2); assert.equal(Buffer.byteLength(NaN, 'utf8'), 3); -assert.equal(Buffer.byteLength({}, 'binary'), 15); assert.equal(Buffer.byteLength(), 9); // buffer @@ -46,7 +44,6 @@ assert.equal(Buffer.byteLength('aaaa==', 'base64'), 3); assert.equal(Buffer.byteLength('Il était tué'), 14); assert.equal(Buffer.byteLength('Il était tué', 'utf8'), 14); assert.equal(Buffer.byteLength('Il était tué', 'ascii'), 12); -assert.equal(Buffer.byteLength('Il était tué', 'binary'), 12); ['ucs2', 'ucs-2', 'utf16le', 'utf-16le'].forEach(function(encoding) { assert.equal(24, Buffer.byteLength('Il était tué', encoding)); }); diff --git a/test/parallel/test-buffer-fill.js b/test/parallel/test-buffer-fill.js index 4ff0b12a46c227..c48eb0070b0056 100644 --- a/test/parallel/test-buffer-fill.js +++ b/test/parallel/test-buffer-fill.js @@ -52,27 +52,6 @@ testBufs('a\u0234b\u0235c\u0236', 12, 1, 'utf8'); assert.equal(Buffer(1).fill(0).fill('\u0222')[0], 0xc8); -// BINARY -testBufs('abc', 'binary'); -testBufs('\u0222aa', 'binary'); -testBufs('a\u0234b\u0235c\u0236', 'binary'); -testBufs('abc', 4, 'binary'); -testBufs('abc', 5, 'binary'); -testBufs('abc', SIZE, 'binary'); -testBufs('\u0222aa', 2, 'binary'); -testBufs('\u0222aa', 8, 'binary'); -testBufs('a\u0234b\u0235c\u0236', 4, 'binary'); -testBufs('a\u0234b\u0235c\u0236', 12, 'binary'); -testBufs('abc', 4, -1, 'binary'); -testBufs('abc', 4, 1, 'binary'); -testBufs('abc', 5, 1, 'binary'); -testBufs('\u0222aa', 2, -1, 'binary'); -testBufs('\u0222aa', 8, 1, 'binary'); -testBufs('a\u0234b\u0235c\u0236', 4, -1, 'binary'); -testBufs('a\u0234b\u0235c\u0236', 4, 1, 'binary'); -testBufs('a\u0234b\u0235c\u0236', 12, 1, 'binary'); - - // UCS2 testBufs('abc', 'ucs2'); testBufs('\u0222aa', 'ucs2'); diff --git a/test/parallel/test-buffer-includes.js b/test/parallel/test-buffer-includes.js index 907bbdf961efd8..64456d86319b5c 100644 --- a/test/parallel/test-buffer-includes.js +++ b/test/parallel/test-buffer-includes.js @@ -102,15 +102,6 @@ assert( Buffer(b.toString('ascii'), 'ascii') .includes(Buffer('d', 'ascii'), 0, 'ascii')); -// test binary encoding -assert( - Buffer(b.toString('binary'), 'binary') - .includes('d', 0, 'binary')); -assert( - Buffer(b.toString('binary'), 'binary') - .includes(Buffer('d', 'binary'), 0, 'binary')); - - // test usc2 encoding var twoByteString = new Buffer('\u039a\u0391\u03a3\u03a3\u0395', 'ucs2'); diff --git a/test/parallel/test-buffer-indexof.js b/test/parallel/test-buffer-indexof.js index 101a60cbe5ae7f..214e43ea4be773 100644 --- a/test/parallel/test-buffer-indexof.js +++ b/test/parallel/test-buffer-indexof.js @@ -102,24 +102,6 @@ assert.equal( Buffer(b.toString('ascii'), 'ascii') .indexOf(Buffer('d', 'ascii'), 0, 'ascii'), 3); -// test binary encoding -assert.equal( - Buffer(b.toString('binary'), 'binary') - .indexOf('d', 0, 'binary'), 3); -assert.equal( - Buffer(b.toString('binary'), 'binary') - .indexOf(Buffer('d', 'binary'), 0, 'binary'), 3); -assert.equal( - Buffer('aa\u00e8aa', 'binary') - .indexOf('\u00e8', 'binary'), 2); -assert.equal( - Buffer('\u00e8', 'binary') - .indexOf('\u00e8', 'binary'), 0); -assert.equal( - Buffer('\u00e8', 'binary') - .indexOf(Buffer('\u00e8', 'binary'), 'binary'), 0); - - // test optional offset with passed encoding assert.equal(new Buffer('aaaa0').indexOf('30', 'hex'), 4); assert.equal(new Buffer('aaaa00a').indexOf('3030', 'hex'), 4); diff --git a/test/parallel/test-buffer.js b/test/parallel/test-buffer.js index 8d42bea82d5e8c..f37cb3e1e7a8dd 100644 --- a/test/parallel/test-buffer.js +++ b/test/parallel/test-buffer.js @@ -235,7 +235,6 @@ assert.strictEqual('Unknown encoding: invalid', caught_error.message); // try to create 0-length buffers new Buffer(''); new Buffer('', 'ascii'); -new Buffer('', 'binary'); new Buffer(0); // try to write a 0-length string beyond the end of b @@ -672,7 +671,7 @@ assert.equal(dot.toString('base64'), '//4uAA=='); for (let i = 0; i < segments.length; ++i) { pos += b.write(segments[i], pos, 'base64'); } - assert.equal(b.toString('binary', 0, pos), 'Madness?! This is node.js!'); + assert.equal(b.toString('utf8', 0, pos), 'Madness?! This is node.js!'); } // Regression test for https://github.com/nodejs/node/issues/3496. @@ -824,23 +823,6 @@ assert.equal(0, Buffer('hello').slice(0, 0).length); assert.equal(b.toString(encoding), 'あいうえお'); }); -{ - // Binary encoding should write only one byte per character. - const b = Buffer([0xde, 0xad, 0xbe, 0xef]); - let s = String.fromCharCode(0xffff); - b.write(s, 0, 'binary'); - assert.equal(0xff, b[0]); - assert.equal(0xad, b[1]); - assert.equal(0xbe, b[2]); - assert.equal(0xef, b[3]); - s = String.fromCharCode(0xaaee); - b.write(s, 0, 'binary'); - assert.equal(0xee, b[0]); - assert.equal(0xad, b[1]); - assert.equal(0xbe, b[2]); - assert.equal(0xef, b[3]); -} - { // #1210 Test UTF-8 string includes null character let buf = new Buffer('\0'); @@ -952,7 +934,7 @@ assert.equal(0, Buffer('hello').slice(0, 0).length); // test for buffer overrun const buf = new Buffer([0, 0, 0, 0, 0]); // length: 5 var sub = buf.slice(0, 4); // length: 4 - written = sub.write('12345', 'binary'); + written = sub.write('12345', 'utf8'); assert.equal(written, 4); assert.equal(buf[4], 0); } @@ -973,7 +955,7 @@ assert.equal(Buffer('99').length, 2); assert.equal(Buffer('13.37').length, 5); // Ensure that the length argument is respected. -'ascii utf8 hex base64 binary'.split(' ').forEach(function(enc) { +'ascii utf8 hex base64'.split(' ').forEach(function(enc) { assert.equal(Buffer(1).write('aaaaaa', 0, 1, enc), 1); }); @@ -992,7 +974,6 @@ Buffer(Buffer(0), 0, 0); 'utf8', 'utf-8', 'ascii', - 'binary', 'base64', 'ucs2', 'ucs-2', diff --git a/test/parallel/test-crypto-binary-default.js b/test/parallel/test-crypto-binary-default.js deleted file mode 100644 index 3cb98db80aca55..00000000000000 --- a/test/parallel/test-crypto-binary-default.js +++ /dev/null @@ -1,666 +0,0 @@ -'use strict'; -// This is the same as test/simple/test-crypto, but from before the shift -// to use buffers by default. - - -var common = require('../common'); -var assert = require('assert'); -var constants = require('constants'); - -if (!common.hasCrypto) { - console.log('1..0 # Skipped: missing crypto'); - return; -} -var crypto = require('crypto'); -var tls = require('tls'); - -crypto.DEFAULT_ENCODING = 'binary'; - -var fs = require('fs'); -var path = require('path'); - -// Test Certificates -var certPem = fs.readFileSync(common.fixturesDir + '/test_cert.pem', 'ascii'); -var certPfx = fs.readFileSync(common.fixturesDir + '/test_cert.pfx'); -var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii'); -var rsaPubPem = fs.readFileSync(common.fixturesDir + '/test_rsa_pubkey.pem', - 'ascii'); -var rsaKeyPem = fs.readFileSync(common.fixturesDir + '/test_rsa_privkey.pem', - 'ascii'); - -// PFX tests -assert.doesNotThrow(function() { - tls.createSecureContext({pfx:certPfx, passphrase:'sample'}); -}); - -assert.throws(function() { - tls.createSecureContext({pfx:certPfx}); -}, 'mac verify failure'); - -assert.throws(function() { - tls.createSecureContext({pfx:certPfx, passphrase:'test'}); -}, 'mac verify failure'); - -assert.throws(function() { - tls.createSecureContext({pfx:'sample', passphrase:'test'}); -}, 'not enough data'); - -// Test HMAC -const hmacHash = crypto.createHmac('sha1', 'Node') - .update('some data') - .update('to hmac') - .digest('hex'); -assert.equal(hmacHash, '19fd6e1ba73d9ed2224dd5094a71babe85d9a892', 'test HMAC'); - -// Test HMAC-SHA-* (rfc 4231 Test Cases) -var rfc4231 = [ - { - key: new Buffer('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), - data: new Buffer('4869205468657265', 'hex'), // 'Hi There' - hmac: { - sha224: '896fb1128abbdf196832107cd49df33f47b4b1169912ba4f53684b22', - sha256: - 'b0344c61d8db38535ca8afceaf0bf12b881dc200c9833da726e9376c' + - '2e32cff7', - sha384: - 'afd03944d84895626b0825f4ab46907f15f9dadbe4101ec682aa034c' + - '7cebc59cfaea9ea9076ede7f4af152e8b2fa9cb6', - sha512: - '87aa7cdea5ef619d4ff0b4241a1d6cb02379f4e2ce4ec2787ad0b305' + - '45e17cdedaa833b7d6b8a702038b274eaea3f4e4be9d914eeb61f170' + - '2e696c203a126854' - } - }, - { - key: new Buffer('4a656665', 'hex'), // 'Jefe' - data: new Buffer('7768617420646f2079612077616e7420666f72206e6f74686' + - '96e673f', 'hex'), // 'what do ya want for nothing?' - hmac: { - sha224: 'a30e01098bc6dbbf45690f3a7e9e6d0f8bbea2a39e6148008fd05e44', - sha256: - '5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b9' + - '64ec3843', - sha384: - 'af45d2e376484031617f78d2b58a6b1b9c7ef464f5a01b47e42ec373' + - '6322445e8e2240ca5e69e2c78b3239ecfab21649', - sha512: - '164b7a7bfcf819e2e395fbe73b56e0a387bd64222e831fd610270cd7' + - 'ea2505549758bf75c05a994a6d034f65f8f0e6fdcaeab1a34d4a6b4b' + - '636e070a38bce737' - } - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), - data: new Buffer('ddddddddddddddddddddddddddddddddddddddddddddddddd' + - 'ddddddddddddddddddddddddddddddddddddddddddddddddddd', - 'hex'), - hmac: { - sha224: '7fb3cb3588c6c1f6ffa9694d7d6ad2649365b0c1f65d69d1ec8333ea', - sha256: - '773ea91e36800e46854db8ebd09181a72959098b3ef8c122d9635514' + - 'ced565fe', - sha384: - '88062608d3e6ad8a0aa2ace014c8a86f0aa635d947ac9febe83ef4e5' + - '5966144b2a5ab39dc13814b94e3ab6e101a34f27', - sha512: - 'fa73b0089d56a284efb0f0756c890be9b1b5dbdd8ee81a3655f83e33' + - 'b2279d39bf3e848279a722c806b485a47e67c807b946a337bee89426' + - '74278859e13292fb' - } - }, - { - key: new Buffer('0102030405060708090a0b0c0d0e0f10111213141516171819', - 'hex'), - data: new Buffer('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + - 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd', - 'hex'), - hmac: { - sha224: '6c11506874013cac6a2abc1bb382627cec6a90d86efc012de7afec5a', - sha256: - '82558a389a443c0ea4cc819899f2083a85f0faa3e578f8077a2e3ff4' + - '6729665b', - sha384: - '3e8a69b7783c25851933ab6290af6ca77a9981480850009cc5577c6e' + - '1f573b4e6801dd23c4a7d679ccf8a386c674cffb', - sha512: - 'b0ba465637458c6990e5a8c5f61d4af7e576d97ff94b872de76f8050' + - '361ee3dba91ca5c11aa25eb4d679275cc5788063a5f19741120c4f2d' + - 'e2adebeb10a298dd' - } - }, - - { - key: new Buffer('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), - // 'Test With Truncation' - data: new Buffer('546573742057697468205472756e636174696f6e', 'hex'), - hmac: { - sha224: '0e2aea68a90c8d37c988bcdb9fca6fa8', - sha256: 'a3b6167473100ee06e0c796c2955552b', - sha384: '3abf34c3503b2a23a46efc619baef897', - sha512: '415fad6271580a531d4179bc891d87a6' - }, - truncate: true - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaa', 'hex'), - // 'Test Using Larger Than Block-Size Key - Hash Key First' - data: new Buffer('54657374205573696e67204c6172676572205468616e20426' + - 'c6f636b2d53697a65204b6579202d2048617368204b657920' + - '4669727374', 'hex'), - hmac: { - sha224: '95e9a0db962095adaebe9b2d6f0dbce2d499f112f2d2b7273fa6870e', - sha256: - '60e431591ee0b67f0d8a26aacbf5b77f8e0bc6213728c5140546040f' + - '0ee37f54', - sha384: - '4ece084485813e9088d2c63a041bc5b44f9ef1012a2b588f3cd11f05' + - '033ac4c60c2ef6ab4030fe8296248df163f44952', - sha512: - '80b24263c7c1a3ebb71493c1dd7be8b49b46d1f41b4aeec1121b0137' + - '83f8f3526b56d037e05f2598bd0fd2215d6a1e5295e64f73f63f0aec' + - '8b915a985d786598' - } - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaa', 'hex'), - // 'This is a test using a larger than block-size key and a larger ' + - // 'than block-size data. The key needs to be hashed before being ' + - // 'used by the HMAC algorithm.' - data: new Buffer('5468697320697320612074657374207573696e672061206c6' + - '172676572207468616e20626c6f636b2d73697a65206b6579' + - '20616e642061206c6172676572207468616e20626c6f636b2' + - 'd73697a6520646174612e20546865206b6579206e65656473' + - '20746f20626520686173686564206265666f7265206265696' + - 'e6720757365642062792074686520484d414320616c676f72' + - '6974686d2e', 'hex'), - hmac: { - sha224: '3a854166ac5d9f023f54d517d0b39dbd946770db9c2b95c9f6f565d1', - sha256: - '9b09ffa71b942fcb27635fbcd5b0e944bfdc63644f0713938a7f5153' + - '5c3a35e2', - sha384: - '6617178e941f020d351e2f254e8fd32c602420feb0b8fb9adccebb82' + - '461e99c5a678cc31e799176d3860e6110c46523e', - sha512: - 'e37b6a775dc87dbaa4dfa9f96e5e3ffddebd71f8867289865df5a32d' + - '20cdc944b6022cac3c4982b10d5eeb55c3e4de15134676fb6de04460' + - '65c97440fa8c6a58' - } - } -]; - -for (let i = 0, l = rfc4231.length; i < l; i++) { - for (var hash in rfc4231[i]['hmac']) { - var result = crypto.createHmac(hash, rfc4231[i]['key']) - .update(rfc4231[i]['data']) - .digest('hex'); - if (rfc4231[i]['truncate']) { - result = result.substr(0, 32); // first 128 bits == 32 hex chars - } - assert.equal(rfc4231[i]['hmac'][hash], - result, - 'Test HMAC-' + hash + ': Test case ' + (i + 1) + ' rfc 4231'); - } -} - -// Test HMAC-MD5/SHA1 (rfc 2202 Test Cases) -var rfc2202_md5 = [ - { - key: new Buffer('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), - data: 'Hi There', - hmac: '9294727a3638bb1c13f48ef8158bfc9d' - }, - { - key: 'Jefe', - data: 'what do ya want for nothing?', - hmac: '750c783e6ab0b503eaa86e310a5db738' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), - data: new Buffer('ddddddddddddddddddddddddddddddddddddddddddddddddd' + - 'ddddddddddddddddddddddddddddddddddddddddddddddddddd', - 'hex'), - hmac: '56be34521d144c88dbb8c733f0e8b3f6' - }, - { - key: new Buffer('0102030405060708090a0b0c0d0e0f10111213141516171819', - 'hex'), - data: new Buffer('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + - 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + - 'cdcdcdcdcd', - 'hex'), - hmac: '697eaf0aca3a3aea3a75164746ffaa79' - }, - { - key: new Buffer('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), - data: 'Test With Truncation', - hmac: '56461ef2342edc00f9bab995690efd4c' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaa', - 'hex'), - data: 'Test Using Larger Than Block-Size Key - Hash Key First', - hmac: '6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaa', - 'hex'), - data: - 'Test Using Larger Than Block-Size Key and Larger Than One ' + - 'Block-Size Data', - hmac: '6f630fad67cda0ee1fb1f562db3aa53e' - } -]; -var rfc2202_sha1 = [ - { - key: new Buffer('0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b', 'hex'), - data: 'Hi There', - hmac: 'b617318655057264e28bc0b6fb378c8ef146be00' - }, - { - key: 'Jefe', - data: 'what do ya want for nothing?', - hmac: 'effcdf6ae5eb2fa2d27416d5f184df9c259a7c79' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa', 'hex'), - data: new Buffer('ddddddddddddddddddddddddddddddddddddddddddddd' + - 'ddddddddddddddddddddddddddddddddddddddddddddd' + - 'dddddddddd', - 'hex'), - hmac: '125d7342b9ac11cd91a39af48aa17b4f63f175d3' - }, - { - key: new Buffer('0102030405060708090a0b0c0d0e0f10111213141516171819', - 'hex'), - data: new Buffer('cdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdc' + - 'dcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcdcd' + - 'cdcdcdcdcd', - 'hex'), - hmac: '4c9007f4026250c6bc8414f9bf50c86c2d7235da' - }, - { - key: new Buffer('0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c', 'hex'), - data: 'Test With Truncation', - hmac: '4c1a03424b55e07fe7f27be1d58bb9324a9a5a04' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaa', - 'hex'), - data: 'Test Using Larger Than Block-Size Key - Hash Key First', - hmac: 'aa4ae5e15272d00e95705637ce8a3b55ed402112' - }, - { - key: new Buffer('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' + - 'aaaaaaaaaaaaaaaaaaaaaa', - 'hex'), - data: - 'Test Using Larger Than Block-Size Key and Larger Than One ' + - 'Block-Size Data', - hmac: 'e8e99d0f45237d786d6bbaa7965c7808bbff1a91' - } -]; - -for (let i = 0, l = rfc2202_md5.length; i < l; i++) { - if (!common.hasFipsCrypto) { - assert.equal(rfc2202_md5[i]['hmac'], - crypto.createHmac('md5', rfc2202_md5[i]['key']) - .update(rfc2202_md5[i]['data']) - .digest('hex'), - 'Test HMAC-MD5 : Test case ' + (i + 1) + ' rfc 2202'); - } -} -for (let i = 0, l = rfc2202_sha1.length; i < l; i++) { - assert.equal(rfc2202_sha1[i]['hmac'], - crypto.createHmac('sha1', rfc2202_sha1[i]['key']) - .update(rfc2202_sha1[i]['data']) - .digest('hex'), - 'Test HMAC-SHA1 : Test case ' + (i + 1) + ' rfc 2202'); -} - -// Test hashing -var a1 = crypto.createHash('sha1').update('Test123').digest('hex'); -var a2 = crypto.createHash('sha256').update('Test123').digest('base64'); -var a3 = crypto.createHash('sha512').update('Test123').digest(); // binary -var a4 = crypto.createHash('sha1').update('Test123').digest('buffer'); - -if (!common.hasFipsCrypto) { - var a0 = crypto.createHash('md5').update('Test123').digest('binary'); - assert.equal(a0, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' + - '\u00bd\u008c', 'Test MD5 as binary'); -} - -assert.equal(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1'); - -assert.equal(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=', - 'Test SHA256 as base64'); - -assert.equal(a3, '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' + - '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' + - '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' + - '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' + - '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'', - 'Test SHA512 as assumed binary'); - -assert.deepEqual(a4, - new Buffer('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex'), - 'Test SHA1'); - -// Test multiple updates to same hash -var h1 = crypto.createHash('sha1').update('Test123').digest('hex'); -var h2 = crypto.createHash('sha1').update('Test').update('123').digest('hex'); -assert.equal(h1, h2, 'multipled updates'); - -// Test hashing for binary files -var fn = path.join(common.fixturesDir, 'sample.png'); -var sha1Hash = crypto.createHash('sha1'); -var fileStream = fs.createReadStream(fn); -fileStream.on('data', function(data) { - sha1Hash.update(data); -}); -fileStream.on('close', function() { - assert.equal(sha1Hash.digest('hex'), - '22723e553129a336ad96e10f6aecdf0f45e4149e', - 'Test SHA1 of sample.png'); -}); - -// Issue #2227: unknown digest method should throw an error. -assert.throws(function() { - crypto.createHash('xyzzy'); -}); - -// Test signing and verifying -var s1 = crypto.createSign('RSA-SHA1') - .update('Test123') - .sign(keyPem, 'base64'); -var s1Verified = crypto.createVerify('RSA-SHA1') - .update('Test') - .update('123') - .verify(certPem, s1, 'base64'); -assert.strictEqual(s1Verified, true, 'sign and verify (base 64)'); - -var s2 = crypto.createSign('RSA-SHA256') - .update('Test123') - .sign(keyPem); // binary -var s2Verified = crypto.createVerify('RSA-SHA256') - .update('Test') - .update('123') - .verify(certPem, s2); // binary -assert.strictEqual(s2Verified, true, 'sign and verify (binary)'); - -var s3 = crypto.createSign('RSA-SHA1') - .update('Test123') - .sign(keyPem, 'buffer'); -var s3Verified = crypto.createVerify('RSA-SHA1') - .update('Test') - .update('123') - .verify(certPem, s3); -assert.strictEqual(s3Verified, true, 'sign and verify (buffer)'); - - -function testCipher1(key) { - // Test encryption and decryption - var plaintext = 'Keep this a secret? No! Tell everyone about node.js!'; - var cipher = crypto.createCipher('aes192', key); - - // encrypt plaintext which is in utf8 format - // to a ciphertext which will be in hex - var ciph = cipher.update(plaintext, 'utf8', 'hex'); - // Only use binary or hex, not base64. - ciph += cipher.final('hex'); - - var decipher = crypto.createDecipher('aes192', key); - var txt = decipher.update(ciph, 'hex', 'utf8'); - txt += decipher.final('utf8'); - - assert.equal(txt, plaintext, 'encryption and decryption'); -} - - -function testCipher2(key) { - // encryption and decryption with Base64 - // reported in https://github.com/joyent/node/issues/738 - var plaintext = - '32|RmVZZkFUVmpRRkp0TmJaUm56ZU9qcnJkaXNNWVNpTTU*|iXmckfRWZBGWWELw' + - 'eCBsThSsfUHLeRe0KCsK8ooHgxie0zOINpXxfZi/oNG7uq9JWFVCk70gfzQH8ZUJ' + - 'jAfaFg**'; - var cipher = crypto.createCipher('aes256', key); - - // 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', key); - var txt = decipher.update(ciph, 'base64', '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'); -} - - -function testCipher4(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', 'buffer'); - ciph = Buffer.concat([ciph, cipher.final('buffer')]); - - var decipher = crypto.createDecipheriv('des-ede3-cbc', key, iv); - var txt = decipher.update(ciph, 'buffer', 'utf8'); - txt += decipher.final('utf8'); - - assert.equal(txt, plaintext, 'encryption and decryption with key and iv'); -} - -if (!common.hasFipsCrypto) { - 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')); - -testCipher4(new Buffer('0123456789abcd0123456789'), new Buffer('12345678')); - - -// update() should only take buffers / strings -assert.throws(function() { - crypto.createHash('sha1').update({foo: 'bar'}); -}, /buffer/); - - -// Test Diffie-Hellman with two parties sharing a secret, -// using various encodings as we go along -var dh1 = crypto.createDiffieHellman(common.hasFipsCrypto ? 1024 : 256); -var p1 = dh1.getPrime('buffer'); -var dh2 = crypto.createDiffieHellman(p1, 'base64'); -var key1 = dh1.generateKeys(); -var key2 = dh2.generateKeys('hex'); -var secret1 = dh1.computeSecret(key2, 'hex', 'base64'); -var secret2 = dh2.computeSecret(key1, 'binary', 'buffer'); - -assert.equal(secret1, secret2.toString('base64')); - -// Create "another dh1" using generated keys from dh1, -// and compute secret again -var dh3 = crypto.createDiffieHellman(p1, 'buffer'); -var privkey1 = dh1.getPrivateKey(); -dh3.setPublicKey(key1); -dh3.setPrivateKey(privkey1); - -assert.equal(dh1.getPrime(), dh3.getPrime()); -assert.equal(dh1.getGenerator(), dh3.getGenerator()); -assert.equal(dh1.getPublicKey(), dh3.getPublicKey()); -assert.equal(dh1.getPrivateKey(), dh3.getPrivateKey()); - -var secret3 = dh3.computeSecret(key2, 'hex', 'base64'); - -assert.equal(secret1, secret3); - -// https://github.com/joyent/node/issues/2338 -var p = 'FFFFFFFFFFFFFFFFC90FDAA22168C234C4C6628B80DC1CD129024E088A67CC74' + - '020BBEA63B139B22514A08798E3404DDEF9519B3CD3A431B302B0A6DF25F1437' + - '4FE1356D6D51C245E485B576625E7EC6F44C42E9A637ED6B0BFF5CB6F406B7ED' + - 'EE386BFB5A899FA5AE9F24117C4B1FE649286651ECE65381FFFFFFFFFFFFFFFF'; -var d = crypto.createDiffieHellman(p, 'hex'); -assert.equal(d.verifyError, constants.DH_NOT_SUITABLE_GENERATOR); - -// Test RSA key signing/verification -var rsaSign = crypto.createSign('RSA-SHA1'); -var rsaVerify = crypto.createVerify('RSA-SHA1'); -assert.ok(rsaSign); -assert.ok(rsaVerify); - -rsaSign.update(rsaPubPem); -var rsaSignature = rsaSign.sign(rsaKeyPem, 'hex'); -assert.equal(rsaSignature, - '5c50e3145c4e2497aadb0eabc83b342d0b0021ece0d4c4a064b7c' + - '8f020d7e2688b122bfb54c724ac9ee169f83f66d2fe90abeb95e8' + - 'e1290e7e177152a4de3d944cf7d4883114a20ed0f78e70e25ef0f' + - '60f06b858e6af42a2f276ede95bbc6bc9a9bbdda15bd663186a6f' + - '40819a7af19e577bb2efa5e579a1f5ce8a0d4ca8b8f6'); - -rsaVerify.update(rsaPubPem); -assert.strictEqual(rsaVerify.verify(rsaPubPem, rsaSignature, 'hex'), true); - - -// -// Test RSA signing and verification -// -(function() { - var privateKey = fs.readFileSync( - common.fixturesDir + '/test_rsa_privkey_2.pem'); - - var publicKey = fs.readFileSync( - common.fixturesDir + '/test_rsa_pubkey_2.pem'); - - var input = 'I AM THE WALRUS'; - - var signature = - '79d59d34f56d0e94aa6a3e306882b52ed4191f07521f25f505a078dc2f89' + - '396e0c8ac89e996fde5717f4cb89199d8fec249961fcb07b74cd3d2a4ffa' + - '235417b69618e4bcd76b97e29975b7ce862299410e1b522a328e44ac9bb2' + - '8195e0268da7eda23d9825ac43c724e86ceeee0d0d4465678652ccaf6501' + - '0ddfb299bedeb1ad'; - - var sign = crypto.createSign('RSA-SHA256'); - sign.update(input); - - var output = sign.sign(privateKey, 'hex'); - assert.equal(output, signature); - - var verify = crypto.createVerify('RSA-SHA256'); - verify.update(input); - - assert.strictEqual(verify.verify(publicKey, signature, 'hex'), true); -})(); - - -// -// Test DSA signing and verification -// -(function() { - var privateKey = fs.readFileSync( - common.fixturesDir + '/test_dsa_privkey.pem'); - - var publicKey = fs.readFileSync( - common.fixturesDir + '/test_dsa_pubkey.pem'); - - var input = 'I AM THE WALRUS'; - - // DSA signatures vary across runs so there is no static string to verify - // against - var sign = crypto.createSign('DSS1'); - sign.update(input); - var signature = sign.sign(privateKey, 'hex'); - - var verify = crypto.createVerify('DSS1'); - verify.update(input); - - assert.strictEqual(verify.verify(publicKey, signature, 'hex'), true); -})(); - - -// -// Test PBKDF2 with RFC 6070 test vectors (except #4) -// -function testPBKDF2(password, salt, iterations, keylen, expected) { - var actual = crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256'); - assert.equal(actual, expected); - - crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', (err, actual) => { - assert.equal(actual, expected); - }); -} - - -testPBKDF2('password', 'salt', 1, 20, - '\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' + - '\x56\xc4\xf8\x37\xa8\x65\x48\xc9'); - -testPBKDF2('password', 'salt', 2, 20, - '\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9' + - '\x28\xf0\x6d\xd0\x2a\x30\x3f\x8e'); - -testPBKDF2('password', 'salt', 4096, 20, - '\xc5\xe4\x78\xd5\x92\x88\xc8\x41\xaa\x53\x0d\xb6' + - '\x84\x5c\x4c\x8d\x96\x28\x93\xa0'); - -testPBKDF2('passwordPASSWORDpassword', - 'saltSALTsaltSALTsaltSALTsaltSALTsalt', - 4096, - 25, - '\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8' + - '\x11\x6e\x84\xcf\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c'); - -testPBKDF2('pass\0word', 'sa\0lt', 4096, 16, - '\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26' + - '\x65\x0a\x86\x87'); diff --git a/test/parallel/test-crypto-dh.js b/test/parallel/test-crypto-dh.js index 68394dd9bcaff9..d075cf83db0d2e 100644 --- a/test/parallel/test-crypto-dh.js +++ b/test/parallel/test-crypto-dh.js @@ -17,7 +17,7 @@ var dh2 = crypto.createDiffieHellman(p1, 'buffer'); var key1 = dh1.generateKeys(); var key2 = dh2.generateKeys('hex'); var secret1 = dh1.computeSecret(key2, 'hex', 'base64'); -var secret2 = dh2.computeSecret(key1, 'binary', 'buffer'); +var secret2 = dh2.computeSecret(key1, 'utf8', 'buffer'); assert.equal(secret1, secret2.toString('base64')); assert.equal(dh1.verifyError, 0); @@ -155,7 +155,7 @@ const ecdh2 = crypto.createECDH('prime256v1'); key1 = ecdh1.generateKeys(); key2 = ecdh2.generateKeys('hex'); secret1 = ecdh1.computeSecret(key2, 'hex', 'base64'); -secret2 = ecdh2.computeSecret(key1, 'binary', 'buffer'); +secret2 = ecdh2.computeSecret(key1, 'utf8', 'buffer'); assert.equal(secret1, secret2.toString('base64')); @@ -176,7 +176,7 @@ const ecdh3 = crypto.createECDH('secp256k1'); const key3 = ecdh3.generateKeys(); assert.throws(function() { - ecdh2.computeSecret(key3, 'binary', 'buffer'); + ecdh2.computeSecret(key3, 'utf8', 'buffer'); }); // ECDH should allow .setPrivateKey()/.setPublicKey() diff --git a/test/parallel/test-crypto-hash.js b/test/parallel/test-crypto-hash.js index 90ff1fd727d104..89acdea6fa020a 100644 --- a/test/parallel/test-crypto-hash.js +++ b/test/parallel/test-crypto-hash.js @@ -13,7 +13,7 @@ var crypto = require('crypto'); // Test hashing var a1 = crypto.createHash('sha1').update('Test123').digest('hex'); var a2 = crypto.createHash('sha256').update('Test123').digest('base64'); -var a3 = crypto.createHash('sha512').update('Test123').digest(); // binary +var a3 = crypto.createHash('sha512').update('Test123').digest(); // buffer var a4 = crypto.createHash('sha1').update('Test123').digest('buffer'); // stream interface @@ -37,23 +37,15 @@ a8.write(''); a8.end(); a8 = a8.read(); -if (!common.hasFipsCrypto) { - var a0 = crypto.createHash('md5').update('Test123').digest('binary'); - assert.equal(a0, 'h\u00ea\u00cb\u0097\u00d8o\fF!\u00fa+\u000e\u0017\u00ca' + - '\u00bd\u008c', 'Test MD5 as binary'); -} assert.equal(a1, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1'); assert.equal(a2, '2bX1jws4GYKTlxhloUB09Z66PoJZW+y+hq5R8dnx9l4=', 'Test SHA256 as base64'); assert.deepEqual( a3, - new Buffer( - '\u00c1(4\u00f1\u0003\u001fd\u0097!O\'\u00d4C/&Qz\u00d4' + - '\u0094\u0015l\u00b8\u008dQ+\u00db\u001d\u00c4\u00b5}\u00b2' + - '\u00d6\u0092\u00a3\u00df\u00a2i\u00a1\u009b\n\n*\u000f' + - '\u00d7\u00d6\u00a2\u00a8\u0085\u00e3<\u0083\u009c\u0093' + - '\u00c2\u0006\u00da0\u00a1\u00879(G\u00ed\'', - 'binary'), + new Buffer('c12834f1031f6497214f27d4432f2651' + + '7ad494156cb88d512bdb1dc4b57db2d6' + + '92a3dfa269a19b0a0a2a0fd7d6a2a885' + + 'e33c839c93c206da30a187392847ed27', 'hex'), 'Test SHA512 as assumed buffer'); assert.deepEqual(a4, new Buffer('8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'hex'), diff --git a/test/parallel/test-crypto-padding-aes256.js b/test/parallel/test-crypto-padding-aes256.js index 550aa5c061f7ef..bebbc9dc36f6de 100644 --- a/test/parallel/test-crypto-padding-aes256.js +++ b/test/parallel/test-crypto-padding-aes256.js @@ -18,13 +18,13 @@ function aes256(decipherFinal) { function encrypt(val, pad) { var c = crypto.createCipheriv('aes256', key, iv); c.setAutoPadding(pad); - return c.update(val, 'utf8', 'binary') + c.final('binary'); + return c.update(val, 'utf8', 'hex') + c.final('hex'); } function decrypt(val, pad) { var c = crypto.createDecipheriv('aes256', key, iv); c.setAutoPadding(pad); - return c.update(val, 'binary', 'utf8') + c[decipherFinal]('utf8'); + return c.update(val, 'hex', 'utf8') + c[decipherFinal]('utf8'); } // echo 0123456789abcdef0123456789abcdef \ diff --git a/test/parallel/test-crypto-padding.js b/test/parallel/test-crypto-padding.js index bc2011d3d892f5..2d986124aa3f13 100644 --- a/test/parallel/test-crypto-padding.js +++ b/test/parallel/test-crypto-padding.js @@ -65,7 +65,7 @@ function dec(encd, pad) { var decrypt = crypto.createDecipheriv(CIPHER_NAME, KEY_PLAIN, IV_PLAIN); decrypt.setAutoPadding(pad); var plain = decrypt.update(encd, 'hex'); - plain += decrypt.final('binary'); + plain += decrypt.final('utf8'); return plain; } diff --git a/test/parallel/test-crypto-pbkdf2.js b/test/parallel/test-crypto-pbkdf2.js index a05eb70a8b8bfb..a0397c1e568737 100644 --- a/test/parallel/test-crypto-pbkdf2.js +++ b/test/parallel/test-crypto-pbkdf2.js @@ -13,36 +13,36 @@ var crypto = require('crypto'); // function testPBKDF2(password, salt, iterations, keylen, expected) { var actual = crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256'); - assert.equal(actual.toString('binary'), expected); + assert.equal(actual.toString('hex'), expected); crypto.pbkdf2(password, salt, iterations, keylen, 'sha256', (err, actual) => { - assert.equal(actual.toString('binary'), expected); + assert.equal(actual.toString('hex'), expected); }); } testPBKDF2('password', 'salt', 1, 20, - '\x12\x0f\xb6\xcf\xfc\xf8\xb3\x2c\x43\xe7\x22\x52' + - '\x56\xc4\xf8\x37\xa8\x65\x48\xc9'); + '120fb6cffcf8b32c43e72252' + + '56c4f837a86548c9'); testPBKDF2('password', 'salt', 2, 20, - '\xae\x4d\x0c\x95\xaf\x6b\x46\xd3\x2d\x0a\xdf\xf9' + - '\x28\xf0\x6d\xd0\x2a\x30\x3f\x8e'); + 'ae4d0c95af6b46d32d0adff9' + + '28f06dd02a303f8e'); testPBKDF2('password', 'salt', 4096, 20, - '\xc5\xe4\x78\xd5\x92\x88\xc8\x41\xaa\x53\x0d\xb6' + - '\x84\x5c\x4c\x8d\x96\x28\x93\xa0'); + 'c5e478d59288c841aa530db6' + + '845c4c8d962893a0'); testPBKDF2('passwordPASSWORDpassword', 'saltSALTsaltSALTsaltSALTsaltSALTsalt', 4096, 25, - '\x34\x8c\x89\xdb\xcb\xd3\x2b\x2f\x32\xd8\x14\xb8\x11' + - '\x6e\x84\xcf\x2b\x17\x34\x7e\xbc\x18\x00\x18\x1c'); + '348c89dbcbd32b2f32d814b811' + + '6e84cf2b17347ebc1800181c'); testPBKDF2('pass\0word', 'sa\0lt', 4096, 16, - '\x89\xb6\x9d\x05\x16\xf8\x29\x89\x3c\x69\x62\x26\x65' + - '\x0a\x86\x87'); + '89b69d0516f829893c69622665' + + '0a8687'); var expected = '64c486c55d30d4c5a079b8823b7d7cb37ff0556f537da8410233bcec330ed956'; diff --git a/test/parallel/test-crypto-sign-verify.js b/test/parallel/test-crypto-sign-verify.js index 1bfaad3f7d026b..c045dfcfe9afbb 100644 --- a/test/parallel/test-crypto-sign-verify.js +++ b/test/parallel/test-crypto-sign-verify.js @@ -33,23 +33,23 @@ var keyPem = fs.readFileSync(common.fixturesDir + '/test_key.pem', 'ascii'); { const s2 = crypto.createSign('RSA-SHA256') .update('Test123') - .sign(keyPem, 'binary'); + .sign(keyPem, 'hex'); let s2stream = crypto.createSign('RSA-SHA256'); s2stream.end('Test123'); - s2stream = s2stream.sign(keyPem, 'binary'); + s2stream = s2stream.sign(keyPem, 'hex'); assert.equal(s2, s2stream, 'Stream produces same output'); let verified = crypto.createVerify('RSA-SHA256') .update('Test') .update('123') - .verify(certPem, s2, 'binary'); - assert.strictEqual(verified, true, 'sign and verify (binary)'); + .verify(certPem, s2, 'hex'); + assert.strictEqual(verified, true, 'sign and verify (utf8)'); const verStream = crypto.createVerify('RSA-SHA256'); verStream.write('Tes'); verStream.write('t12'); verStream.end('3'); - verified = verStream.verify(certPem, s2, 'binary'); + verified = verStream.verify(certPem, s2, 'hex'); assert.strictEqual(verified, true, 'sign and verify (stream)'); } diff --git a/test/parallel/test-file-read-noexist.js b/test/parallel/test-file-read-noexist.js index 096af7cce36a70..d12ab00278f961 100644 --- a/test/parallel/test-file-read-noexist.js +++ b/test/parallel/test-file-read-noexist.js @@ -6,7 +6,7 @@ var fs = require('fs'); var got_error = false; var filename = path.join(common.fixturesDir, 'does_not_exist.txt'); -fs.readFile(filename, 'binary', function(err, content) { +fs.readFile(filename, 'utf8', function(err, content) { if (err) { got_error = true; } else { diff --git a/test/parallel/test-fs-write-stream-encoding.js b/test/parallel/test-fs-write-stream-encoding.js index 7dabebcd16b046..b685d371137c22 100644 --- a/test/parallel/test-fs-write-stream-encoding.js +++ b/test/parallel/test-fs-write-stream-encoding.js @@ -5,7 +5,7 @@ const fs = require('fs'); const path = require('path'); const stream = require('stream'); const firstEncoding = 'base64'; -const secondEncoding = 'binary'; +const secondEncoding = 'utf8'; const examplePath = path.join(common.fixturesDir, 'x.txt'); const dummyPath = path.join(common.tmpDir, 'x.txt'); diff --git a/test/parallel/test-http-status-message.js b/test/parallel/test-http-status-message.js index e579b8e995e120..b8cb21ad8cdcbe 100644 --- a/test/parallel/test-http-status-message.js +++ b/test/parallel/test-http-status-message.js @@ -22,7 +22,7 @@ function test() { bufs.push(chunk); }); client.on('end', function() { - var head = Buffer.concat(bufs).toString('binary').split('\r\n')[0]; + var head = Buffer.concat(bufs).toString('utf8').split('\r\n')[0]; assert.equal('HTTP/1.1 200 Custom Message', head); console.log('ok'); s.close(); diff --git a/test/parallel/test-net-binary.js b/test/parallel/test-net-binary.js deleted file mode 100644 index 484ae60c111b3a..00000000000000 --- a/test/parallel/test-net-binary.js +++ /dev/null @@ -1,67 +0,0 @@ -/* eslint-disable strict */ -var common = require('../common'); -var assert = require('assert'); -var net = require('net'); - -var binaryString = ''; -for (var i = 255; i >= 0; i--) { - var s = '\'\\' + i.toString(8) + '\''; - var S = eval(s); - assert.ok(S.charCodeAt(0) == i); - assert.ok(S == String.fromCharCode(i)); - binaryString += S; -} - -// safe constructor -var echoServer = net.Server(function(connection) { - connection.setEncoding('binary'); - connection.on('data', function(chunk) { - connection.write(chunk, 'binary'); - }); - connection.on('end', function() { - connection.end(); - }); -}); -echoServer.listen(common.PORT); - -var recv = ''; - -echoServer.on('listening', function() { - var j = 0; - var c = net.createConnection({ - port: common.PORT - }); - - c.setEncoding('binary'); - c.on('data', function(chunk) { - var n = j + chunk.length; - while (j < n && j < 256) { - c.write(String.fromCharCode(j), 'binary'); - j++; - } - if (j === 256) { - c.end(); - } - recv += chunk; - }); - - c.on('connect', function() { - c.write(binaryString, 'binary'); - }); - - c.on('close', function() { - echoServer.close(); - }); -}); - -process.on('exit', function() { - assert.equal(2 * 256, recv.length); - - var a = recv.split(''); - - var first = a.slice(0, 256).reverse().join(''); - - var second = a.slice(256, 2 * 256).join(''); - - assert.equal(first, second); -}); diff --git a/test/parallel/test-stream-writev.js b/test/parallel/test-stream-writev.js index 2aa992b4a9a785..1d6839f46e2771 100644 --- a/test/parallel/test-stream-writev.js +++ b/test/parallel/test-stream-writev.js @@ -59,7 +59,7 @@ function test(decode, uncork, multi, next) { { encoding: 'ascii', chunk: 'hello, ' }, { encoding: 'utf8', chunk: 'world' }, { encoding: 'buffer', chunk: [33] }, - { encoding: 'binary', chunk: '\nand then...' }, + { encoding: 'utf8', chunk: '\nand then...' }, { encoding: 'hex', chunk: 'facebea7deadbeefdecafbad' } ]; @@ -83,7 +83,7 @@ function test(decode, uncork, multi, next) { w.cork(); w.write(new Buffer('!'), 'buffer', cnt('!')); - w.write('\nand then...', 'binary', cnt('and then')); + w.write('\nand then...', 'utf8', cnt('and then')); if (multi) w.uncork(); diff --git a/test/parallel/test-stream2-writable.js b/test/parallel/test-stream2-writable.js index 81e57d06eb3bec..c17a81c3211af9 100644 --- a/test/parallel/test-stream2-writable.js +++ b/test/parallel/test-stream2-writable.js @@ -140,7 +140,6 @@ test('write bufferize', function(t) { 'utf8', 'utf-8', 'ascii', - 'binary', 'base64', 'ucs2', 'ucs-2', @@ -177,7 +176,6 @@ test('write no bufferize', function(t) { 'utf8', 'utf-8', 'ascii', - 'binary', 'base64', 'ucs2', 'ucs-2', @@ -275,7 +273,7 @@ test('encoding should be ignored for buffers', function(t) { t.end(); }; var buf = new Buffer(hex, 'hex'); - tw.write(buf, 'binary'); + tw.write(buf, 'utf8'); }); test('writables are not pipable', function(t) { diff --git a/test/parallel/test-stringbytes-external-at-max.js b/test/parallel/test-stringbytes-external-at-max.js index 5c6455e808f217..1d764c1d9dd1f8 100644 --- a/test/parallel/test-stringbytes-external-at-max.js +++ b/test/parallel/test-stringbytes-external-at-max.js @@ -28,5 +28,5 @@ try { return; } -const maxString = buf.toString('binary'); +const maxString = buf.toString('utf8'); assert.equal(maxString.length, kStringMaxLength); diff --git a/test/parallel/test-stringbytes-external.js b/test/parallel/test-stringbytes-external.js index f9bd78378e3414..6853a3c05a249a 100644 --- a/test/parallel/test-stringbytes-external.js +++ b/test/parallel/test-stringbytes-external.js @@ -11,7 +11,7 @@ var write_str = 'a'; // first do basic checks var b = new Buffer(write_str, 'ucs2'); -var c = b.toString('binary'); +var c = b.toString('utf8'); assert.equal(b[0], 0x61); assert.equal(b[1], 0); assert.equal(ucs2_control, c); @@ -29,12 +29,12 @@ for (let i = 0; i < b.length; i += 2) { assert.equal(b[i + 1], 0); } // create another string to create an external string -var b_bin = b.toString('binary'); +var b_bin = b.toString('utf8'); var b_ucs = b.toString('ucs2'); -// check control against external binary string +// check control against external utf8 string assert.equal(ucs2_control, b_bin); // create buffer copy from external -var c_bin = new Buffer(b_bin, 'binary'); +var c_bin = new Buffer(b_bin, 'utf8'); var c_ucs = new Buffer(b_ucs, 'ucs2'); // make sure they're the same length assert.equal(c_bin.length, c_ucs.length); @@ -44,8 +44,8 @@ for (let i = 0; i < c_bin.length; i++) { } // check resultant strings assert.equal(c_bin.toString('ucs2'), c_ucs.toString('ucs2')); -assert.equal(c_bin.toString('binary'), ucs2_control); -assert.equal(c_ucs.toString('binary'), ucs2_control); +assert.equal(c_bin.toString('utf8'), ucs2_control); +assert.equal(c_ucs.toString('utf8'), ucs2_control); // now let's test BASE64 and HEX ecoding/decoding diff --git a/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js b/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js index 0213140a5fe5e3..43bf0db826d196 100644 --- a/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js +++ b/test/sequential/test-stringbytes-external-exceed-max-by-1-binary.js @@ -29,13 +29,13 @@ try { } assert.throws(function() { - buf.toString('binary'); + buf.toString('utf8'); }, /"toString\(\)" failed/); -var maxString = buf.toString('binary', 1); +var maxString = buf.toString('utf8', 1); assert.equal(maxString.length, kStringMaxLength); // Free the memory early instead of at the end of the next assignment maxString = undefined; -maxString = buf.toString('binary', 0, kStringMaxLength); +maxString = buf.toString('utf8', 0, kStringMaxLength); assert.equal(maxString.length, kStringMaxLength);