1
0
mirror of https://github.com/Jermolene/TiddlyWiki5 synced 2024-11-26 19:47:20 +00:00

Rejigged encrypted import so that the current password isn't changed

We still try the currently stored password. If that doesn’t work then
we prompt for a password, but we no longer store the password in the
store.
This commit is contained in:
Jermolene 2014-01-20 13:35:55 +00:00
parent f31369035b
commit 2fc6451bf7
2 changed files with 17 additions and 15 deletions

View File

@ -545,8 +545,9 @@ the password, and to encrypt/decrypt a block of text
*/
$tw.utils.Crypto = function() {
var sjcl = $tw.browser ? window.sjcl : require("./sjcl.js"),
password = null,
callSjcl = function(method,inputText) {
currentPassword = null,
callSjcl = function(method,inputText,password) {
password = password || currentPassword;
var outputText;
try {
if(password) {
@ -559,22 +560,22 @@ $tw.utils.Crypto = function() {
return outputText;
};
this.setPassword = function(newPassword) {
password = newPassword;
currentPassword = newPassword;
this.updateCryptoStateTiddler();
};
this.updateCryptoStateTiddler = function() {
if($tw.wiki && $tw.wiki.addTiddler) {
$tw.wiki.addTiddler(new $tw.Tiddler({title: "$:/isEncrypted", text: password ? "yes" : "no"}));
$tw.wiki.addTiddler(new $tw.Tiddler({title: "$:/isEncrypted", text: currentPassword ? "yes" : "no"}));
}
};
this.hasPassword = function() {
return !!password;
return !!currentPassword;
}
this.encrypt = function(text) {
return callSjcl("encrypt",text);
this.encrypt = function(text,password) {
return callSjcl("encrypt",text,password);
};
this.decrypt = function(text) {
return callSjcl("decrypt",text);
this.decrypt = function(text,password) {
return callSjcl("decrypt",text,password);
};
};

View File

@ -28,15 +28,17 @@ exports.extractEncryptedStoreArea = function(text) {
};
/*
Attempt to extract the tiddlers from an encrypted store area using the current password
Attempt to extract the tiddlers from an encrypted store area using the current password. If the password is not provided then the password in the password store will be used
*/
exports.decryptStoreArea = function(encryptedStoreArea) {
var decryptedText = $tw.crypto.decrypt(encryptedStoreArea);
exports.decryptStoreArea = function(encryptedStoreArea,password) {
var decryptedText = $tw.crypto.decrypt(encryptedStoreArea,password);
if(decryptedText) {
var json = JSON.parse(decryptedText),
tiddlers = [];
for(var title in json) {
tiddlers.push(json[title]);
if(title !== "$:/isEncrypted") {
tiddlers.push(json[title]);
}
}
return tiddlers;
} else {
@ -62,8 +64,7 @@ exports.decryptStoreAreaInteractive = function(encryptedStoreArea,callback) {
return false;
}
// Attempt to decrypt the tiddlers
$tw.crypto.setPassword(data.password);
var tiddlers = $tw.utils.decryptStoreArea(encryptedStoreArea);
var tiddlers = $tw.utils.decryptStoreArea(encryptedStoreArea,data.password);
if(tiddlers) {
callback(tiddlers);
// Exit and remove the password prompt