Buffer for Cipher, Decipher, Hmac, Sign and Verify
This commit is contained in:
parent
cb97cdb256
commit
e0d6f14b22
3
TODO
3
TODO
@ -6,9 +6,6 @@
|
|||||||
- tab completion interface
|
- tab completion interface
|
||||||
- SSL should be factored out of net.js into standalone stream object
|
- SSL should be factored out of net.js into standalone stream object
|
||||||
- base64 write and toString for buffers
|
- base64 write and toString for buffers
|
||||||
- node_crypto, support for Buffer in update function of Cipher, Decipher,
|
|
||||||
Hmac, Sign, Verify. See 9a26946aaa99a3f27905d5ba91220784e864e5d0 for an
|
|
||||||
example of how to do it.
|
|
||||||
|
|
||||||
- debug and production modes
|
- debug and production modes
|
||||||
- EventSource branch merged
|
- EventSource branch merged
|
||||||
|
@ -998,15 +998,18 @@ class Cipher : public ObjectWrap {
|
|||||||
return ThrowException(exception);
|
return ThrowException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
unsigned char *out=0;
|
||||||
|
int out_len=0;
|
||||||
|
if (Buffer::HasInstance(args[0])) {
|
||||||
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
|
int r = cipher->CipherUpdate(buffer->data(), buffer->length(), &out, &out_len);
|
||||||
|
} else {
|
||||||
char* buf = new char[len];
|
char* buf = new char[len];
|
||||||
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
||||||
assert(written == len);
|
assert(written == len);
|
||||||
|
|
||||||
unsigned char *out=0;
|
|
||||||
int out_len=0;
|
|
||||||
int r = cipher->CipherUpdate(buf, len,&out,&out_len);
|
int r = cipher->CipherUpdate(buf, len,&out,&out_len);
|
||||||
|
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
}
|
||||||
|
|
||||||
Local<Value> outString;
|
Local<Value> outString;
|
||||||
if (out_len==0) {
|
if (out_len==0) {
|
||||||
@ -1337,8 +1340,20 @@ class Decipher : public ObjectWrap {
|
|||||||
"node`DecodeBytes() failed")));
|
"node`DecodeBytes() failed")));
|
||||||
}
|
}
|
||||||
|
|
||||||
char* buf = new char[len];
|
char* buf;
|
||||||
|
// if alloc_buf then buf must be deleted later
|
||||||
|
bool alloc_buf = false;
|
||||||
|
if (Buffer::HasInstance(args[0])) {
|
||||||
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
|
buf = buffer->data();
|
||||||
|
len = buffer->length();
|
||||||
|
} else {
|
||||||
|
alloc_buf = true;
|
||||||
|
buf = new char[len];
|
||||||
ssize_t written = DecodeWrite(buf, len, args[0], BINARY);
|
ssize_t written = DecodeWrite(buf, len, args[0], BINARY);
|
||||||
|
assert(written == len);
|
||||||
|
}
|
||||||
|
|
||||||
char* ciphertext;
|
char* ciphertext;
|
||||||
int ciphertext_len;
|
int ciphertext_len;
|
||||||
|
|
||||||
@ -1353,7 +1368,10 @@ class Decipher : public ObjectWrap {
|
|||||||
char* complete_hex = new char[len+2];
|
char* complete_hex = new char[len+2];
|
||||||
memcpy(complete_hex, &cipher->incomplete_hex, 1);
|
memcpy(complete_hex, &cipher->incomplete_hex, 1);
|
||||||
memcpy(complete_hex+1, buf, len);
|
memcpy(complete_hex+1, buf, len);
|
||||||
|
if (alloc_buf) {
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
alloc_buf = false;
|
||||||
|
}
|
||||||
buf = complete_hex;
|
buf = complete_hex;
|
||||||
len += 1;
|
len += 1;
|
||||||
}
|
}
|
||||||
@ -1366,14 +1384,19 @@ class Decipher : public ObjectWrap {
|
|||||||
}
|
}
|
||||||
HexDecode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
|
HexDecode((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
|
||||||
|
|
||||||
|
if (alloc_buf) {
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
alloc_buf = false;
|
||||||
|
}
|
||||||
buf = ciphertext;
|
buf = ciphertext;
|
||||||
len = ciphertext_len;
|
len = ciphertext_len;
|
||||||
|
|
||||||
} else if (strcasecmp(*encoding, "base64") == 0) {
|
} else if (strcasecmp(*encoding, "base64") == 0) {
|
||||||
unbase64((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
|
unbase64((unsigned char*)buf, len, (char **)&ciphertext, &ciphertext_len);
|
||||||
|
if (alloc_buf) {
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
alloc_buf = false;
|
||||||
|
}
|
||||||
buf = ciphertext;
|
buf = ciphertext;
|
||||||
len = ciphertext_len;
|
len = ciphertext_len;
|
||||||
|
|
||||||
@ -1426,7 +1449,7 @@ class Decipher : public ObjectWrap {
|
|||||||
|
|
||||||
if (out) delete [] out;
|
if (out) delete [] out;
|
||||||
|
|
||||||
delete [] buf;
|
if (alloc_buf) delete [] buf;
|
||||||
return scope.Close(outString);
|
return scope.Close(outString);
|
||||||
|
|
||||||
}
|
}
|
||||||
@ -1640,13 +1663,16 @@ class Hmac : public ObjectWrap {
|
|||||||
return ThrowException(exception);
|
return ThrowException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if( Buffer::HasInstance(args[0])) {
|
||||||
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
|
int r = hmac->HmacUpdate(buffer->data(), buffer->length());
|
||||||
|
} else {
|
||||||
char* buf = new char[len];
|
char* buf = new char[len];
|
||||||
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
||||||
assert(written == len);
|
assert(written == len);
|
||||||
|
|
||||||
int r = hmac->HmacUpdate(buf, len);
|
int r = hmac->HmacUpdate(buf, len);
|
||||||
|
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
}
|
||||||
|
|
||||||
return args.This();
|
return args.This();
|
||||||
}
|
}
|
||||||
@ -1954,13 +1980,16 @@ class Sign : public ObjectWrap {
|
|||||||
return ThrowException(exception);
|
return ThrowException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (Buffer::HasInstance(args[0])) {
|
||||||
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
|
int r = sign->SignUpdate(buffer->data(), buffer->length());
|
||||||
|
} else {
|
||||||
char* buf = new char[len];
|
char* buf = new char[len];
|
||||||
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
||||||
assert(written == len);
|
assert(written == len);
|
||||||
|
|
||||||
int r = sign->SignUpdate(buf, len);
|
int r = sign->SignUpdate(buf, len);
|
||||||
|
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
}
|
||||||
|
|
||||||
return args.This();
|
return args.This();
|
||||||
}
|
}
|
||||||
@ -2150,13 +2179,16 @@ class Verify : public ObjectWrap {
|
|||||||
return ThrowException(exception);
|
return ThrowException(exception);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if(Buffer::HasInstance(args[0])) {
|
||||||
|
Buffer *buffer = ObjectWrap::Unwrap<Buffer>(args[0]->ToObject());
|
||||||
|
int r = verify->VerifyUpdate(buffer->data(), buffer->length());
|
||||||
|
} else {
|
||||||
char* buf = new char[len];
|
char* buf = new char[len];
|
||||||
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
ssize_t written = DecodeWrite(buf, len, args[0], enc);
|
||||||
assert(written == len);
|
assert(written == len);
|
||||||
|
|
||||||
int r = verify->VerifyUpdate(buf, len);
|
int r = verify->VerifyUpdate(buf, len);
|
||||||
|
|
||||||
delete [] buf;
|
delete [] buf;
|
||||||
|
}
|
||||||
|
|
||||||
return args.This();
|
return args.This();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user