chrisa/node-rsa — Issue #6: Return buffer object from RsaKeypair.{en,de}crypt()?
Author: danfuzz
Created: 2012-01-30 22:43:30+00:00
State: open

The Node (0.6.8) documentation for Buffer says this of the "binary" encoding:

> `'binary'` - A way of encoding raw binary data into strings by using only the first 8 bits of each character. This encoding method is deprecated and should be avoided in favor of `Buffer` objects where possible. This encoding will be removed in future versions of Node.

To be prepared for that eventuality, I think it makes sense to make the methods on RsaKeypair optionally return a buffer instead of a string. Maybe use the (pseudo) encoding name "buffer" to trigger that? E.g.:

```javascript
var kp = rsa.createRsaKeypair({ ... });
var buf = rsa.encrypt(someData, "base64", "buffer"); // would return a buffer object
```

Similarly, I think it makes sense to accept buffers as the first argument, and it looks like (perhaps by happenstance) this is already partially supported. In particular, the first argument to encrypt() can already be a buffer. E.g., this works:

```javascript
var encrypted = kp.encrypt(new Buffer("foo", "utf8"), "binary", "base64");
var decrypted = kp.decrypt(encrypted, "base64", "utf8"); // returns "foo"
```

Unfortunately, it looks like decrypt() doesn't already deal. This fails with an error in SSL:

```javascript
var decrypted = kp.decrypt(new Buffer(encrypted, "base64"), "binary", "utf8");
```

I may have time to work on patches for this in the near term (but I'm not 100% sure). In any case, I'd love to hear what you think. Thanks for your consideration!