Permalink
Browse files

crypto: bring module into modern age

Introduce 'buffer' encoding, allow returning and giving buffers as
arguments of 'crypto' routines.

Fix #3278
  • Loading branch information...
1 parent 3301c90 commit 63ff449d87e23b5e3d475da960135c1a2fd0ed58 @indutny indutny committed Sep 12, 2012
Showing with 173 additions and 142 deletions.
  1. +27 −24 doc/api/crypto.markdown
  2. +10 −2 src/node.cc
  3. +1 −1 src/node.h
  4. +96 −109 src/node_crypto.cc
  5. +2 −2 test/simple/test-crypto-padding-aes256.js
  6. +37 −4 test/simple/test-crypto.js
View
@@ -64,14 +64,14 @@ Returned by `crypto.createHash`.
### 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'`.
+in `input_encoding` and can be `'buffer'`, `'utf8'`, `'ascii'` or `'binary'`.
Defaults to `'binary'`.
This can be called many times with new data as it is streamed.
### hash.digest([encoding])
Calculates the digest of all of the passed data to be hashed.
-The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
+The `encoding` can be `'buffer'`, `'hex'`, `'binary'` or `'base64'`.
Defaults to `'binary'`.
Note: `hash` object can not be used after `digest()` method been called.
@@ -98,7 +98,7 @@ This can be called many times with new data as it is streamed.
### hmac.digest([encoding])
Calculates the digest of all of the passed data to the hmac.
-The `encoding` can be `'hex'`, `'binary'` or `'base64'`.
+The `encoding` can be `'buffer'`, `'hex'`, `'binary'` or `'base64'`.
Defaults to `'binary'`.
Note: `hmac` object can not be used after `digest()` method been called.
@@ -134,18 +134,18 @@ Returned by `crypto.createCipher` and `crypto.createCipheriv`.
### cipher.update(data, [input_encoding], [output_encoding])
Updates the cipher with `data`, the encoding of which is given in
-`input_encoding` and can be `'utf8'`, `'ascii'` or `'binary'`.
+`input_encoding` and can be `'buffer'`, `'utf8'`, `'ascii'` or `'binary'`.
Defaults to `'binary'`.
The `output_encoding` specifies the output format of the enciphered data,
-and can be `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
+and can be `'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
Returns the enciphered contents, and can be called many times with new data as it is streamed.
### cipher.final([output_encoding])
Returns any remaining enciphered contents, with `output_encoding` being one of:
-`'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
+`'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
Note: `cipher` object can not be used after `final()` method been called.
@@ -174,16 +174,18 @@ Returned by `crypto.createDecipher` and `crypto.createDecipheriv`.
### decipher.update(data, [input_encoding], [output_encoding])
-Updates the decipher with `data`, which is encoded in `'binary'`, `'base64'`
-or `'hex'`. Defaults to `'binary'`.
+Updates the decipher with `data`, which is encoded in `'buffer'`, `'binary'`,
+`'base64'` or `'hex'`. Defaults to `'binary'`.
The `output_decoding` specifies in what format to return the deciphered
-plaintext: `'binary'`, `'ascii'` or `'utf8'`. Defaults to `'binary'`.
+plaintext: `'buffer'`, `'binary'`, `'ascii'` or `'utf8'`.
+Defaults to `'binary'`.
### decipher.final([output_encoding])
Returns any remaining plaintext which is deciphered,
-with `output_encoding` being one of: `'binary'`, `'ascii'` or `'utf8'`.
+with `output_encoding` being one of: `'buffer'`, `'binary'`, `'ascii'` or
+`'utf8'`.
Defaults to `'binary'`.
Note: `decipher` object can not be used after `final()` method been called.
@@ -216,8 +218,8 @@ This can be called many times with new data as it is streamed.
Calculates the signature on all the updated data passed through the signer.
`private_key` is a string containing the PEM encoded private key for signing.
-Returns the signature in `output_format` which can be `'binary'`, `'hex'` or
-`'base64'`. Defaults to `'binary'`.
+Returns the signature in `output_format` which can be `'buffer'`, `'binary'`,
+`'hex'` or `'base64'`. Defaults to `'binary'`.
Note: `signer` object can not be used after `sign()` method been called.
@@ -242,8 +244,8 @@ This can be called many times with new data as it is streamed.
Verifies the signed data by using the `object` and `signature`. `object` is a
string containing a PEM encoded object, which can be one of RSA public key,
DSA public key, or X.509 certificate. `signature` is the previously calculated
-signature for the data, in the `signature_format` which can be `'binary'`,
-`'hex'` or `'base64'`. Defaults to `'binary'`.
+signature for the data, in the `signature_format` which can be `'buffer'`,
+`'binary'`, `'hex'` or `'base64'`. Defaults to `'binary'`.
Returns true or false depending on the validity of the signature for the data and public key.
@@ -257,7 +259,8 @@ given bit length. The generator used is `2`.
## crypto.createDiffieHellman(prime, [encoding])
Creates a Diffie-Hellman key exchange object using the supplied prime. The
-generator used is `2`. Encoding can be `'binary'`, `'hex'`, or `'base64'`.
+generator used is `2`. Encoding can be `'buffer'`, `'binary'`, `'hex'`, or
+`'base64'`.
Defaults to `'binary'`.
## Class: DiffieHellman
@@ -278,19 +281,19 @@ Defaults to `'binary'`.
Computes the shared secret using `other_public_key` as the other party's
public key and returns the computed shared secret. Supplied key is
interpreted using specified `input_encoding`, and secret is encoded using
-specified `output_encoding`. Encodings can be `'binary'`, `'hex'`, or
-`'base64'`. The input encoding defaults to `'binary'`.
+specified `output_encoding`. Encodings can be `'buffer'`, `'binary'`, `'hex'`,
+or `'base64'`. The input encoding defaults to `'binary'`.
If no output encoding is given, the input encoding is used as output encoding.
### diffieHellman.getPrime([encoding])
Returns the Diffie-Hellman prime in the specified encoding, which can be
-`'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
+`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
### diffieHellman.getGenerator([encoding])
Returns the Diffie-Hellman prime in the specified encoding, which can be
-`'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
+`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
### diffieHellman.getPublicKey([encoding])
@@ -300,17 +303,17 @@ be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
### diffieHellman.getPrivateKey([encoding])
Returns the Diffie-Hellman private key in the specified encoding, which can
-be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
+be `'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
### diffieHellman.setPublicKey(public_key, [encoding])
-Sets the Diffie-Hellman public key. Key encoding can be `'binary'`, `'hex'`,
-or `'base64'`. Defaults to `'binary'`.
+Sets the Diffie-Hellman public key. Key encoding can be `'buffer', ``'binary'`,
+`'hex'` or `'base64'`. Defaults to `'binary'`.
### diffieHellman.setPrivateKey(public_key, [encoding])
-Sets the Diffie-Hellman private key. Key encoding can be `'binary'`, `'hex'`,
-or `'base64'`. Defaults to `'binary'`.
+Sets the Diffie-Hellman private key. Key encoding can be `'buffer'`, `'binary'`,
+`'hex'` or `'base64'`. Defaults to `'binary'`.
## crypto.getDiffieHellman(group_name)
View
@@ -1067,6 +1067,8 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
return UCS2;
} else if (strcasecmp(*encoding, "binary") == 0) {
return BINARY;
+ } else if (strcasecmp(*encoding, "buffer") == 0) {
+ return BUFFER;
} else if (strcasecmp(*encoding, "hex") == 0) {
return HEX;
} else if (strcasecmp(*encoding, "raw") == 0) {
@@ -1089,6 +1091,11 @@ enum encoding ParseEncoding(Handle<Value> encoding_v, enum encoding _default) {
Local<Value> Encode(const void *buf, size_t len, enum encoding encoding) {
HandleScope scope;
+ if (encoding == BUFFER) {
+ return scope.Close(
+ Buffer::New(static_cast<const char*>(buf), len)->handle_);
+ }
+
if (!len) return scope.Close(String::Empty());
if (encoding == BINARY) {
@@ -1119,7 +1126,7 @@ ssize_t DecodeBytes(v8::Handle<v8::Value> val, enum encoding encoding) {
return -1;
}
- if (encoding == BINARY && Buffer::HasInstance(val)) {
+ if ((encoding == BUFFER || encoding == BINARY) && Buffer::HasInstance(val)) {
return Buffer::Length(val->ToObject());
}
@@ -1158,7 +1165,8 @@ ssize_t DecodeWrite(char *buf,
bool is_buffer = Buffer::HasInstance(val);
- if (is_buffer && encoding == BINARY) { // fast path, copy buffer data
+ if (is_buffer && (encoding == BINARY || encoding == BUFFER)) {
+ // fast path, copy buffer data
const char* data = Buffer::Data(val.As<Object>());
size_t size = Buffer::Length(val.As<Object>());
size_t len = size < buflen ? size : buflen;
View
@@ -127,7 +127,7 @@ void SetPrototypeMethod(target_t target,
#define NODE_SET_METHOD node::SetMethod
#define NODE_SET_PROTOTYPE_METHOD node::SetPrototypeMethod
-enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX};
+enum encoding {ASCII, UTF8, BASE64, UCS2, BINARY, HEX, BUFFER};
enum encoding ParseEncoding(v8::Handle<v8::Value> encoding_v,
enum encoding _default = BINARY);
NODE_EXTERN void FatalException(v8::TryCatch &try_catch);
Oops, something went wrong.

0 comments on commit 63ff449

Please sign in to comment.