├── .gitignore ├── LICENSE.txt ├── README.md ├── index.js ├── package.json └── test └── test.js /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | /.idea 3 | /node_modules 4 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Alon Valadji 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Enc 2 | ==== 3 | 4 | > Simple and fast crypto wrapper for encoding/decoding, ciphering/deciphering and hasing strings. 5 | 6 | #### # 7 | 8 | introduction 9 | ------------ 10 | An idiomatic way to encode/decode (using key or to base64) or hashing strings. 11 | 12 | 13 | installation 14 | ------------ 15 | 16 | $ npm install enc 17 | 18 | usage 19 | -------- 20 | require the Enc library 21 | 22 | ```js 23 | var Enc = require('enc'); 24 | ``` 25 | 26 | basic encode to base64 27 | 28 | ```js 29 | var encoded_str = Enc.base64.encode('test'); 30 | ``` 31 | 32 | now let's decode it 33 | 34 | ```js 35 | var decoded_str = Enc.base64.decode('dGVzdA=='); 36 | ``` 37 | 38 | let's create a secret key 39 | 40 | ```js 41 | var key = 'test'; // can be any string you want 42 | ``` 43 | 44 | let's encrypt using aes192 algorithm 45 | 46 | ```js 47 | var encrypted_str = Enc.aes192.encode('test', key); 48 | ``` 49 | 50 | let's decrypt it 51 | 52 | ```js 53 | var decrypted_str = Enc.aes192.decode('79caa93da9153f23fe10c7ddf2d8267e', key); 54 | ``` 55 | 56 | we want to make a hash 57 | 58 | ```js 59 | var hash = Enc.md5('test'); 60 | ``` 61 | 62 | we can also pass a digest string to the hash method as optional 63 | argument: 'hex', 'binary' or 'base64' ('hex' is default) 64 | 65 | ```js 66 | var hash = Enc.md5('test', 'binary'); 67 | ``` 68 | 69 | that's it :) 70 | 71 | you can use any popular algorithm `Enc#algorithm.encode(str, key)`, 72 | or `Enc#algorithm.decode(str, key)` like so: 73 | 74 | ```js 75 | // hashing 76 | 77 | Enc.md5('test'); 78 | Enc.sha1('test'); 79 | 80 | // encrypting 81 | 82 | Enc.blowfish.encode('test', key); 83 | Enc.des_ede_cfb.encode('test', key); 84 | 85 | // base64 86 | 87 | Enc.base64.encode('test'); 88 | ``` 89 | 90 | to see available algorithms: 91 | ```js 92 | 93 | // for hashes function 94 | console.log(Enc.algorithms.hashes); 95 | 96 | // for cyphers 97 | console.log(Enc.algorithms.cyphers); 98 | 99 | // for buffers 100 | console.log(Enc.algorithms.buffers); 101 | 102 | // all 103 | console.log(Enc.algorithms); 104 | ``` 105 | 106 | __note__: all functions are lowercase and if an algorithm contains a `-` just replace it with `_`. 107 | 108 | ## The MIT License 109 | 110 | Copyright (C) 2012 Alon Valadji 111 | 112 | 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: 113 | 114 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 115 | 116 | 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. 117 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | var crypto = require("crypto"); 4 | var algorithms = exports.algorithms = {}; 5 | algorithms.ciphers = crypto.getCiphers(); 6 | algorithms.hashes = crypto.getHashes(); 7 | algorithms.buffers = ['base64']; 8 | 9 | /** 10 | * Cipher encoding/decoding using key 11 | * 12 | * @param str 13 | * @param key 14 | * 15 | * usage: 16 | * Enc.aes192.encode('test', 'test) 17 | * => 79caa93da9153f23fe10c7ddf2d8267e 18 | * 19 | * Enc.aes192.decode('79caa93da9153f23fe10c7ddf2d8267e', 'test') 20 | * => test 21 | * 22 | * Popular algorithms: 'aes192', 'blowfish', 'cast', 'des', 'des3', 'desx', 'rc2', 'rc4' 23 | * Use method name with _ eg: Enc.aes_128_cbc.encode(str, key) 24 | * 25 | */ 26 | algorithms.ciphers.forEach(function(alg){ 27 | exports[alg.toLowerCase().replace(/\-/g, '_')] = { 28 | encode:function(str, key){ 29 | var cipher = crypto.createCipher(alg, key), 30 | msg = []; 31 | 32 | msg.push(cipher.update(str, 'binary', 'hex')); 33 | msg.push(cipher.final('hex')); 34 | 35 | return msg.join(''); 36 | }, 37 | decode:function(str, key){ 38 | var decipher = crypto.createDecipher(alg, key), 39 | msg = []; 40 | 41 | msg.push(decipher.update(str, 'hex', 'binary')); 42 | msg.push(decipher.final('binary')); 43 | 44 | return msg.join(''); 45 | } 46 | } 47 | }); 48 | 49 | /** 50 | * Buffer encoding/decoding 51 | * 52 | * Encode/Decode strings to base64 53 | * @param str 54 | * 55 | * usage: 56 | * Enc.base64.encode('test') 57 | * => dGVzdA== 58 | * 59 | * Enc.base64.decode('dGVzdA==') 60 | * => test 61 | * 62 | */ 63 | algorithms.buffers.forEach(function(alg){ 64 | exports[alg] = { 65 | encode:function(str){ 66 | return new Buffer(str).toString(alg); 67 | }, 68 | decode:function(str){ 69 | return new Buffer(str, alg).toString(); 70 | } 71 | } 72 | }); 73 | 74 | /** 75 | * Hash encrypting 76 | * 77 | * One way encryption 78 | * @param str 79 | * @param digest (optional - 'hex', 'binary' or 'base64' default to 'hex') 80 | * 81 | * usage: 82 | * Enc.md5('test') 83 | * => 098f6bcd4621d373cade4e832627b4f6 84 | */ 85 | algorithms.hashes.forEach(function(alg){ 86 | exports[alg.toLowerCase().replace(/\-/g, '_')] = function(str, digest){ 87 | return crypto.createHash(alg).update(str.toString()).digest(digest || 'hex'); 88 | } 89 | }); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "enc", 3 | "description": "Encription/Decription crypto wrapper", 4 | "version": "0.4.0", 5 | "author": { 6 | "name": "Alon Valadji", 7 | "email": "alon@ronin.co.il" 8 | }, 9 | "scripts": { 10 | "test": "mocha" 11 | }, 12 | "main": "index", 13 | "keywords": [ 14 | "base64", 15 | "sha1", 16 | "md5", 17 | "crypto", 18 | "encryption", 19 | "decryption" 20 | ], 21 | "repository": "git@github.com:alonronin/Enc.git", 22 | "engines": { 23 | "node": ">= 0.4.1" 24 | }, 25 | "devDependencies": { 26 | "chai": "^1.10.0", 27 | "mocha": "^2.0.1" 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /test/test.js: -------------------------------------------------------------------------------- 1 | var expect = require('chai').expect; 2 | var Enc = require('../'); 3 | 4 | describe('algorithms', function(){ 5 | it('should contain ciphers, hashes and buffers', function(){ 6 | expect(Enc.algorithms).to.have.property('ciphers'); 7 | expect(Enc.algorithms).to.have.property('hashes'); 8 | expect(Enc.algorithms).to.have.property('buffers') 9 | }); 10 | 11 | describe('ciphers', function(){ 12 | it('should be an Array', function(){ 13 | expect(Enc.algorithms.ciphers).to.be.an('Array'); 14 | }); 15 | 16 | it('should contain aes192', function(){ 17 | expect(Enc.algorithms.ciphers).to.contain('aes192'); 18 | }); 19 | 20 | describe('encoding', function(){ 21 | it('using aes192 alg', function(){ 22 | expect(Enc.aes192.encode('test', 'test')).to.equal('79caa93da9153f23fe10c7ddf2d8267e'); 23 | }) 24 | }); 25 | 26 | describe('decoding', function(){ 27 | it('using aes192 alg', function(){ 28 | expect(Enc.aes192.decode('79caa93da9153f23fe10c7ddf2d8267e', 'test')).to.equal('test'); 29 | }) 30 | }); 31 | 32 | }); 33 | 34 | describe('hashes', function(){ 35 | it('hashes should be an Array', function(){ 36 | expect(Enc.algorithms.hashes).to.be.an('Array'); 37 | }); 38 | 39 | it('should contain md5', function(){ 40 | expect(Enc.algorithms.hashes).to.contain('md5'); 41 | }); 42 | 43 | describe('encoding', function(){ 44 | it('using md5 hash', function(){ 45 | expect(Enc.md5('test')).to.equal('098f6bcd4621d373cade4e832627b4f6'); 46 | }) 47 | }); 48 | }); 49 | 50 | describe('buffers', function(){ 51 | it('buffers should be an Array', function(){ 52 | expect(Enc.algorithms.buffers).to.be.an('Array'); 53 | }); 54 | 55 | it('should contain base64', function(){ 56 | expect(Enc.algorithms.buffers).to.contain('base64'); 57 | }); 58 | 59 | describe('encoding', function(){ 60 | it('using base64 buffer', function(){ 61 | expect(Enc.base64.encode('test')).to.equal('dGVzdA=='); 62 | }) 63 | }); 64 | 65 | describe('decoding', function(){ 66 | it('using aes192 alg', function(){ 67 | expect(Enc.base64.decode('dGVzdA==', 'test')).to.equal('test'); 68 | }) 69 | }); 70 | }); 71 | }); --------------------------------------------------------------------------------