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");
|
|
|
|
// Rabbit, see http://code.google.com/p/crypto-js/#Rabbi
|
|
|
|
require("crypto-js/rollups/rabbit");
|
|
|
|
|
2013-11-29 21:43:28 +00:00
|
|
|
|
2013-11-29 20:39:42 +00:00
|
|
|
function CryptoWrappedContext(context, encrypt, decrypt) {
|
|
|
|
this.context = context;
|
|
|
|
this.encrypt = encrypt;
|
|
|
|
this.decrypt = decrypt;
|
|
|
|
}
|
|
|
|
CryptoWrappedContext.prototype.clear = function(callback) {
|
|
|
|
this.context.clear(callback);
|
|
|
|
};
|
|
|
|
CryptoWrappedContext.prototype.get = function(key, callback) {
|
|
|
|
var that = this;
|
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) {
|
|
|
|
value = that.decrypt(value);
|
|
|
|
}
|
|
|
|
callback(null, value);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
CryptoWrappedContext.prototype.put = function(key, value, callback) {
|
|
|
|
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
|
|
|
};
|
|
|
|
CryptoWrappedContext.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
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
function buildCryptoWrapper(encryptionType) {
|
|
|
|
// 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.
|
|
|
|
function CryptoWrappedProvider(passphrase, provider) {
|
|
|
|
this.provider = provider;
|
|
|
|
this.encrypt = function(plain) {
|
2013-11-29 21:43:28 +00:00
|
|
|
return CryptoJS[encryptionType].encrypt(plain, passphrase)
|
|
|
|
.toString();
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
this.decrypt = function(encrypted) {
|
2013-11-29 21:43:28 +00:00
|
|
|
return CryptoJS[encryptionType].decrypt(encrypted, passphrase)
|
|
|
|
.toString(CryptoJS.enc.Utf8);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
CryptoWrappedProvider.isSupported = function() {
|
|
|
|
return true;
|
|
|
|
};
|
|
|
|
|
|
|
|
CryptoWrappedProvider.prototype.open = function(callback) {
|
|
|
|
this.provider.open(callback);
|
|
|
|
};
|
|
|
|
CryptoWrappedProvider.prototype.getReadOnlyContext = function() {
|
2013-11-29 21:43:28 +00:00
|
|
|
return new CryptoWrappedContext(this.provider.getReadOnlyContext(),
|
|
|
|
this.encrypt,
|
|
|
|
this.decrypt);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
CryptoWrappedProvider.prototype.getReadWriteContext = function() {
|
2013-11-29 21:43:28 +00:00
|
|
|
return new CryptoWrappedContext(this.provider.getReadWriteContext(),
|
|
|
|
this.encrypt,
|
|
|
|
this.decrypt);
|
2013-11-29 20:39:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
return CryptoWrappedProvider;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
AESWrapper: buildCryptoWrapper('AES'),
|
|
|
|
TripleDESWrapper: buildCryptoWrapper('TripleDES'),
|
|
|
|
RabbitWrapper: buildCryptoWrapper('Rabbit')
|
|
|
|
};
|
|
|
|
});
|