Skip to content

Commit

Permalink
crypto: default output encoding to buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny committed Oct 19, 2012
1 parent c11c19b commit d917226
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 44 deletions.
40 changes: 20 additions & 20 deletions doc/api/crypto.markdown
Expand Up @@ -87,14 +87,14 @@ Returned by `crypto.createHash`.

Updates the hash content with the given `data`, the encoding of which is given
in `input_encoding` and can be `'buffer'`, `'utf8'`, `'ascii'` or `'binary'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.
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 `'buffer'`, `'hex'`, `'binary'` or `'base64'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

Note: `hash` object can not be used after `digest()` method been called.

Expand All @@ -121,7 +121,7 @@ This can be called many times with new data as it is streamed.

Calculates the digest of all of the passed data to the hmac.
The `encoding` can be `'buffer'`, `'hex'`, `'binary'` or `'base64'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

Note: `hmac` object can not be used after `digest()` method been called.

Expand Down Expand Up @@ -157,17 +157,17 @@ Returned by `crypto.createCipher` and `crypto.createCipheriv`.

Updates the cipher with `data`, the encoding of which is given in
`input_encoding` and can be `'buffer'`, `'utf8'`, `'ascii'` or `'binary'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

The `output_encoding` specifies the output format of the enciphered data,
and can be `'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
and can be `'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'buffer'`.

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:
`'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'binary'`.
`'buffer'`, `'binary'`, `'base64'` or `'hex'`. Defaults to `'buffer'`.

Note: `cipher` object can not be used after `final()` method been called.

Expand Down Expand Up @@ -197,18 +197,18 @@ Returned by `crypto.createDecipher` and `crypto.createDecipheriv`.
### decipher.update(data, [input_encoding], [output_encoding])

Updates the decipher with `data`, which is encoded in `'buffer'`, `'binary'`,
`'base64'` or `'hex'`. Defaults to `'binary'`.
`'base64'` or `'hex'`. Defaults to `'buffer'`.

The `output_decoding` specifies in what format to return the deciphered
plaintext: `'buffer'`, `'binary'`, `'ascii'` or `'utf8'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

### decipher.final([output_encoding])

Returns any remaining plaintext which is deciphered,
with `output_encoding` being one of: `'buffer'`, `'binary'`, `'ascii'` or
`'utf8'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

Note: `decipher` object can not be used after `final()` method been called.

Expand Down Expand Up @@ -241,7 +241,7 @@ 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 `'buffer'`, `'binary'`,
`'hex'` or `'base64'`. Defaults to `'binary'`.
`'hex'` or `'base64'`. Defaults to `'buffer'`.

Note: `signer` object can not be used after `sign()` method been called.

Expand All @@ -267,7 +267,7 @@ 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 `'buffer'`,
`'binary'`, `'hex'` or `'base64'`. Defaults to `'binary'`.
`'binary'`, `'hex'` or `'base64'`. Defaults to `'buffer'`.

Returns true or false depending on the validity of the signature for the data and public key.

Expand All @@ -283,7 +283,7 @@ given bit length. The generator used is `2`.
Creates a Diffie-Hellman key exchange object using the supplied prime. The
generator used is `2`. Encoding can be `'buffer'`, `'binary'`, `'hex'`, or
`'base64'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

## Class: DiffieHellman

Expand All @@ -296,46 +296,46 @@ Returned by `crypto.createDiffieHellman`.
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'`, or `'base64'`.
Defaults to `'binary'`.
Defaults to `'buffer'`.

### diffieHellman.computeSecret(other_public_key, [input_encoding], [output_encoding])

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 `'buffer'`, `'binary'`, `'hex'`,
or `'base64'`. The input encoding defaults to `'binary'`.
or `'base64'`. The input encoding defaults to `'buffer'`.
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
`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'buffer'`.

### diffieHellman.getGenerator([encoding])

Returns the Diffie-Hellman prime in the specified encoding, which can be
`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
`'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'buffer'`.

### diffieHellman.getPublicKey([encoding])

Returns the Diffie-Hellman public key in the specified encoding, which can
be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
be `'binary'`, `'hex'`, or `'base64'`. Defaults to `'buffer'`.

### diffieHellman.getPrivateKey([encoding])

Returns the Diffie-Hellman private key in the specified encoding, which can
be `'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'binary'`.
be `'buffer'`, `'binary'`, `'hex'`, or `'base64'`. Defaults to `'buffer'`.

### diffieHellman.setPublicKey(public_key, [encoding])

Sets the Diffie-Hellman public key. Key encoding can be `'buffer', ``'binary'`,
`'hex'` or `'base64'`. Defaults to `'binary'`.
`'hex'` or `'base64'`. Defaults to `'buffer'`.

### diffieHellman.setPrivateKey(public_key, [encoding])

Sets the Diffie-Hellman private key. Key encoding can be `'buffer'`, `'binary'`,
`'hex'` or `'base64'`. Defaults to `'binary'`.
`'hex'` or `'base64'`. Defaults to `'buffer'`.

## crypto.getDiffieHellman(group_name)

Expand Down
34 changes: 17 additions & 17 deletions src/node_crypto.cc
Expand Up @@ -2382,7 +2382,7 @@ class Cipher : public ObjectWrap {
Local<Value> outString;
char* out_hexdigest;
int out_hex_len;
enum encoding out_enc = ParseEncoding(args[2], BINARY);
enum encoding out_enc = ParseEncoding(args[2], BUFFER);
if (out_enc == HEX) {
// Hex encoding
HexEncode(out, out_len, &out_hexdigest, &out_hex_len);
Expand Down Expand Up @@ -2466,7 +2466,7 @@ class Cipher : public ObjectWrap {
}
}

enum encoding enc = ParseEncoding(args[0], BINARY);
enum encoding enc = ParseEncoding(args[0], BUFFER);
if (enc == HEX) {
// Hex encoding
HexEncode(out_value, out_len, &out_hexdigest, &out_hex_len);
Expand Down Expand Up @@ -2784,7 +2784,7 @@ class Decipher : public ObjectWrap {
char* ciphertext;
int ciphertext_len;

enum encoding enc = ParseEncoding(args[1], BINARY);
enum encoding enc = ParseEncoding(args[1], BUFFER);
if (enc == HEX) {
// Hex encoding
// Do we have a previous hex carry over?
Expand Down Expand Up @@ -2841,7 +2841,7 @@ class Decipher : public ObjectWrap {
}

Local<Value> outString;
enum encoding out_enc = ParseEncoding(args[2], BINARY);
enum encoding out_enc = ParseEncoding(args[2], BUFFER);
if (out_enc == UTF8) {
// See if we have any overhang from last utf8 partial ending
if (cipher->incomplete_utf8!=NULL) {
Expand Down Expand Up @@ -2909,9 +2909,9 @@ class Decipher : public ObjectWrap {
}

if (args.Length() == 0 || !args[0]->IsString()) {
outString = Encode(out_value, out_len, BINARY);
outString = Encode(out_value, out_len, BUFFER);
} else {
enum encoding enc = ParseEncoding(args[0], BINARY);
enum encoding enc = ParseEncoding(args[0], BUFFER);
if (enc == UTF8) {
// See if we have any overhang from last utf8 partial ending
if (cipher->incomplete_utf8!=NULL) {
Expand Down Expand Up @@ -3114,7 +3114,7 @@ class Hmac : public ObjectWrap {
md_len = 0;
}

enum encoding enc = ParseEncoding(args[0], BINARY);
enum encoding enc = ParseEncoding(args[0], BUFFER);
if (enc == HEX) {
// Hex encoding
HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len);
Expand Down Expand Up @@ -3261,7 +3261,7 @@ class Hash : public ObjectWrap {

Local<Value> outString;

enum encoding enc = ParseEncoding(args[0], BINARY);
enum encoding enc = ParseEncoding(args[0], BUFFER);
if (enc == HEX) {
// Hex encoding
char* md_hexdigest;
Expand Down Expand Up @@ -3466,7 +3466,7 @@ class Sign : public ObjectWrap {

delete [] buf;

enum encoding enc = ParseEncoding(args[1], BINARY);
enum encoding enc = ParseEncoding(args[1], BUFFER);
if (enc == HEX) {
// Hex encoding
HexEncode(md_value, md_len, &md_hexdigest, &md_hex_len);
Expand Down Expand Up @@ -3717,7 +3717,7 @@ class Verify : public ObjectWrap {

int r=-1;

enum encoding enc = ParseEncoding(args[2], BINARY);
enum encoding enc = ParseEncoding(args[2], BUFFER);
if (enc == HEX) {
// Hex encoding
HexDecode(hbuf, hlen, (char **)&dbuf, &dlen);
Expand Down Expand Up @@ -3927,7 +3927,7 @@ class DiffieHellman : public ObjectWrap {
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}
delete[] data;

Expand All @@ -3953,7 +3953,7 @@ class DiffieHellman : public ObjectWrap {
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}

delete[] data;
Expand All @@ -3980,7 +3980,7 @@ class DiffieHellman : public ObjectWrap {
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}

delete[] data;
Expand Down Expand Up @@ -4013,7 +4013,7 @@ class DiffieHellman : public ObjectWrap {
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}

delete[] data;
Expand Down Expand Up @@ -4046,7 +4046,7 @@ class DiffieHellman : public ObjectWrap {
if (args.Length() > 0 && args[0]->IsString()) {
outString = EncodeWithEncoding(args[0], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}

delete[] data;
Expand Down Expand Up @@ -4137,7 +4137,7 @@ class DiffieHellman : public ObjectWrap {
} else if (args.Length() > 1 && args[1]->IsString()) {
outString = EncodeWithEncoding(args[1], data, dataSize);
} else {
outString = Encode(data, dataSize, BINARY);
outString = Encode(data, dataSize, BUFFER);
}
}

Expand Down Expand Up @@ -4284,7 +4284,7 @@ class DiffieHellman : public ObjectWrap {
} else if (enc == BASE64) {
unbase64((unsigned char*)*buf, len, &retbuf, &retlen);

} else if (enc == BINARY) {
} else if (enc == BINARY || enc == BUFFER) {
// Binary - do nothing
} else {
fprintf(stderr, "node-crypto : Diffie-Hellman parameter encoding "
Expand Down
14 changes: 7 additions & 7 deletions test/simple/test-crypto.js
Expand Up @@ -368,7 +368,7 @@ for (var i = 0, l = rfc2202_sha1.length; i < l; i++) {
var a0 = crypto.createHash('sha1').update('Test123').digest('hex');
var a1 = crypto.createHash('md5').update('Test123').digest('binary');
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('binary');
var a4 = crypto.createHash('sha1').update('Test123').digest('buffer');

assert.equal(a0, '8308651804facb7b9af8ffc53a33a22d6a1c8ac2', 'Test SHA1');
Expand Down Expand Up @@ -421,11 +421,11 @@ assert.strictEqual(verified, true, 'sign and verify (base 64)');

var s2 = crypto.createSign('RSA-SHA256')
.update('Test123')
.sign(keyPem); // binary
.sign(keyPem, 'binary');
var verified = crypto.createVerify('RSA-SHA256')
.update('Test')
.update('123')
.verify(certPem, s2); // binary
.verify(certPem, s2, 'binary');
assert.strictEqual(verified, true, 'sign and verify (binary)');

var s3 = crypto.createSign('RSA-SHA1')
Expand Down Expand Up @@ -554,10 +554,10 @@ 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());
assert.deepEqual(dh1.getPrime(), dh3.getPrime());
assert.deepEqual(dh1.getGenerator(), dh3.getGenerator());
assert.deepEqual(dh1.getPublicKey(), dh3.getPublicKey());
assert.deepEqual(dh1.getPrivateKey(), dh3.getPrivateKey());

var secret3 = dh3.computeSecret(key2, 'hex', 'base64');

Expand Down

0 comments on commit d917226

Please sign in to comment.