├── .travis.yml ├── .gitignore ├── bench ├── package.json └── index.js ├── salteen.d.ts ├── .editorconfig ├── src └── index.js ├── package.json ├── license ├── test └── index.js └── readme.md /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - 6 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | node_modules 3 | *-lock.json 4 | *.lock 5 | *.log 6 | dist 7 | -------------------------------------------------------------------------------- /bench/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "private": true, 3 | "devDependencies": { 4 | "benchmark": "^2.1.4" 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /salteen.d.ts: -------------------------------------------------------------------------------- 1 | export const encrypt: (salt: string) => (value: string) => string; 2 | export const decrypt: (salt: string) => (value: string) => string; 3 | -------------------------------------------------------------------------------- /.editorconfig: -------------------------------------------------------------------------------- 1 | # http://editorconfig.org 2 | root = true 3 | 4 | [*] 5 | indent_size = 2 6 | indent_style = tab 7 | end_of_line = lf 8 | charset = utf-8 9 | trim_trailing_whitespace = true 10 | insert_final_newline = true 11 | 12 | [*.{json,yml,md}] 13 | indent_style = space 14 | -------------------------------------------------------------------------------- /bench/index.js: -------------------------------------------------------------------------------- 1 | const { Suite } = require('benchmark'); 2 | const salteen = require('../dist/salteen'); 3 | 4 | const KEY = 'ABvbsjaBSHC1265CHS'; 5 | const VALUE = 'hello world how are you?'; 6 | const STRING = '45484141420d5a425f41490d45425a0d4c5f480d54425812'; 7 | 8 | function bench(name) { 9 | console.log(`\n# ${name}`); 10 | const suite = new Suite(); 11 | suite.on('cycle', e => console.log(' ' + e.target)); 12 | return suite; 13 | } 14 | 15 | bench('encrypt') 16 | .add('salteen ', () => salteen.encrypt(KEY)(VALUE)) 17 | .run(); 18 | 19 | bench('decrypt') 20 | .add('salteen ', () => salteen.decrypt(KEY)(STRING)) 21 | .run(); 22 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | function toChars(str) { 2 | var i=0, arr=[]; 3 | while (i < str.length) { 4 | arr.push(str.charCodeAt(i++)); 5 | } 6 | return arr; 7 | } 8 | 9 | function reduce(hasher, base, value) { 10 | var i=0, j, tmp, str=''; 11 | var chars = base ? value.match(/.{1,2}/g) : toChars(value); 12 | while (i < chars.length) { 13 | tmp = base ? parseInt(chars[i++], 16) : chars[i++]; 14 | for (j=0; j < hasher.length;) tmp ^= hasher[j++]; 15 | str += base ? String.fromCharCode(tmp) : ('0' + tmp.toString(16)).substr(-2); 16 | } 17 | return str; 18 | } 19 | 20 | export function encrypt(key) { 21 | return reduce.bind(reduce, toChars(key), 0); 22 | } 23 | 24 | export function decrypt(key) { 25 | return reduce.bind(reduce, toChars(key), 1); 26 | } 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "salteen", 3 | "version": "1.0.0", 4 | "repository": "lukeed/salteen", 5 | "description": "A snappy and lightweight (259B) utility to encrypt and decrypt values with salt.", 6 | "unpkg": "dist/salteen.min.js", 7 | "module": "dist/salteen.mjs", 8 | "main": "dist/salteen.js", 9 | "types": "salteen.d.ts", 10 | "license": "MIT", 11 | "author": { 12 | "name": "Luke Edwards", 13 | "email": "luke.edwards05@gmail.com", 14 | "url": "lukeed.com" 15 | }, 16 | "engines": { 17 | "node": ">=6" 18 | }, 19 | "scripts": { 20 | "build": "bundt", 21 | "pretest": "npm run build", 22 | "test": "tape test/*.js | tap-spec" 23 | }, 24 | "files": [ 25 | "*.d.ts", 26 | "dist" 27 | ], 28 | "keywords": [ 29 | "encrypt", 30 | "decrypt", 31 | "hasher", 32 | "hashing", 33 | "salt", 34 | "salting" 35 | ], 36 | "devDependencies": { 37 | "bundt": "^0.3.0", 38 | "tap-spec": "^5.0.0", 39 | "tape": "^4.10.1" 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /license: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Luke Edwards (lukeed.com) 4 | 5 | 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: 6 | 7 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 8 | 9 | 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. 10 | -------------------------------------------------------------------------------- /test/index.js: -------------------------------------------------------------------------------- 1 | const test = require('tape'); 2 | const lib = require('../dist/salteen'); 3 | 4 | test('exports', t => { 5 | t.is(typeof lib, 'object', 'exports a object'); 6 | t.is(typeof lib.encrypt, 'function', '~> has "encrypt" function'); 7 | t.is(typeof lib.decrypt, 'function', '~> has "decrypt" function'); 8 | t.end(); 9 | }); 10 | 11 | test('encrypt()', t => { 12 | let key = 'foobar'; 13 | 14 | let foo = lib.encrypt(key); 15 | t.is(typeof foo, 'function', 'returns a new function'); 16 | 17 | let val1 = foo('hello'); 18 | t.is(typeof val1, 'string', '~> factory produces a string'); 19 | 20 | let val2 = foo('world'); 21 | t.is(typeof val2, 'string', '~> factory produces a string'); 22 | 23 | t.isNot(val1, val2, 'hashed-values are not the same'); 24 | t.is(val1, '7f727b7b78', '~> 1st value is correct'); 25 | t.is(val2, '6078657b73', '~> 2nd value is correct'); 26 | 27 | let val3 = lib.encrypt('hello')('hello'); 28 | t.isNot(val1, val3, 'result is different w/ new key'); 29 | t.is(val3, '0a070e0e0d', '~> 3rd value is correct'); 30 | 31 | t.end(); 32 | }); 33 | 34 | test('decrypt()', t => { 35 | let key = 'foobar'; 36 | 37 | let foo = lib.decrypt(key); 38 | t.is(typeof foo, 'function', 'returns a new function'); 39 | 40 | let val1 = foo('7f727b7b78'); 41 | t.is(typeof val1, 'string', '~> factory produces a string'); 42 | 43 | let val2 = foo('6078657b73'); 44 | t.is(typeof val2, 'string', '~> factory produces a string'); 45 | 46 | t.isNot(val1, val2, 'real-values are not the same'); 47 | t.is(val1, 'hello', '~> 1st value is correct'); 48 | t.is(val2, 'world', '~> 2nd value is correct'); 49 | 50 | let val3 = lib.decrypt('hello')('0a070e0e0d'); 51 | t.is(val3, 'hello', '~> 3rd value is correct'); 52 | 53 | t.end(); 54 | }); 55 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # salteen [![Build Status](https://badgen.now.sh/travis/lukeed/salteen)](https://travis-ci.org/lukeed/salteen) 2 | 3 | > A snappy and lightweight (259B) utility to encrypt and decrypt values with salt. 4 | 5 | Both `encrypt` and `decrypt` are factory functions that accept a `salt` key and return new functions to be called with the unique value(s) to handle. This allows you reuse instances across multiple values. 6 | 7 | This module is available in three formats: 8 | 9 | * **CommonJS**: `dist/salteen.js` 10 | * **ESModule**: `dist/salteen.mjs` 11 | * **UMD**: `dist/salteen.min.js` 12 | 13 | 14 | ## Install 15 | 16 | ``` 17 | $ npm install --save salteen 18 | ``` 19 | 20 | 21 | ## Usage 22 | 23 | ```js 24 | const { encrypt, decrypt } = require('salteen'); 25 | 26 | const MY_SALT = 'foobar'; // PS: the longer the better 27 | 28 | const encoder = encrypt(MY_SALT); 29 | const decoder = decrypt(MY_SALT); 30 | 31 | ['hello', 'world'].map(encoder); 32 | //=> ['7f727b7b78', '6078657b73'] 33 | 34 | ['7f727b7b78', '6078657b73'].map(decoder); 35 | //=> ['hello', 'world'] 36 | ``` 37 | 38 | 39 | ## API 40 | 41 | ### encrypt(salt) 42 | Returns: `Function` 43 | 44 | Returns a new `Function` which will accept a `value` to be encrypted, using `salt` as the salting value. 45 | 46 | #### salt 47 | Type: `String` 48 | 49 | The salting value to be used – longer salts are more secure. 50 | 51 | ### decrypt(salt) 52 | return `Function` 53 | 54 | Returns a new `Function` which will accept a `value` to be decrypted, using `salt` as the salting value. 55 | 56 | #### salt 57 | Type: `String` 58 | 59 | The salting value to be used – longer salts are more secure. 60 | 61 | 62 | ## Benchmarks 63 | 64 | > Results below are with Node v11.11.0 65 | 66 | ``` 67 | encrypt x 281,299 ops/sec ±0.66% (97 runs sampled) 68 | decrypt x 383,827 ops/sec ±1.81% (90 runs sampled) 69 | ``` 70 | 71 | 72 | ## License 73 | 74 | MIT © [Luke Edwards](https://lukeed.com) 75 | --------------------------------------------------------------------------------