Permalink
Browse files

Initial commit

  • Loading branch information...
0 parents commit f6f2ef4f76b24c362e36692ab469eb66db08e8ac Nicholas Penree committed Jun 5, 2011
Showing with 285 additions and 0 deletions.
  1. +1 −0 .gitignore
  2. +43 −0 README.markdown
  3. +15 −0 examples/decrypt.js
  4. +15 −0 examples/encrypt.js
  5. +7 −0 index.js
  6. +187 −0 lib/gpg.js
  7. +17 −0 package.json
@@ -0,0 +1 @@
+.DS_Store
@@ -0,0 +1,43 @@
+# GPG Encryption/Decryption in Node.js
+
+ This module adds GPG encryption and decryption in node.js by way of the gpg command-line tool.
+
+## Requirements
+
+In order to use the GPG encryption, you'll need to have the `gpg` binary in your $PATH.
+
+## Installation
+
+ npm install gpg
+
+## Contributors
+
+The following are the major contributors of `node-gpg` (in no specific order).
+
+ * Nicholas Penree ([drudge](http://github.com/drudge))
+
+## License
+
+(The MIT License)
+
+Copyright (c) 2010 Nicholas Penree <drudge@conceited.net>
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+'Software'), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
@@ -0,0 +1,15 @@
+/*!
+ * node-gpg
+ * Copyright(c) 2011 Nicholas Penree <drudge@conceited.net>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var gpg = require(__dirname + '/../lib/gpg')
+
+gpg.decryptFile('/tmp/test.txt', function(err, contents){
+ console.log(contents);
+});
@@ -0,0 +1,15 @@
+/*!
+ * node-gpg
+ * Copyright(c) 2011 Nicholas Penree <drudge@conceited.net>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var gpg = require(__dirname + '/../lib/gpg')
+
+gpg.encryptToFile({source:__dirname + '/../lib/gpg.js', dest: '/tmp/test.txt' }, function(err, data){
+ console.log(err);
+});
@@ -0,0 +1,7 @@
+/*!
+ * node-gpg
+ * Copyright(c) 2011 Nicholas Penree <drudge@conceited.net>
+ * MIT Licensed
+ */
+
+module.exports = require(__dirname + '/lib/gpg');
@@ -0,0 +1,187 @@
+
+/*!
+ * node-gpg
+ * Copyright(c) 2011 Nicholas Penree <drudge@conceited.net>
+ * MIT Licensed
+ */
+
+/**
+ * Module dependencies.
+ */
+
+var spawn = require('child_process').spawn
+ , stream = require('fs').createReadStream
+ , writeStream = require('fs').createWriteStream
+ , readFile = require('fs').readFile
+ , pump = require('util').pump
+ , exists = require('path').exists
+
+/**
+ * Base `GPG` object.
+ */
+
+var GPG = {
+
+ /**
+ * Encrypt source file passed as `options.source` and store it in a file specified in `options.dest`.
+ *
+ * @param {Object} options
+ * @param {Function} Optional callback
+ * @api public
+ */
+
+ encryptToFile: function(options, fn){
+ options = options || {};
+
+ if (typeof options.source === 'undefined'){
+ fn.call(null, new Error('Missing \'source\' option'));
+ return;
+ } else if (typeof options.dest === 'undefined'){
+ fn.call(null, new Error('Missing \'dest\' option'));
+ return;
+ }
+
+ exists(options.source, function(e){
+ if (e){
+ var buffer = new Buffer('')
+ , fileStream = stream(options.source)
+ , destStream = writeStream(options.dest)
+ , gpg = spawn('gpg', ['--encrypt']);
+
+ gpg.on('exit', function (code){
+ fn.call(null,null);
+ });
+
+ pump(fileStream, gpg.stdin);
+ pump(gpg.stdout, destStream);
+ } else {
+ fn.call(null, new Error(options.source + ' does not exist', null));
+ }
+ });
+ },
+
+ /**
+ * Encrypt source `file` and pass the encrypted contents to the callback `fn`.
+ *
+ * @param {String} file
+ * @param {Function} Optional callback containing the encrypted file contents.
+ * @api public
+ */
+
+ encryptFile: function(file, fn){
+ var self = this;
+
+ readFile(file, function(err, content){
+ self.encrypt(content, function(err, data){
+ fn.call(null, err, data);
+ });
+ });
+ },
+
+ /**
+ * Encrypt string `str` and pass the encrypted version to the callback `fn`.
+ *
+ * @param {String} str
+ * @param {Function} Optional callback containing the encrypted string contents.
+ * @api public
+ */
+
+ encrypt: function(str, fn){
+ var buffer = new Buffer('')
+ , gpg = spawn('gpg', ['--encrypt']);
+
+ gpg.stdout.on('data', function (data){
+ buffer.write(data.toString('utf8'));
+ });
+ gpg.on('exit', function (code){
+ fn.call(null, null, buffer.toString('utf8'));
+ });
+
+ gpg.stdin.write(str);
+ },
+
+ /**
+ * Dencrypt string `str` and pass the decrypted version to the callback `fn`.
+ *
+ * @param {String} str
+ * @param {Function} Optional callback containing the decrypted string contents.
+ * @api public
+ */
+
+ decrypt: function(str, fn){
+ var buffer = ''
+ , gpg = spawn('gpg', ['--decrypt']);
+
+ gpg.stdout.on('data', function (data){
+ buffer += data.toString('utf8');
+ });
+ gpg.on('exit', function (code){
+ fn.call(null, null, buffer);
+ });
+
+ gpg.stdin.write(str);
+ gpg.stdin.end();
+ },
+
+ /**
+ * Decrypt source `file` and pass the decrypted contents to the callback `fn`.
+ *
+ * @param {String} file
+ * @param {Function} Optional callback containing the decrypted file contents.
+ * @api public
+ */
+
+ decryptFile: function(file, fn){
+ var self = this;
+
+ readFile(file, function(err, content){
+ self.decrypt(content, function(err, data){
+ fn.call(null, err, data);
+ });
+ });
+ },
+
+ /**
+ * Decrypt source file passed as `options.source` and store it in a file specified in `options.dest`.
+ *
+ * @param {Object} options
+ * @param {Function} Optional callback
+ * @api public
+ */
+
+ decryptToFile: function(options, fn){
+ options = options || {};
+
+ if (typeof options.source === 'undefined'){
+ fn.call(null, new Error('Missing \'source\' option'));
+ return;
+ } else if (typeof options.dest === 'undefined'){
+ fn.call(null, new Error('Missing \'dest\' option'));
+ return;
+ }
+
+ exists(options.source, function(e){
+ if (e){
+ var buffer = new Buffer('')
+ , fileStream = stream(options.source)
+ , destStream = writeStream(options.dest)
+ , gpg = spawn('gpg', ['--decrypt']);
+
+ gpg.on('exit', function (code){
+ fn.call(null,null);
+ });
+
+ pump(fileStream, gpg.stdin);
+ pump(gpg.stdout, destStream);
+ } else {
+ fn.call(null, new Error(options.source + ' does not exist'));
+ }
+ });
+ },
+};
+
+/**
+ * Expose `GPG` object.
+ */
+
+module.exports = GPG;
@@ -0,0 +1,17 @@
+{
+ "author": "Nicholas Penree <drudge@conceited.net> (http://penr.ee)",
+ "name": "gpg",
+ "description": "GPG encryption and decryption in node.js by way of the gpg command-line tool",
+ "version": "0.1.0",
+ "repository": {
+ "type": "git",
+ "url": "git://github.com/drudge/node-gpg.git"
+ },
+ "keywords": ["gpg", "encrypt", "decrypt", "pgp", "gnupg"],
+ "main": "./lib/gpg",
+ "engines": {
+ "node": ">= 0.4.0"
+ },
+ "dependencies": {},
+ "devDependencies": {}
+}

0 comments on commit f6f2ef4

Please sign in to comment.