export aes encrypt and decrypt methof in jsb_global.h

This commit is contained in:
CounterFire2023 2023-08-15 15:15:14 +08:00
parent 72151488a1
commit 18b5d82244
2 changed files with 545 additions and 472 deletions

View File

@ -37,7 +37,6 @@ extern "C"
#include "rustwallet/rustwallet.h"
}
#if CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID
#include "platform/android/jni/JniImp.h"
#endif // CC_PLATFORM_ANDROID
@ -67,7 +66,8 @@ static cocos2d::network::Downloader *localDownloader()
{
g_localDownloader = std::make_shared<cocos2d::network::Downloader>();
g_localDownloader->onDataTaskSuccess = [=](const cocos2d::network::DownloadTask &task,
std::vector<unsigned char>& data) {
std::vector<unsigned char> &data)
{
if (data.empty())
{
SE_REPORT_ERROR("Getting image from (%s) failed!", task.requestURL.c_str());
@ -91,8 +91,8 @@ static cocos2d::network::Downloader *localDownloader()
g_localDownloader->onTaskError = [=](const cocos2d::network::DownloadTask &task,
int errorCode,
int errorCodeInternal,
const std::string& errorStr) {
const std::string &errorStr)
{
SE_REPORT_ERROR("Getting image from (%s) failed!", task.requestURL.c_str());
auto callback = g_localDownloaderHandlers.find(task.identifier);
if (callback == g_localDownloaderHandlers.end())
@ -108,7 +108,6 @@ static cocos2d::network::Downloader *localDownloader()
return g_localDownloader.get();
}
static const char *BYTE_CODE_FILE_EXT = ".jsc";
static std::string removeFileExt(const std::string &filePath)
@ -126,28 +125,33 @@ void jsb_init_file_operation_delegate()
static se::ScriptEngine::FileOperationDelegate delegate;
if (!delegate.isValid())
{
delegate.onGetDataFromFile = [](const std::string& path, const std::function<void(const uint8_t*, size_t)>& readCallback) -> void{
delegate.onGetDataFromFile = [](const std::string &path, const std::function<void(const uint8_t *, size_t)> &readCallback) -> void
{
assert(!path.empty());
Data fileData;
std::string byteCodePath = removeFileExt(path) + BYTE_CODE_FILE_EXT;
if (FileUtils::getInstance()->isFileExist(byteCodePath)) {
if (FileUtils::getInstance()->isFileExist(byteCodePath))
{
fileData = FileUtils::getInstance()->getDataFromFile(byteCodePath);
size_t dataLen = 0;
uint8_t *data = xxtea_decrypt((unsigned char *)fileData.getBytes(), (uint32_t)fileData.getSize(), (unsigned char *)xxteaKey.c_str(), (uint32_t)xxteaKey.size(), (uint32_t *)&dataLen);
if (data == nullptr) {
if (data == nullptr)
{
SE_REPORT_ERROR("Can't decrypt code for %s", byteCodePath.c_str());
return;
}
if (ZipUtils::isGZipBuffer(data,dataLen)) {
if (ZipUtils::isGZipBuffer(data, dataLen))
{
uint8_t *unpackedData;
ssize_t unpackedLen = ZipUtils::inflateMemory(data, dataLen, &unpackedData);
if (unpackedData == nullptr) {
if (unpackedData == nullptr)
{
SE_REPORT_ERROR("Can't decrypt code for %s", byteCodePath.c_str());
return;
}
@ -156,7 +160,8 @@ void jsb_init_file_operation_delegate()
free(data);
free(unpackedData);
}
else {
else
{
readCallback(data, dataLen);
free(data);
}
@ -168,25 +173,30 @@ void jsb_init_file_operation_delegate()
readCallback(fileData.getBytes(), fileData.getSize());
};
delegate.onGetStringFromFile = [](const std::string& path) -> std::string{
delegate.onGetStringFromFile = [](const std::string &path) -> std::string
{
assert(!path.empty());
std::string byteCodePath = removeFileExt(path) + BYTE_CODE_FILE_EXT;
if (FileUtils::getInstance()->isFileExist(byteCodePath)) {
if (FileUtils::getInstance()->isFileExist(byteCodePath))
{
Data fileData = FileUtils::getInstance()->getDataFromFile(byteCodePath);
uint32_t dataLen;
uint8_t *data = xxtea_decrypt((uint8_t *)fileData.getBytes(), (uint32_t)fileData.getSize(), (uint8_t *)xxteaKey.c_str(), (uint32_t)xxteaKey.size(), &dataLen);
if (data == nullptr) {
if (data == nullptr)
{
SE_REPORT_ERROR("Can't decrypt code for %s", byteCodePath.c_str());
return "";
}
if (ZipUtils::isGZipBuffer(data,dataLen)) {
if (ZipUtils::isGZipBuffer(data, dataLen))
{
uint8_t *unpackedData;
ssize_t unpackedLen = ZipUtils::inflateMemory(data, dataLen, &unpackedData);
if (unpackedData == nullptr) {
if (unpackedData == nullptr)
{
SE_REPORT_ERROR("Can't decrypt code for %s", byteCodePath.c_str());
return "";
}
@ -197,32 +207,38 @@ void jsb_init_file_operation_delegate()
return ret;
}
else {
else
{
std::string ret(reinterpret_cast<const char *>(data), dataLen);
free(data);
return ret;
}
}
if (FileUtils::getInstance()->isFileExist(path)) {
if (FileUtils::getInstance()->isFileExist(path))
{
return FileUtils::getInstance()->getStringFromFile(path);
}
else {
else
{
SE_LOGE("ScriptEngine::onGetStringFromFile %s not found, possible missing file.\n", path.c_str());
}
return "";
};
delegate.onGetFullPath = [](const std::string& path) -> std::string{
delegate.onGetFullPath = [](const std::string &path) -> std::string
{
assert(!path.empty());
std::string byteCodePath = removeFileExt(path) + BYTE_CODE_FILE_EXT;
if (FileUtils::getInstance()->isFileExist(byteCodePath)) {
if (FileUtils::getInstance()->isFileExist(byteCodePath))
{
return FileUtils::getInstance()->fullPathForFilename(byteCodePath);
}
return FileUtils::getInstance()->fullPathForFilename(path);
};
delegate.onCheckFileExist = [](const std::string& path) -> bool{
delegate.onCheckFileExist = [](const std::string &path) -> bool
{
assert(!path.empty());
return FileUtils::getInstance()->isFileExist(path);
};
@ -294,7 +310,8 @@ bool jsb_set_extend_property(const char* ns, const char* clsName)
return false;
}
namespace {
namespace
{
std::unordered_map<std::string, se::Value> __moduleCache;
static bool require(se::State &s)
@ -351,7 +368,6 @@ static bool doModuleRequire(const std::string& path, se::Value* ret, const std::
fullPath = fileOperationDelegate.onGetFullPath(pathWithSuffix);
}
if (!scriptBuffer.empty())
{
const auto &iter = __moduleCache.find(fullPath);
@ -390,7 +406,6 @@ static bool doModuleRequire(const std::string& path, se::Value* ret, const std::
}
#endif
// RENDERER_LOGD("Evaluate: %s", fullPath.c_str());
auto se = se::ScriptEngine::getInstance();
@ -446,7 +461,8 @@ bool jsb_run_script(const std::string& filePath, se::Value* rval/* = nullptr */)
return se::ScriptEngine::getInstance()->runScript(filePath, rval);
}
bool jsb_run_code(const std::string& coder, se::Value* rval/* = nullptr */) {
bool jsb_run_code(const std::string &coder, se::Value *rval /* = nullptr */)
{
se::AutoHandleScope hs;
return se::ScriptEngine::getInstance()->evalString(coder.c_str(), coder.size(), rval);
}
@ -485,7 +501,8 @@ static bool jsc_dumpNativePtrToSeObjectMap(se::State& s)
namePtrArray.push_back(tmp);
}
std::sort(namePtrArray.begin(), namePtrArray.end(), [](const NamePtrStruct& a, const NamePtrStruct& b) -> bool {
std::sort(namePtrArray.begin(), namePtrArray.end(), [](const NamePtrStruct &a, const NamePtrStruct &b) -> bool
{
std::string left = a.name;
std::string right = b.name;
for( std::string::const_iterator lit = left.begin(), rit = right.begin(); lit != left.end() && rit != right.end(); ++lit, ++rit )
@ -495,8 +512,7 @@ static bool jsc_dumpNativePtrToSeObjectMap(se::State& s)
return false;
if( left.size() < right.size() )
return true;
return false;
});
return false; });
for (const auto &e : namePtrArray)
{
@ -720,13 +736,13 @@ static bool js_performance_now(se::State& s)
}
SE_BIND_FUNC(js_performance_now)
static bool JSB_openURL(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string url;
ok = seval_to_std_string(args[0], &url);
SE_PRECONDITION2(ok, false, "url is invalid!");
@ -744,7 +760,8 @@ static bool JSB_copyTextToClipboard(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string text;
ok = seval_to_std_string(args[0], &text);
SE_PRECONDITION2(ok, false, "text is invalid!");
@ -762,7 +779,8 @@ static bool JSB_setPreferredFramesPerSecond(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
int32_t fps;
ok = seval_to_int32(args[0], &fps);
SE_PRECONDITION2(ok, false, "fps is invalid!");
@ -802,7 +820,8 @@ static bool JSB_cryptoScrypt(se::State& s)
SE_PRECONDITION2(ok, false, "Error processing arguments");
std::string value;
int result = crypto_scrypt_base64(&password, &salt, n, r, p, size, &value);
if (result == 0) {
if (result == 0)
{
s.rval().setString(value);
}
else
@ -821,7 +840,8 @@ static bool JSB_scanQRCode(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1) {
if (argc > 1)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -843,7 +863,8 @@ static bool JSB_signWithGoogle(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -860,7 +881,8 @@ static bool JSB_signWithApple(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -878,7 +900,8 @@ static bool JSB_signWithTiktok(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -895,7 +918,8 @@ static bool JSB_signWithFacebook(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -912,7 +936,8 @@ static bool JSB_signWithTwitter(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -929,7 +954,8 @@ static bool JSB_signWithEmail(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -946,7 +972,8 @@ static bool JSB_callJcVoidMethodJNI(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1) {
if (argc > 1)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -965,7 +992,8 @@ static bool JSB_signOutGoogle(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -982,7 +1010,8 @@ static bool JSB_showQRCode(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc >= 2) {
if (argc >= 2)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -997,10 +1026,12 @@ static bool JSB_showQRCode(se::State& s)
}
SE_BIND_FUNC(JSB_showQRCode)
bool JSB_showWebPage(se::State& s) {
bool JSB_showWebPage(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
if (argc >= 2) {
if (argc >= 2)
{
bool ok;
std::string funid;
ok = seval_to_std_string(args[0], &funid);
@ -1020,7 +1051,8 @@ static bool JSB_queryProductsJNI(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1) {
if (argc > 1)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -1040,7 +1072,8 @@ static bool JSB_queryPurchaseJNI(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -1052,13 +1085,13 @@ static bool JSB_queryPurchaseJNI(se::State& s)
}
SE_BIND_FUNC(JSB_queryPurchaseJNI)
static bool JSB_beginBuyJNI(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 2) {
if (argc > 2)
{
std::string funid;
ok = seval_to_std_string(args[0], &funid);
SE_PRECONDITION2(ok, false, "funid is invalid!");
@ -1082,7 +1115,8 @@ static bool JSB_toWallet(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string text;
ok = seval_to_std_string(args[0], &text);
SE_PRECONDITION2(ok, false, "url is invalid!");
@ -1100,7 +1134,8 @@ static bool JSB_prepareWallet(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 5) {
if (argc == 5)
{
std::string id;
ok = seval_to_std_string(args[0], &id);
SE_PRECONDITION2(ok, false, "Error processing oid");
@ -1124,8 +1159,7 @@ static bool JSB_prepareWallet(se::State& s)
std::string address = get_address(
Application::getInstance()->getKeySeed(),
Application::getInstance()->getKeyMaster(),
Application::getInstance()->getKeySecond()
);
Application::getInstance()->getKeySecond());
s.rval().setString(address);
return true;
}
@ -1140,7 +1174,8 @@ static bool JSB_walletSecKey(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc == 5) {
if (argc == 5)
{
std::string id;
ok = seval_to_std_string(args[0], &id);
SE_PRECONDITION2(ok, false, "Error processing oid");
@ -1161,17 +1196,14 @@ static bool JSB_walletSecKey(se::State& s)
std::string address = get_address(
seed.c_str(),
keyMaster.c_str(),
localSKey.c_str()
);
localSKey.c_str());
std::string key_str = generate_sec_key(
seed.c_str(),
keyMaster.c_str(),
localSKey.c_str()
);
localSKey.c_str());
std::string result = StringUtils::format("{\"address\": \"%s\",\"key\": \"%s\"}\n",
address.c_str(),
key_str.c_str()
);
key_str.c_str());
s.rval().setString(result);
return true;
@ -1182,13 +1214,13 @@ static bool JSB_walletSecKey(se::State& s)
}
SE_BIND_FUNC(JSB_walletSecKey)
static bool JSB_hashSvrPass(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string pass;
ok = seval_to_std_string(args[0], &pass);
SE_PRECONDITION2(ok, false, "Error processing pass");
@ -1207,7 +1239,8 @@ static bool JSB_walletSign(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing seed");
@ -1225,12 +1258,35 @@ static bool JSB_walletSign(se::State& s)
}
SE_BIND_FUNC(JSB_walletSign)
static bool JSB_saveLocalStorage(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1)
{
std::string key;
ok = seval_to_std_string(args[0], &key);
SE_PRECONDITION2(ok, false, "Error processing key");
std::string val;
ok = seval_to_std_string(args[1], &val);
SE_PRECONDITION2(ok, false, "Error processing val");
Application::getInstance()->saveKeyLocal(key, val);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 2);
return false;
}
SE_BIND_FUNC(JSB_saveLocalStorage)
static bool JSB_loadLocalStorage(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string key;
ok = seval_to_std_string(args[0], &key);
SE_PRECONDITION2(ok, false, "Error processing seed");
@ -1251,7 +1307,8 @@ static bool JSB_walletSignTran(se::State& s)
size_t argc = args.size();
CC_UNUSED bool ok = true;
CCLOG("JSB_walletSignTran\n");
if (argc > 0) {
if (argc > 0)
{
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing seed");
@ -1313,7 +1370,8 @@ static bool JSB_walletEncrypt(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing msg");
@ -1336,7 +1394,8 @@ static bool JSB_rencrypt(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 1) {
if (argc > 1)
{
std::string pk;
ok = seval_to_std_string(args[0], &pk);
SE_PRECONDITION2(ok, false, "Error processing pk");
@ -1360,7 +1419,8 @@ static bool JSB_walletDecrypt(se::State& s)
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0) {
if (argc > 0)
{
std::string msg;
ok = seval_to_std_string(args[0], &msg);
SE_PRECONDITION2(ok, false, "Error processing seed");
@ -1378,6 +1438,15 @@ static bool JSB_walletDecrypt(se::State& s)
}
SE_BIND_FUNC(JSB_walletDecrypt)
std::string encrypt_aes(std::string &content, std::string &key)
{
return aes_encrypt(content.c_str(), key.c_str());
}
std::string decrypt_aes(std::string &content, std::string &key)
{
return aes_decrypt(content.c_str(), key.c_str());
}
bool jsb_register_global_variables(se::Object *global)
{
@ -1429,6 +1498,7 @@ bool jsb_register_global_variables(se::Object* global)
__jsbObj->defineFunction("walletEncrypt", _SE(JSB_walletEncrypt));
__jsbObj->defineFunction("walletDecrypt", _SE(JSB_walletDecrypt));
__jsbObj->defineFunction("loadLocalStorage", _SE(JSB_loadLocalStorage));
__jsbObj->defineFunction("saveLocalStorage", _SE(JSB_saveLocalStorage));
__jsbObj->defineFunction("setPreferredFramesPerSecond", _SE(JSB_setPreferredFramesPerSecond));
@ -1449,21 +1519,21 @@ bool jsb_register_global_variables(se::Object* global)
se::ScriptEngine::getInstance()->clearException();
se::ScriptEngine::getInstance()->addBeforeCleanupHook([](){
se::ScriptEngine::getInstance()->addBeforeCleanupHook([]()
{
g_threadPool = nullptr;
PoolManager::getInstance()->getCurrentPool()->clear();
});
PoolManager::getInstance()->getCurrentPool()->clear(); });
se::ScriptEngine::getInstance()->addAfterCleanupHook([](){
se::ScriptEngine::getInstance()->addAfterCleanupHook([]()
{
PoolManager::getInstance()->getCurrentPool()->clear();
__moduleCache.clear();
SAFE_DEC_REF(__jsbObj);
SAFE_DEC_REF(__glObj);
});
SAFE_DEC_REF(__glObj); });
return true;
}

View File

@ -48,3 +48,6 @@ bool jsb_run_script_module(const std::string& filePath, se::Value* rval = nullpt
void jsb_set_xxtea_key(const std::string& key);
bool jsb_global_load_image(const std::string& path, const se::Value& callbackVal);
std::string encrypt_aes(std::string& content, std::string& key);
std::string decrypt_aes(std::string& content, std::string& key);