2013-11-29 20:39:42 +00:00
|
|
|
define(function(require) {
|
|
|
|
|
|
|
|
// AES encryption, see http://code.google.com/p/crypto-js/#AES
|
|
|
|
require("crypto-js/rollups/aes");
|
|
|
|
// DES, Triple DES, see http://code.google.com/p/crypto-js/#DES,_Triple_DES
|
|
|
|
require("crypto-js/rollups/tripledes");
|
2013-12-01 21:41:04 +00:00
|
|
|
// Rabbit, see http://code.google.com/p/crypto-js/#Rabbit
|
2013-11-29 20:39:42 +00:00
|
|
|
require("crypto-js/rollups/rabbit");
|
|
|
|
|
2013-12-03 20:23:47 +00:00
|
|
|
// Move back and forth from Uint8Arrays and CryptoJS' WordArray
|
|
|
|
// source: https://groups.google.com/forum/#!topic/crypto-js/TOb92tcJlU0
|
|
|
|
Uint8ArrayFormatter = {
|
|
|
|
fromWordArray: function (wordArray) {
|
|
|
|
// Shortcuts
|
|
|
|
var words = wordArray.words;
|
|
|
|
var sigBytes = wordArray.sigBytes;
|
|
|
|
|
|
|
|
// Convert
|
|
|
|
var u8 = new Uint8Array(sigBytes);
|
|
|
|
for (var i = 0; i < sigBytes; i++) {
|
|
|
|
var byte = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
|
|
|
|
u8[i]=byte;
|
|
|
|
}
|
|
|
|
console.log("stringify", wordArray, u8);
|
|
|
|
return u8;
|
|
|
|
},
|
|
|
|
|
|
|
|
toWordArray: function (u8arr) {
|
|
|
|
// Shortcut
|
|
|
|
var len = u8arr.length;
|
|
|
|
|
|
|
|
// Convert
|
|
|
|
var words = [];
|
|
|
|
for (var i = 0; i < len; i++) {
|
|
|
|
words[i >>> 2] |= (u8arr[i] & 0xff) << (24 - (i % 4) * 8);
|
|
|
|
}
|
|
|
|
console.log("parse", u8arr, CryptoJS.lib.WordArray.create(words, len));
|
|
|
|
return CryptoJS.lib.WordArray.create(words, len);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-29 21:43:28 +00:00
|
|
|
|
2013-12-01 21:41:04 +00:00
|
|
|
function CryptoContext(context, encrypt, decrypt) {
|
2013-11-29 20:39:42 +00:00
|
|
|
this.context = context;
|
|
|
|
this.encrypt = encrypt;
|
|
|
|
this.decrypt = decrypt;
|
|
|
|
}
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoContext.prototype.clear = function(callback) {
|
2013-11-29 20:39:42 +00:00
|
|
|
this.context.clear(callback);
|
|
|
|
};
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoContext.prototype.get = function(key, callback) {
|
|
|
|
var decrypt = this.decrypt;
|
2013-11-29 21:00:41 +00:00
|
|
|
this.context.get(key, function(err, value) {
|
2013-11-29 20:39:42 +00:00
|
|
|
if(err) {
|
|
|
|
callback(err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if(value) {
|
2013-12-03 20:23:47 +00:00
|
|
|
value = Uint8ArrayFormatter.fromWordArray(decrypt(value));
|
2013-11-29 20:39:42 +00:00
|
|
|
}
|
|
|
|
callback(null, value);
|
|
|
|
});
|
|
|
|
};
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoContext.prototype.put = function(key, value, callback) {
|
2013-11-29 20:39:42 +00:00
|
|
|
var encryptedValue = this.encrypt(value);
|
2013-11-29 21:00:41 +00:00
|
|
|
this.context.put(key, encryptedValue, callback);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoContext.prototype.delete = function(key, callback) {
|
2013-11-29 21:00:41 +00:00
|
|
|
this.context.delete(key, callback);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2013-12-01 21:41:04 +00:00
|
|
|
function buildCryptoAdapter(encryptionType) {
|
2013-11-29 20:39:42 +00:00
|
|
|
// It is up to the app using this wrapper how the passphrase is acquired, probably by
|
|
|
|
// prompting the user to enter it when the file system is being opened.
|
2013-12-01 21:41:04 +00:00
|
|
|
function CryptoAdapter(passphrase, provider) {
|
2013-11-29 20:39:42 +00:00
|
|
|
this.provider = provider;
|
|
|
|
this.encrypt = function(plain) {
|
2013-12-03 20:14:20 +00:00
|
|
|
return CryptoJS[encryptionType]
|
2013-12-03 20:23:47 +00:00
|
|
|
.encrypt(Uint8ArrayFormatter.toWordArray(plain), passphrase)
|
2013-12-03 20:14:20 +00:00
|
|
|
.toString();
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
this.decrypt = function(encrypted) {
|
2013-12-03 20:14:20 +00:00
|
|
|
return CryptoJS[encryptionType]
|
2013-12-03 20:23:47 +00:00
|
|
|
.decrypt(encrypted, passphrase)
|
|
|
|
.toString();
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
}
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoAdapter.isSupported = function() {
|
2013-11-29 20:39:42 +00:00
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoAdapter.prototype.open = function(callback) {
|
2013-11-29 20:39:42 +00:00
|
|
|
this.provider.open(callback);
|
|
|
|
};
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoAdapter.prototype.getReadOnlyContext = function() {
|
|
|
|
return new CryptoContext(this.provider.getReadOnlyContext(),
|
|
|
|
this.encrypt,
|
|
|
|
this.decrypt);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
2013-12-01 21:41:04 +00:00
|
|
|
CryptoAdapter.prototype.getReadWriteContext = function() {
|
|
|
|
return new CryptoContext(this.provider.getReadWriteContext(),
|
|
|
|
this.encrypt,
|
|
|
|
this.decrypt);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
|
2013-12-01 21:41:04 +00:00
|
|
|
return CryptoAdapter;
|
2013-11-29 20:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
2013-12-01 21:41:04 +00:00
|
|
|
AES: buildCryptoAdapter('AES'),
|
|
|
|
TripleDES: buildCryptoAdapter('TripleDES'),
|
|
|
|
Rabbit: buildCryptoAdapter('Rabbit')
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
});
|