Permalink
Browse files

optional encrypt args, pass buffer to callback

  • Loading branch information...
1 parent 34d10c0 commit 22e74a6cd66f13bb9f607bc026973008a9dd278f @freewil freewil committed Aug 16, 2012
Showing with 121 additions and 9 deletions.
  1. +1 −0 .gitignore
  2. +35 −7 lib/gpg.js
  3. +13 −2 package.json
  4. +39 −0 test/index.js
  5. +33 −0 test/mykey1.key
View
1 .gitignore
@@ -1 +1,2 @@
.DS_Store
+node_modules/
View
42 lib/gpg.js
@@ -81,22 +81,50 @@ var GPG = {
* Encrypt string `str` and pass the encrypted version to the callback `fn`.
*
* @param {String} str
- * @param {Function} Optional callback containing the encrypted string contents.
+ * @param {Function} Optional callback containing the encrypted buffer.
* @api public
*/
- encrypt: function(str, fn){
- var buffer = new Buffer('')
- , gpg = spawn('gpg', ['--encrypt']);
+ encrypt: function(str, args, fn){
+ var defaultArgs = ['--encrypt'];
+ if (typeof args === 'function'){
+ fn = args;
+ args = defaultArgs;
+ } else {
+ args = defaultArgs.concat(args);
+ }
- gpg.stdout.on('data', function (data){
- buffer.write(data.toString('utf8'));
+ var buffers = []
+ , buffersLength = 0
+ , errors = ''
+ , gpg = spawn('gpg', args);
+
+ gpg.stdout.on('data', function (buf){
+ buffers.push(buf);
+ buffersLength += buf.length;
+ });
+
+ gpg.stderr.on('data', function(buf){
+ errors += buf.toString('utf8');
});
+
gpg.on('exit', function (code){
- fn.call(null, null, buffer.toString('utf8'));
+ if (errors.length) {
+ return fn.call(null, new Error(errors));
+ }
+
+ // concatenate all buffers together
+ var buffer = new Buffer(buffersLength)
+ , targetStart = 0;
+ buffers.forEach(function(b) {
+ b.copy(buffer, targetStart);
+ targetStart += b.length
+ });
+ fn.call(null, null, buffer);
});
gpg.stdin.write(str);
+ gpg.stdin.end();
},
/**
View
15 package.json
@@ -7,11 +7,22 @@
"type": "git",
"url": "git://github.com/drudge/node-gpg.git"
},
- "keywords": ["gpg", "encrypt", "decrypt", "pgp", "gnupg"],
+ "keywords": [
+ "gpg",
+ "encrypt",
+ "decrypt",
+ "pgp",
+ "gnupg"
+ ],
"main": "./lib/gpg",
"engines": {
"node": ">= 0.4.0"
},
"dependencies": {},
- "devDependencies": {}
+ "devDependencies": {
+ "mocha": "~1.3.2"
+ },
+ "scripts": {
+ "test": "mocha"
+ }
}
View
39 test/index.js
@@ -0,0 +1,39 @@
+var assert = require('assert')
+ , gpg = require('../');
+
+describe('gpg', function(){
+
+ describe('encrypt', function(){
+ it('should encrypt data', function(done){
+ var mysecret = 'Hello World';
+ var args = [
+ '--no-use-agent'
+ , '--batch'
+ , '--default-key', 'D5762441'
+ , '--recipient', 'D5762441'
+ ];
+ gpg.encrypt(mysecret, args, function(err, encrypted){
+ assert.ifError(err);
+ assert.ok(encrypted.length);
+ done();
+ });
+ });
+ });
+
+ describe('clearsign', function(){
+ it('should clearsign data', function(done){
+ var mymessage = 'Hello, this is me!';
+ var args = [
+ '--no-use-agent'
+ , '--batch'
+ , '--default-key', 'D5762441'
+ ];
+ gpg.clearsign(mymessage, args, function(err, clearsigned){
+ assert.ifError(err);
+ assert.ok(clearsigned.length);
+ done();
+ });
+ });
+ });
+
+});
View
33 test/mykey1.key
@@ -0,0 +1,33 @@
+-----BEGIN PGP PRIVATE KEY BLOCK-----
+Version: GnuPG v1.4.11 (GNU/Linux)
+
+lQHYBFAtVlgBBADMBAtPKrZ8z6OfEX7iWDt89IjKzTVxRbrBCVQmId/d1iulX+iy
+i/t0rIIn23hlGC33S6jDK86IUEyXNlQBE/EkwW+w5ZBugDKbZCRiP0fTLnjJ0P5a
+FUFZqdzOTGQS4szlAU4jT4ceCLFuy7Azga8mMl0fQS84nqsGoOSIQY6u8QARAQAB
+AAP+JNOeDv5QzprUmEQaOVVf96BUiPcd7IJ/wZSjgfafaHXejw2O/ZvTGi6BmBw/
+0U/aDEe2APUJZQXJrYn7A+7LmloKxuyGGn3w6z4VI0b6P3+LH6dHztqs6VfUXim0
+WxWHP6dbxsAg9/k8nWY2AcQC1pyxtaVn+1XTQkYu4Ss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+XIZAF1t1v0V/j9OFHXXFqpeql5iYAGdXVOV0FDSpSIw0CQpC6l3qSSEB/0PozKxp
+9qjTV53vH6l56BmDIzfV9J9qThV+BRpUltEcUIoqIJl41rzsNbM8yKPJ3ucT4gUe
+6QBe4j6ryuJBI5SeTIifBBgBAgAJBQJQLVZYAhsMAAoJEGEk/2vVdiRBe0MD/0Rp
+AqKLZTE1buir/jKbv6zXzEchMuv4MksIQ0oCDKTUPiyD33BxgSziwSANSkM+RZVL
+RQZAq4yeGAlakfwHYSAGJC4BUQAyzAdNPfcEqlsfq/nL6RHKHHD+fb7V8IAV59WU
+x+Ncqh8mgzjs+jY7FExVX4tONse4+IylWouo1dNK
+=LZNd
+-----END PGP PRIVATE KEY BLOCK-----

0 comments on commit 22e74a6

Please sign in to comment.