diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..c2658d7 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +node_modules/ diff --git a/browserify/crypto-index.js b/browserify/crypto-index.js new file mode 100644 index 0000000..9a4f4c9 --- /dev/null +++ b/browserify/crypto-index.js @@ -0,0 +1,32 @@ +;(function () { +'use strict'; + + var createHash = require('create-hash'); + var pbkdf2 = require('pbkdf2'); + var aes = require('browserify-aes'); + + exports.sha256 = function (buf) { + var hash = createHash('sha256'); + hash.update(buf); + hash.end(); + return Promise.resolve(hash.read()); + }; + + exports.encrypt = function (data, password, salt, iv) { + // Derived AES key is 128 bit, and the function takes a size in bytes. + var aesKey = pbkdf2.pbkdf2Sync(password, Buffer(salt), 8192, 16, 'sha256'); + var cipher = aes.createCipheriv('aes-128-gcm', aesKey, Buffer(iv)); + var result = Buffer.concat([cipher.update(Buffer(data)), cipher.final(), cipher.getAuthTag()]); + return Promise.resolve(result); + }; + + exports.decrypt = function (data, password, salt, iv) { + var aesKey = pbkdf2.pbkdf2Sync(password, Buffer(salt), 8192, 16, 'sha256'); + var decipher = aes.createDecipheriv('aes-128-gcm', aesKey, Buffer(iv)); + + decipher.setAuthTag(Buffer(data.slice(-16))); + var result = Buffer.concat([decipher.update(Buffer(data.slice(0, -16))), decipher.final()]); + return Promise.resolve(result); + }; + +}()); diff --git a/gulpfile.js b/gulpfile.js new file mode 100644 index 0000000..5cdc695 --- /dev/null +++ b/gulpfile.js @@ -0,0 +1,21 @@ +;(function () { + 'use strict'; + + var gulp = require('gulp'); + var browserify = require('browserify'); + var source = require('vinyl-source-stream'); + var streamify = require('gulp-streamify'); + var uglify = require('gulp-uglify'); + var rename = require('gulp-rename'); + + gulp.task('default', function () { + return browserify('./browserify/crypto-index.js', {standalone: 'OAUTH3_crypto'}).bundle() + .pipe(source('browserify/crypto-index.js')) + .pipe(rename('oauth3.crypto.js')) + .pipe(gulp.dest('./')) + .pipe(streamify(uglify())) + .pipe(rename('oauth3.crypto.min.js')) + .pipe(gulp.dest('./')) + ; + }); +}()); diff --git a/package.json b/package.json new file mode 100644 index 0000000..6ae6ae3 --- /dev/null +++ b/package.json @@ -0,0 +1,23 @@ +{ + "name": "oauth3", + "respository": { + "type": "git", + "url": "git+ssh://git@git.daplie.com:Daplie/oauth3.js.git" + }, + "scripts": { + "install": "./node_modules/.bin/gulp" + }, + "devDependencies": { + "atob": "^2.0.3", + "browserify": "^14.1.0", + "browserify-aes": "^1.0.6", + "btoa": "^1.1.2", + "gulp": "^3.9.1", + "gulp-cli": "^1.2.2", + "gulp-rename": "^1.2.2", + "gulp-streamify": "^1.0.2", + "gulp-uglify": "^2.1.0", + "pbkdf2": "^3.0.9", + "vinyl-source-stream": "^1.1.0" + } +}