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-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-01 21:41:04 +00:00
|
|
|
value = 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-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
|
|
|
};
|
|
|
|
}
|
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
|
|
|
};
|
|
|
|
});
|