Skip to content

Commit

Permalink
Add crc32 for jsFile included for tamperproofing
Browse files Browse the repository at this point in the history
  • Loading branch information
mascot6699 committed Jan 17, 2020
1 parent 9dceefd commit 6f388aa
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion sdk/src/main/java/com/crux/sdk/bridge/CruxJSBridge.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public class CruxJSBridge {
private final JSContext jsContext;
private JSObject jsClient;
private final String cruxJsFileName = "cruxpay-0.1.9.js";
private final Long cruxJsFileCheckSum = new Long(349195419);


public CruxJSBridge(CruxClientInitConfig.Builder configBuilder, Context androidContextObject) throws IOException, CruxClientError {
Expand Down Expand Up @@ -45,8 +46,11 @@ private void prepareCruxClientInitConfig(CruxClientInitConfig.Builder configBuil
configBuilder = null;
}

private JSContext getContextForClient(Context androidContextObject) throws IOException {
private JSContext getContextForClient(Context androidContextObject) throws IOException, CruxClientError {
String sdkFile = GenericUtils.getFromFile(androidContextObject, cruxJsFileName);
if (GenericUtils.crc32(sdkFile) != cruxJsFileCheckSum) {
throw CruxClientError.getCruxClientError(AndroidCruxClientErrorCode.runningInUnsafeEnvironment);
}
JSContext jsContext = new JSContext();
JSPolyFill.fixConsoleLog(jsContext);
JSPolyFill.addFetch(jsContext, androidContextObject);
Expand Down
10 changes: 10 additions & 0 deletions sdk/src/main/java/com/crux/sdk/bridge/GenericUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.zip.CRC32;
import java.util.zip.Checksum;

import org.liquidplayer.javascript.JSValue;

Expand Down Expand Up @@ -47,5 +49,13 @@ else if (val.isObject())
return null;
}

public static long crc32(String input) {
byte[] bytes = input.getBytes();
Checksum checksum = new CRC32(); // java.util.zip.CRC32
checksum.update(bytes, 0, bytes.length);

return checksum.getValue();
}

}

0 comments on commit 6f388aa

Please sign in to comment.