From b680a266492d831633f0ea499b6d4689dc3f91b8 Mon Sep 17 00:00:00 2001 From: nbarikipoulos Date: Mon, 11 Mar 2019 23:32:00 +0100 Subject: [PATCH] Initial seed --- .gitignore | 3 +++ LICENSE.md | 9 +++++++ README.md | 48 +++++++++++++++++++++++++++++++++++ index.js | 71 ++++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 33 ++++++++++++++++++++++++ 5 files changed, 164 insertions(+) create mode 100644 .gitignore create mode 100644 LICENSE.md create mode 100644 README.md create mode 100644 index.js create mode 100644 package.json diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f6c8c89 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +node_modules/ + +.DS_Store \ No newline at end of file diff --git a/LICENSE.md b/LICENSE.md new file mode 100644 index 0000000..0b6c6ca --- /dev/null +++ b/LICENSE.md @@ -0,0 +1,9 @@ + +Copyright (c) 2019 N. Barikipoulos + +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. + \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..5d36de8 --- /dev/null +++ b/README.md @@ -0,0 +1,48 @@ +# gulp-cipher-json +[![NPM version][npm-image]][npm-url] + +| Package | gulp-cipher-value | +| ------- | ----------------- | +| Description | Gulp wrapper for [json-cipher-value](https://github.com/nbarikipoulos/json-cipher-value). (Un)cipher values of json files retaining their types. | + +## Install + +```shell +npm install gulp-cipher-json --save-dev +``` + +## Usage + +```js + +const gulp = require('gulp'); +const cipherJSON = require('gulp-cipher-json'); + +gulp.task('default', () => gulp.src('src/data/*.json') + .pipe(cipherJSON( + 'decrypt', + 'my secret password from anywhere' + )) + .pipe(gulp.dest('dist/data')) +); +``` + +## cipherJSON(action, secret, [options]) + +__action__: either 'encrypt' or 'decrypt' + +__secret__: password + +__options__: [json-cipher-value](https://github.com/nbarikipoulos/json-cipher-value) options. Note the default settings perform an aes-256-ctr ciphering. + + +## Credits + +- Nikolaos Barikipoulos ([nbarikipoulos](https://github.com/nbarikipoulos)) + +## License + +This module is MIT licensed. See [LICENSE](./LICENSE.md). + +[npm-url]: https://www.npmjs.com/package/gulp-cipher-json +[npm-image]: https://img.shields.io/npm/v/gulp-cipher-json.svg \ No newline at end of file diff --git a/index.js b/index.js new file mode 100644 index 0000000..84dbcc8 --- /dev/null +++ b/index.js @@ -0,0 +1,71 @@ +/*! + * (The MIT License) + * + * Copyright (c) 2019 N. Barikipoulos + * + * 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. + */ +'use strict' + +const through = require('through2'); +const PluginError = require('plugin-error'); + +const createCryptObject = require('json-cipher-value'); + +const PLUGIN_NAME = 'gulp-cipher-json'; + +module.exports = (action, secret, options) => { + return through.obj( (file, enc, cb) => { + + if (file.isNull()) { + // Do nothing + return cb(null, file); + } + + if (file.isStream()) { + cb(new PluginError( + PLUGIN_NAME, + 'Streams not supported!' + )); + } + + // Validation of args + + if ( action !== 'encrypt' && action !=='decrypt' ) { + cb(new PluginError( + PLUGIN_NAME, + 'action option must be set either to \'crypt\' or \'encrypt\'' + )); + } + if ( undefined === secret ) { + cb(new PluginError( + PLUGIN_NAME, + 'A secret phrase is required' + )); + } + + // + // Main job + // + + if (file.isBuffer()) { // Other cas possible? + + let cryptObject = createCryptObject(secret, options); + + let encryptedObject = cryptObject[`${action}`].call( + cryptObject, + JSON.parse(file.contents.toString()) + ); + + file.contents = Buffer.from(JSON.stringify( + encryptedObject, + null, + 2 + )); + + return cb(null, file); + } + }); +}; \ No newline at end of file diff --git a/package.json b/package.json new file mode 100644 index 0000000..a114473 --- /dev/null +++ b/package.json @@ -0,0 +1,33 @@ +{ + "name": "gulp-cipher-json", + "version": "1.0.0", + "description": "(Un)cipher json values", + "main": "index.js", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "homepage": "https://github.com/nbarikipoulos/gulp-cipher-json#readme", + "author": "Nikolaos Barikipoulos ", + "license": "MIT", + "keywords": [ + "gulpplugin", + "json", + "crypto", + "cipher", + "encrypt", + "decrypt", + "aes256" + ], + "dependencies": { + "json-cipher-value": "^1.1.0", + "plugin-error": "^1.0.1", + "through2": "^3.0.1" + }, + "repository": { + "type": "git", + "url": "https://github.com/nbarikipoulos/gulp-cipher-json.git" + }, + "engines": { + "node": ">=8.0.0" + } +}