Skip to content

Commit 8465dcf

Browse files
feat: move checksumText to native
1 parent 8f93334 commit 8465dcf

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

src/lib/installState.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,13 @@ async function checksum(data) {
159159
* @returns
160160
*/
161161
async function checksumText(text) {
162-
const textUint8 = new TextEncoder().encode(text);
163-
return await checksum(textUint8);
162+
return new Promise((resolve, reject) => {
163+
cordova.exec(
164+
(hash) => resolve(hash),
165+
(error) => reject(error),
166+
"System",
167+
"checksumText",
168+
[text],
169+
);
170+
});
164171
}

src/plugins/system/android/com/foxdebug/system/System.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,13 @@
107107
import android.graphics.ImageDecoder;
108108

109109

110+
import org.apache.cordova.*;
111+
import org.json.JSONArray;
112+
import org.json.JSONException;
113+
114+
import java.security.MessageDigest;
115+
116+
110117

111118
public class System extends CordovaPlugin {
112119
private static final String TAG = "SystemPlugin";
@@ -560,6 +567,33 @@ public void run() {
560567
break;
561568
case "compare-texts":
562569
compareTexts(arg1, arg2, callbackContext);
570+
break;
571+
case "checksumText":
572+
573+
cordova.getThreadPool().execute(() -> {
574+
try {
575+
576+
MessageDigest digest = MessageDigest.getInstance("SHA-256");
577+
578+
byte[] hash = digest.digest(args.getString(0).getBytes("UTF-8"));
579+
580+
StringBuilder hexString = new StringBuilder();
581+
582+
for (byte b : hash) {
583+
String hex = Integer.toHexString(0xff & b);
584+
585+
if (hex.length() == 1) hexString.append('0');
586+
587+
hexString.append(hex);
588+
}
589+
590+
591+
callbackContext.success(hexString.toString());
592+
} catch (Exception e) {
593+
callbackContext.error(e.getMessage());
594+
}
595+
});
596+
563597
break;
564598
default:
565599
break;

0 commit comments

Comments
 (0)