Skip to content
This repository has been archived by the owner on Jan 14, 2020. It is now read-only.

Commit

Permalink
PBKDF2 override
Browse files Browse the repository at this point in the history
  • Loading branch information
perry-mitchell committed Apr 10, 2016
1 parent 95b5691 commit 3770deb
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 3 deletions.
2 changes: 2 additions & 0 deletions Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ module.exports = function(grunt) {
buttercup_raw: {
src: [
"node_modules/es6-promise/dist/es6-promise.min.js",
"node_modules/text-encoding/lib/encoding.js",
"build/buttercup.js"
],
options: {
Expand All @@ -17,6 +18,7 @@ module.exports = function(grunt) {
buttercup_min: {
src: [
"node_modules/es6-promise/dist/es6-promise.min.js",
"node_modules/text-encoding/lib/encoding.js",
"build/buttercup.min.js"
],
options: {
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
},
"devDependencies": {
"es6-promise": "^3.1.2",
"grunt-contrib-jasmine": "^0.9.2"
"grunt-contrib-jasmine": "^0.9.2",
"text-encoding": "^0.6.0"
}
}
66 changes: 66 additions & 0 deletions source/HashingTools.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
(function(module) {

"use strict";

function arrayBufferToHexString(arrayBuffer) {
var byteArray = new Uint8Array(arrayBuffer);
var hexString = "";
var nextHexByte;

for (var i = 0; i < byteArray.byteLength; i += 1) {
nextHexByte = byteArray[i].toString(16);
if (nextHexByte.length < 2) {
nextHexByte = "0" + nextHexByte;
}
hexString += nextHexByte;
}
return hexString;
}

function stringToArrayBuffer(string) {
var encoder = new TextEncoder("utf-8");
return encoder.encode(string);
}

var lib = module.exports = {

deriveKeyFromPassword: function(password, salt, rounds, bits/*, algorithm*/) {
if (!window.TextEncoder || !window.TextDecoder) {
throw new Error("TextEncoder is not available");
}
return window.crypto.subtle.importKey(
"raw",
stringToArrayBuffer("passw0rd"),
{"name": "PBKDF2"},
false,
["deriveKey"]
)
.then(function(baseKey) {
return window.crypto.subtle.deriveKey(
{
"name": "PBKDF2",
"salt": stringToArrayBuffer(salt),
"iterations": rounds,
"hash": "SHA-256"
},
baseKey,
{"name": "AES-CBC", "length": bits},
true,
["encrypt", "decrypt"]
);
})
.then(function(aesKey) {
return window.crypto.subtle.exportKey("raw", aesKey);
});
// .then(function(keyBytes) {
// return arrayBufferToHexString(keyBytes);
// })
},

patchCorePBKDF: function() {
window.Buttercup.vendor.iocane.components.setPBKDF2(lib.deriveKeyFromPassword);
}

};

})(module);
15 changes: 13 additions & 2 deletions source/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,26 @@
window.Buttercup = require("buttercup");

var ArchiveManager = require("__buttercup_web/ArchiveManager.js"),
ArchiveTools = require("__buttercup_web/ArchiveTools.js");
ArchiveTools = require("__buttercup_web/ArchiveTools.js"),
HashingTools = require("__buttercup_web/HashingTools.js");

// BEGIN initialisation

HashingTools.patchCorePBKDF();

// END initialisation

// Export:

window.Buttercup.Web = {

ArchiveManager: ArchiveManager,

archiveManager: new ArchiveManager(),

ArchiveTools: ArchiveTools
ArchiveTools: ArchiveTools,

HashingTools: HashingTools

};

Expand Down

0 comments on commit 3770deb

Please sign in to comment.