├── package.json ├── LICENSE ├── README.md └── cipher.js /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cipherstream", 3 | "description": "Simple Stream layer for encryption/decryption", 4 | "version": "0.1.1", 5 | "author" : "Andris Reinman", 6 | "maintainers":[ 7 | { 8 | "name":"andris", 9 | "email":"andris@node.ee" 10 | } 11 | ], 12 | "homepage": "http://github.com/andris9/cipher", 13 | "repository" : { 14 | "type" : "git", 15 | "url" : "http://github.com/andris9/cipher.git" 16 | }, 17 | "main" : "./cipher", 18 | "licenses" : [ 19 | { 20 | "type": "MIT", 21 | "url": "http://github.com/andris9/cipher/blob/master/LICENSE" 22 | } 23 | ], 24 | "engine": [ "node >=0.4.0" ], 25 | "keywords": ["cipher", "stream", "cryptography"] 26 | } 27 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2011 Andris Reinman 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a copy 4 | of this software and associated documentation files (the "Software"), to deal 5 | in the Software without restriction, including without limitation the rights 6 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 | copies of the Software, and to permit persons to whom the Software is 8 | furnished to do so, subject to the following conditions: 9 | 10 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 11 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 12 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 13 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 14 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 15 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 16 | SOFTWARE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DEPRECATION WARNING 2 | 3 | This project is deprecated and might not work with the latest versions of Node. Node Crypto module provides the same functionality, so this module isn't even needed. 4 | 5 | # cipherstream 6 | 7 | **cipherstream** is a thin streaming layer for encryption/decryption in Node.JS. 8 | 9 | ## Installation 10 | 11 | npm install cipherstream 12 | 13 | ## Usage 14 | 15 | Require as `cipherstream` 16 | 17 | var cipherstream = require("cipherstream"); 18 | 19 | ## CipherStream 20 | 21 | **CipherStream** creates a Stream object which takes data in and encrypts it. 22 | 23 | `new CipherStream(password[, algorithm])` where `algorithm` defaults to AES192. 24 | 25 | Example: 26 | 27 | var cipherstream = require("cipherstream"), 28 | fs = require("fs"); 29 | 30 | fs.createReadStream("plain.txt").pipe(new cipherstream.CipherStream("secret")).pipe(fs.writeReadStream("secret.txt")); 31 | 32 | 33 | ## DecipherStream 34 | 35 | **DecipherStream** creates a Stream object which takes encrypted data in and decrypts it. 36 | 37 | `new DecipherStream(password[, algorithm])` where `algorithm` defaults to AES192. 38 | 39 | Example: 40 | 41 | var cipherstream = require("cipherstream"), 42 | fs = require("fs"); 43 | 44 | fs.createReadStream("secret.txt").pipe(new cipherstream.DecipherStream("secret")).pipe(fs.writeReadStream("plain.txt")); 45 | 46 | ## HMAC 47 | 48 | **hmac** is a simple wrapper function to create SHA-256 hmac values. 49 | 50 | `cipherstream.hmac(data, key) → String` 51 | 52 | ## License 53 | 54 | **MIT** 55 | -------------------------------------------------------------------------------- /cipher.js: -------------------------------------------------------------------------------- 1 | var Stream = require("stream").Stream, 2 | utillib = require("util"), 3 | crypto = require('crypto'), 4 | fs = require("fs"); 5 | 6 | module.exports.CipherStream = CipherStream; 7 | module.exports.DecipherStream = DecipherStream; 8 | 9 | module.exports.hmac = function(data, key){ 10 | var hmac = crypto.createHmac("SHA256", key); 11 | hmac.update(data); 12 | return hmac.digest("hex"); 13 | }; 14 | 15 | module.exports.md5 = function(data){ 16 | var hash = crypto.createHash("MD5"); 17 | hash.update(data); 18 | return hash.digest("hex"); 19 | }; 20 | 21 | function CipherStream(password, algorithm){ 22 | var self = this; 23 | 24 | Stream.call(this); 25 | 26 | algorithm = algorithm || "aes192"; 27 | 28 | this.paused = false; 29 | this.destStream = null; 30 | this.writable = true; 31 | 32 | this.on("pipe", function(source) { 33 | source.on("drain", function() { 34 | self.resume(); 35 | }); 36 | }); 37 | 38 | this.cipher = crypto.createCipher(algorithm, password); 39 | } 40 | utillib.inherits(CipherStream, Stream); 41 | 42 | CipherStream.prototype.write = function(chunk){ 43 | this.emit("data", new Buffer(this.cipher.update(chunk), "binary")); 44 | return(! this.paused); 45 | } 46 | 47 | CipherStream.prototype.end = function(){ 48 | this.emit("data", new Buffer(this.cipher.final(), "binary")); 49 | this.emit("end"); 50 | } 51 | 52 | CipherStream.prototype.pipe = function() { 53 | var self = this; 54 | this.destStream = arguments[0]; 55 | this.destStream.on("drain", function() { 56 | self.emit("drain"); 57 | self.resume(); 58 | }); 59 | Stream.prototype.pipe.apply(this, arguments); 60 | } 61 | 62 | CipherStream.prototype.pause = function() { 63 | this.paused = true; 64 | this.emit("pause"); 65 | } 66 | 67 | CipherStream.prototype.resume = function() { 68 | this.paused = false; 69 | } 70 | 71 | function DecipherStream(password, algorithm){ 72 | var self = this; 73 | 74 | Stream.call(this); 75 | 76 | algorithm = algorithm || "aes192"; 77 | 78 | this.paused = false; 79 | this.destStream = null; 80 | this.writable = true; 81 | 82 | this.on("pipe", function(source) { 83 | source.on("drain", function() { 84 | self.resume(); 85 | }); 86 | }); 87 | 88 | this.decipher = crypto.createDecipher(algorithm, password); 89 | } 90 | utillib.inherits(DecipherStream, Stream); 91 | 92 | DecipherStream.prototype.write = function(chunk){ 93 | this.emit("data", new Buffer(this.decipher.update(chunk), "binary")); 94 | return(! this.paused); 95 | } 96 | 97 | DecipherStream.prototype.end = function(){ 98 | this.emit("data", new Buffer(this.decipher.final(), "binary")); 99 | this.emit("end"); 100 | } 101 | 102 | DecipherStream.prototype.pipe = function() { 103 | var self = this; 104 | this.destStream = arguments[0]; 105 | this.destStream.on("drain", function() { 106 | self.emit("drain"); 107 | self.resume(); 108 | }); 109 | Stream.prototype.pipe.apply(this, arguments); 110 | } 111 | 112 | DecipherStream.prototype.pause = function() { 113 | this.paused = true; 114 | this.emit("pause"); 115 | } 116 | 117 | DecipherStream.prototype.resume = function() { 118 | this.paused = false; 119 | } 120 | 121 | --------------------------------------------------------------------------------