browser-passworder
Advanced tools
Comparing version 1.0.1 to 2.0.0
83
index.js
@@ -0,1 +1,3 @@ | ||
require('unibabel') | ||
module.exports = { | ||
@@ -12,6 +14,2 @@ | ||
// Buffer <-> String methods | ||
convertArrayBufferViewtoString, | ||
convertStringToArrayBufferView, | ||
// Buffer <-> Hex string methods | ||
@@ -21,6 +19,2 @@ serializeBufferForStorage, | ||
// Buffer <-> base64 string methods | ||
encodeBufferToBase64, | ||
decodeBase64ToBuffer, | ||
generateSalt, | ||
@@ -31,5 +25,5 @@ } | ||
function encrypt (password, dataObj) { | ||
const salt = this.generateSalt() | ||
var salt = generateSalt() | ||
return keyFromPassword(password + salt) | ||
return keyFromPassword(password, salt) | ||
.then(function (passwordDerivedKey) { | ||
@@ -46,3 +40,3 @@ return encryptWithKey(passwordDerivedKey, dataObj) | ||
var data = JSON.stringify(dataObj) | ||
var dataBuffer = convertStringToArrayBufferView(data) | ||
var dataBuffer = Unibabel.utf8ToBuffer(data) | ||
var vector = global.crypto.getRandomValues(new Uint8Array(16)) | ||
@@ -54,4 +48,4 @@ return global.crypto.subtle.encrypt({ | ||
var buffer = new Uint8Array(buf) | ||
var vectorStr = encodeBufferToBase64(vector) | ||
var vaultStr = encodeBufferToBase64(buffer) | ||
var vectorStr = Unibabel.bufferToBase64(vector) | ||
var vaultStr = Unibabel.bufferToBase64(buffer) | ||
return { | ||
@@ -68,3 +62,3 @@ data: vaultStr, | ||
const salt = payload.salt | ||
return keyFromPassword(password + salt) | ||
return keyFromPassword(password, salt) | ||
.then(function (key) { | ||
@@ -76,8 +70,8 @@ return decryptWithKey(key, payload) | ||
function decryptWithKey (key, payload) { | ||
const encryptedData = decodeBase64ToBuffer(payload.data) | ||
const vector = decodeBase64ToBuffer(payload.iv) | ||
const encryptedData = Unibabel.base64ToBuffer(payload.data) | ||
const vector = Unibabel.base64ToBuffer(payload.iv) | ||
return crypto.subtle.decrypt({name: 'AES-GCM', iv: vector}, key, encryptedData) | ||
.then(function (result) { | ||
const decryptedData = new Uint8Array(result) | ||
const decryptedStr = convertArrayBufferViewtoString(decryptedData) | ||
const decryptedStr = Unibabel.bufferToUtf8(decryptedData) | ||
const decryptedObj = JSON.parse(decryptedStr) | ||
@@ -91,25 +85,25 @@ return decryptedObj | ||
function convertStringToArrayBufferView (str) { | ||
var bytes = new Uint8Array(str.length) | ||
for (var i = 0; i < str.length; i++) { | ||
bytes[i] = str.charCodeAt(i) | ||
} | ||
function keyFromPassword (password, salt) { | ||
var passBuffer = Unibabel.utf8ToBuffer(password) | ||
var saltBuffer = Unibabel.base64ToBuffer(salt) | ||
return bytes | ||
} | ||
return global.crypto.subtle.importKey( | ||
'raw', | ||
passBuffer, | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits', 'deriveKey'] | ||
).then(function (key) { | ||
function convertArrayBufferViewtoString (buffer) { | ||
var str = '' | ||
for (var i = 0; i < buffer.byteLength; i++) { | ||
str += String.fromCharCode(buffer[i]) | ||
} | ||
return str | ||
} | ||
function keyFromPassword (password) { | ||
var passBuffer = convertStringToArrayBufferView(password) | ||
return global.crypto.subtle.digest('SHA-256', passBuffer) | ||
.then(function (passHash) { | ||
return global.crypto.subtle.importKey('raw', passHash, {name: 'AES-GCM'}, false, ['encrypt', 'decrypt']) | ||
return global.crypto.subtle.deriveKey( | ||
{ name: 'PBKDF2', | ||
salt: saltBuffer, | ||
iterations: 10000, | ||
hash: 'SHA-256', | ||
}, | ||
key, | ||
{ name: 'AES-GCM', length: 256 }, | ||
false, | ||
['encrypt', 'decrypt'] | ||
) | ||
}) | ||
@@ -146,15 +140,2 @@ } | ||
function encodeBufferToBase64 (buf) { | ||
var b64encoded = btoa(String.fromCharCode.apply(null, buf)) | ||
return b64encoded | ||
} | ||
function decodeBase64ToBuffer (base64) { | ||
var buf = new Uint8Array(atob(base64).split('') | ||
.map(function (c) { | ||
return c.charCodeAt(0) | ||
})) | ||
return buf | ||
} | ||
function generateSalt (byteCount = 32) { | ||
@@ -161,0 +142,0 @@ var view = new Uint8Array(byteCount) |
{ | ||
"name": "browser-passworder", | ||
"version": "1.0.1", | ||
"version": "2.0.0", | ||
"description": "A simple browserifiable module for password-encrypting JS objects.", | ||
@@ -28,3 +28,6 @@ "main": "index.js", | ||
}, | ||
"homepage": "https://github.com/flyswatter/browser-encryptor#readme" | ||
"homepage": "https://github.com/flyswatter/browser-encryptor#readme", | ||
"dependencies": { | ||
"unibabel": "^2.1.3" | ||
} | ||
} |
@@ -1924,2 +1924,4 @@ (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){ | ||
(function (global){ | ||
require('unibabel') | ||
module.exports = { | ||
@@ -1936,6 +1938,2 @@ | ||
// Buffer <-> String methods | ||
convertArrayBufferViewtoString, | ||
convertStringToArrayBufferView, | ||
// Buffer <-> Hex string methods | ||
@@ -1945,6 +1943,2 @@ serializeBufferForStorage, | ||
// Buffer <-> base64 string methods | ||
encodeBufferToBase64, | ||
decodeBase64ToBuffer, | ||
generateSalt, | ||
@@ -1955,5 +1949,5 @@ } | ||
function encrypt (password, dataObj) { | ||
const salt = this.generateSalt() | ||
var salt = generateSalt() | ||
return keyFromPassword(password + salt) | ||
return keyFromPassword(password, salt) | ||
.then(function (passwordDerivedKey) { | ||
@@ -1970,3 +1964,3 @@ return encryptWithKey(passwordDerivedKey, dataObj) | ||
var data = JSON.stringify(dataObj) | ||
var dataBuffer = convertStringToArrayBufferView(data) | ||
var dataBuffer = Unibabel.utf8ToBuffer(data) | ||
var vector = global.crypto.getRandomValues(new Uint8Array(16)) | ||
@@ -1978,4 +1972,4 @@ return global.crypto.subtle.encrypt({ | ||
var buffer = new Uint8Array(buf) | ||
var vectorStr = encodeBufferToBase64(vector) | ||
var vaultStr = encodeBufferToBase64(buffer) | ||
var vectorStr = Unibabel.bufferToBase64(vector) | ||
var vaultStr = Unibabel.bufferToBase64(buffer) | ||
return { | ||
@@ -1992,3 +1986,3 @@ data: vaultStr, | ||
const salt = payload.salt | ||
return keyFromPassword(password + salt) | ||
return keyFromPassword(password, salt) | ||
.then(function (key) { | ||
@@ -2000,8 +1994,8 @@ return decryptWithKey(key, payload) | ||
function decryptWithKey (key, payload) { | ||
const encryptedData = decodeBase64ToBuffer(payload.data) | ||
const vector = decodeBase64ToBuffer(payload.iv) | ||
const encryptedData = Unibabel.base64ToBuffer(payload.data) | ||
const vector = Unibabel.base64ToBuffer(payload.iv) | ||
return crypto.subtle.decrypt({name: 'AES-GCM', iv: vector}, key, encryptedData) | ||
.then(function (result) { | ||
const decryptedData = new Uint8Array(result) | ||
const decryptedStr = convertArrayBufferViewtoString(decryptedData) | ||
const decryptedStr = Unibabel.bufferToUtf8(decryptedData) | ||
const decryptedObj = JSON.parse(decryptedStr) | ||
@@ -2015,23 +2009,30 @@ return decryptedObj | ||
function convertStringToArrayBufferView (str) { | ||
var bytes = new Uint8Array(str.length) | ||
for (var i = 0; i < str.length; i++) { | ||
bytes[i] = str.charCodeAt(i) | ||
} | ||
function keyFromPassword (password, salt) { | ||
var passBuffer = Unibabel.utf8ToBuffer(password) | ||
var saltBuffer = Unibabel.base64ToBuffer(salt) | ||
return bytes | ||
} | ||
return global.crypto.subtle.importKey( | ||
'raw', | ||
passBuffer, | ||
{ name: 'PBKDF2' }, | ||
false, | ||
['deriveBits', 'deriveKey'] | ||
).then(function (key) { | ||
function convertArrayBufferViewtoString (buffer) { | ||
var str = '' | ||
for (var i = 0; i < buffer.byteLength; i++) { | ||
str += String.fromCharCode(buffer[i]) | ||
} | ||
return global.crypto.subtle.deriveKey( | ||
{ name: 'PBKDF2', | ||
salt: saltBuffer, | ||
iterations: 10000, | ||
hash: 'SHA-256', | ||
}, | ||
key, | ||
{ name: 'AES-GCM', length: 256 }, | ||
false, | ||
['encrypt', 'decrypt'] | ||
) | ||
}) | ||
return str | ||
} | ||
return global.crypto.subtle.deriveKey('') | ||
return global.crypto.subtle.digest('SHA-256', passBuffer) | ||
function keyFromPassword (password) { | ||
var passBuffer = convertStringToArrayBufferView(password) | ||
return global.crypto.subtle.digest('SHA-256', passBuffer) | ||
.then(function (passHash) { | ||
@@ -2070,15 +2071,2 @@ return global.crypto.subtle.importKey('raw', passHash, {name: 'AES-GCM'}, false, ['encrypt', 'decrypt']) | ||
function encodeBufferToBase64 (buf) { | ||
var b64encoded = btoa(String.fromCharCode.apply(null, buf)) | ||
return b64encoded | ||
} | ||
function decodeBase64ToBuffer (base64) { | ||
var buf = new Uint8Array(atob(base64).split('') | ||
.map(function (c) { | ||
return c.charCodeAt(0) | ||
})) | ||
return buf | ||
} | ||
function generateSalt (byteCount = 32) { | ||
@@ -2092,3 +2080,109 @@ var view = new Uint8Array(byteCount) | ||
}).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {}) | ||
},{}],6:[function(require,module,exports){ | ||
},{"unibabel":6}],6:[function(require,module,exports){ | ||
(function () { | ||
'use strict'; | ||
function utf8ToBinaryString(str) { | ||
var escstr = encodeURIComponent(str); | ||
// replaces any uri escape sequence, such as %0A, | ||
// with binary escape, such as 0x0A | ||
var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) { | ||
return String.fromCharCode(parseInt(p1, 16)); | ||
}); | ||
return binstr; | ||
} | ||
function utf8ToBuffer(str) { | ||
var binstr = utf8ToBinaryString(str); | ||
var buf = binaryStringToBuffer(binstr); | ||
return buf; | ||
} | ||
function utf8ToBase64(str) { | ||
var binstr = utf8ToBinaryString(str); | ||
return btoa(binstr); | ||
} | ||
function binaryStringToUtf8(binstr) { | ||
var escstr = binstr.replace(/(.)/g, function (m, p) { | ||
var code = p.charCodeAt(0).toString(16).toUpperCase(); | ||
if (code.length < 2) { | ||
code = '0' + code; | ||
} | ||
return '%' + code; | ||
}); | ||
return decodeURIComponent(escstr); | ||
} | ||
function bufferToUtf8(buf) { | ||
var binstr = bufferToBinaryString(buf); | ||
return binaryStringToUtf8(binstr); | ||
} | ||
function base64ToUtf8(b64) { | ||
var binstr = atob(b64); | ||
return binaryStringToUtf8(binstr); | ||
} | ||
function bufferToBinaryString(buf) { | ||
var binstr = Array.prototype.map.call(buf, function (ch) { | ||
return String.fromCharCode(ch); | ||
}).join(''); | ||
return binstr; | ||
} | ||
function bufferToBase64(arr) { | ||
var binstr = bufferToBinaryString(arr); | ||
return btoa(binstr); | ||
} | ||
function binaryStringToBuffer(binstr) { | ||
var buf; | ||
if ('undefined' !== typeof Uint8Array) { | ||
buf = new Uint8Array(binstr.length); | ||
} else { | ||
buf = []; | ||
} | ||
Array.prototype.forEach.call(binstr, function (ch, i) { | ||
buf[i] = ch.charCodeAt(0); | ||
}); | ||
return buf; | ||
} | ||
function base64ToBuffer(base64) { | ||
var binstr = atob(base64); | ||
var buf = binaryStringToBuffer(binstr); | ||
return buf; | ||
} | ||
window.Unibabel = { | ||
utf8ToBinaryString: utf8ToBinaryString | ||
, utf8ToBuffer: utf8ToBuffer | ||
, utf8ToBase64: utf8ToBase64 | ||
, binaryStringToUtf8: binaryStringToUtf8 | ||
, bufferToUtf8: bufferToUtf8 | ||
, base64ToUtf8: base64ToUtf8 | ||
, bufferToBinaryString: bufferToBinaryString | ||
, bufferToBase64: bufferToBase64 | ||
, binaryStringToBuffer: binaryStringToBuffer | ||
, base64ToBuffer: base64ToBuffer | ||
// compat | ||
, strToUtf8Arr: utf8ToBuffer | ||
, utf8ArrToStr: bufferToUtf8 | ||
, arrToBase64: bufferToBase64 | ||
, base64ToArr: base64ToBuffer | ||
}; | ||
}()); | ||
},{}],7:[function(require,module,exports){ | ||
(function (Buffer){ | ||
@@ -2137,2 +2231,3 @@ var encryptor = require('../') | ||
.catch(function(reason) { | ||
console.error(reason) | ||
assert.ifError(reason, 'threw an error') | ||
@@ -2169,2 +2264,2 @@ done(reason) | ||
}).call(this,require("buffer").Buffer) | ||
},{"../":5,"buffer":2}]},{},[6]); | ||
},{"../":5,"buffer":2}]},{},[7]); |
@@ -43,2 +43,3 @@ var encryptor = require('../') | ||
.catch(function(reason) { | ||
console.error(reason) | ||
assert.ifError(reason, 'threw an error') | ||
@@ -45,0 +46,0 @@ done(reason) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
69018
9
2085
1
+ Addedunibabel@^2.1.3
+ Addedunibabel@2.1.8(transitive)