dominictarr/pull-crypto — pull request #3 by swys (2013-12-15 09:46:12+00:00)
Title: output buffers by default

Hey! Thanks for merging before, was better for me to make those changes anyway, aside from any networking woes:)

I just modified the `encrypt`/`decrypt` methods to default to buffers if no `encoding` is set.

This will slightly change the API, though there is no actual harm. This makes it so the previous `inputEncoding` options are ignored. The `inputEncoding` option was necessary before because I was using the `update` and `final` methods on the `cipher` objects.

But this update now uses the `cipher` objects as streams. I figured since the node api docs label these methods as [`legacy`](http://nodejs.org/api/crypto.html#crypto_crypto_createcipher_algorithm_password) and streaming is more fun, I might as well use the streaming api.

Now you just have to set the encoding if you want to get back something other than a `buffer`. You can still pass a buffer in and if no encoding is set you'll get a buffer out. So you can do things like pass in a buffer to `encrypt`, set the encoding as `hex` and you'll get an encrypted `hex` string. You can store the `hex` and then later when you need to decrypt it, you can pass the `hex` to the `decrypt` method and you'll get back whatever encoding you set for the `decrypt.encoding` or you'll get a buffer if you didn't set anything.

You can do this because under the hood the `decrypt` method will take in anything you give it and convert it to a `buffer`. I found that when using the streaming api you must pass in a `buffer` to the `decipher` object or it will not be able to decrypt. So that means it will buffer all the data until your done, then convert it to a buffer using `bops.join` and then pass it to the `decipher` stream.

Also, I didn't realize that node uses `slow buffers` for the `cipher` object. Did you notice this or I am doing something wrong?

Let me know what you think.

Thanks!