Permalink
Browse files

crypto: bring module into modern age

Return and use Buffers everywhere by default
  • Loading branch information...
1 parent 3806cf0 commit b3e4a60c734ed41804879e7ee865be788acb2334 @indutny committed Sep 12, 2012
Showing with 150 additions and 122 deletions.
  1. +4 −4 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,7 +64,7 @@ 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 `'utf8'`, `'ascii'`, `'buffer'` or `'binary'`.
Defaults to `'binary'`.
This can be called many times with new data as it is streamed.
@@ -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 `'utf8'`, `'ascii'`, `'buffer'` 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 `'binary'`, `'base64'`, `'buffer'` 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'`.
+`'binary'`, `'base64'`, `'buffer'` or `'hex'`. Defaults to `'binary'`.
Note: `cipher` object can not be used after `final()` method been called.
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(reinterpret_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 b3e4a60

Please sign in to comment.