2010-02-25 08:18:35 +08:00
|
|
|
package com.phonegap;
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
import org.json.JSONArray;
|
|
|
|
import org.json.JSONException;
|
|
|
|
|
2010-09-08 02:59:54 +08:00
|
|
|
import com.phonegap.api.Plugin;
|
|
|
|
import com.phonegap.api.PluginResult;
|
2010-09-07 02:13:09 +08:00
|
|
|
|
2010-10-05 12:58:14 +08:00
|
|
|
public class CryptoHandler extends Plugin {
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
/**
|
|
|
|
* Constructor.
|
|
|
|
*/
|
|
|
|
public CryptoHandler() {
|
2010-02-25 08:18:35 +08:00
|
|
|
}
|
2010-09-07 02:13:09 +08:00
|
|
|
|
|
|
|
/**
|
2010-10-06 09:35:51 +08:00
|
|
|
* Executes the request and returns PluginResult.
|
2010-09-07 02:13:09 +08:00
|
|
|
*
|
2010-10-06 09:35:51 +08:00
|
|
|
* @param action The action to execute.
|
|
|
|
* @param args JSONArry of arguments for the plugin.
|
|
|
|
* @param callbackId The callback id used when calling back into JavaScript.
|
|
|
|
* @return A PluginResult object with a status and message.
|
2010-09-07 02:13:09 +08:00
|
|
|
*/
|
2010-10-06 09:35:51 +08:00
|
|
|
public PluginResult execute(String action, JSONArray args, String callbackId) {
|
2010-09-08 02:59:54 +08:00
|
|
|
PluginResult.Status status = PluginResult.Status.OK;
|
2010-09-07 02:13:09 +08:00
|
|
|
String result = "";
|
|
|
|
|
|
|
|
try {
|
|
|
|
if (action.equals("encrypt")) {
|
|
|
|
this.encrypt(args.getString(0), args.getString(1));
|
|
|
|
}
|
|
|
|
else if (action.equals("decrypt")) {
|
|
|
|
this.decrypt(args.getString(0), args.getString(1));
|
|
|
|
}
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(status, result);
|
2010-09-07 02:13:09 +08:00
|
|
|
} catch (JSONException e) {
|
2010-09-08 02:59:54 +08:00
|
|
|
return new PluginResult(PluginResult.Status.JSON_EXCEPTION);
|
2010-09-07 02:13:09 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
// LOCAL METHODS
|
|
|
|
//--------------------------------------------------------------------------
|
|
|
|
|
|
|
|
public void encrypt(String pass, String text) {
|
2010-02-25 08:18:35 +08:00
|
|
|
try {
|
|
|
|
String encrypted = SimpleCrypto.encrypt(pass,text);
|
2010-09-07 02:13:09 +08:00
|
|
|
// TODO: Why not just return text now?
|
2010-10-05 12:58:14 +08:00
|
|
|
this.sendJavascript("Crypto.gotCryptedString('" + text + "')");
|
2010-02-25 08:18:35 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-09-07 02:13:09 +08:00
|
|
|
public void decrypt(String pass, String text) {
|
2010-02-25 08:18:35 +08:00
|
|
|
try {
|
|
|
|
String decrypted = SimpleCrypto.decrypt(pass,text);
|
2010-10-05 12:58:14 +08:00
|
|
|
this.sendJavascript("Crypto.gotPlainString('" + text + "')");
|
2010-02-25 08:18:35 +08:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|