增加aes相关方法

This commit is contained in:
CounterFire2023 2023-10-20 15:56:45 +08:00
parent 0c6d9286d3
commit 3e00bfb18f
14 changed files with 118 additions and 316 deletions

View File

@ -162,313 +162,6 @@ extern "C"
return JNI_VERSION_1_4;
}
/*****************************************************
* Cocos2dxActivity native functions implementation.
*****************************************************/
JNIEXPORT jintArray JNICALL JNI_ACTIVITY(getGLContextAttrs)(JNIEnv *env, jclass thiz)
{
// REFINE
int tmp[7] = {8, 8, 8,
8, 0, 0, 0};
jintArray glContextAttrsJava = env->NewIntArray(7);
env->SetIntArrayRegion(glContextAttrsJava, 0, 7, tmp);
return glContextAttrsJava;
}
/*****************************************************
* Cocos2dxRenderer native functions implementation.
*****************************************************/
JNIEXPORT void JNICALL JNI_RENDER(nativeInit)(JNIEnv *env, jclass thiz, jint w, jint h, jstring jDefaultResourcePath)
{
g_width = w;
g_height = h;
g_app = cocos_android_app_init(w, h);
g_isGameFinished = false;
ccInvalidateStateCache();
std::string defaultResourcePath = JniHelper::jstring2string(jDefaultResourcePath);
LOGD("nativeInit: %d, %d, %s", w, h, defaultResourcePath.c_str());
if (!defaultResourcePath.empty())
FileUtils::getInstance()->setDefaultResourceRootPath(defaultResourcePath);
se::ScriptEngine *se = se::ScriptEngine::getInstance();
se->addRegisterCallback(setCanvasCallback);
EventDispatcher::init();
g_app->start();
g_isStarted = true;
}
JNIEXPORT void JNICALL JNI_RENDER(nativeRender)(JNIEnv *env)
{
if (g_isGameFinished)
{
// with Application destructor called, native resource will be released
delete g_app;
g_app = nullptr;
JniHelper::callStaticVoidMethod(JCLS_HELPER, "endApplication");
return;
}
if (!g_isStarted)
{
auto scheduler = Application::getInstance()->getScheduler();
scheduler->removeAllFunctionsToBePerformedInCocosThread();
scheduler->unscheduleAll();
se::ScriptEngine::getInstance()->cleanup();
cocos2d::PoolManager::getInstance()->getCurrentPool()->clear();
// REFINE: Wait HttpClient, WebSocket, Audio thread to exit
ccInvalidateStateCache();
se::ScriptEngine *se = se::ScriptEngine::getInstance();
se->addRegisterCallback(setCanvasCallback);
EventDispatcher::init();
if (!g_app->applicationDidFinishLaunching())
{
g_isGameFinished = true;
return;
}
g_isStarted = true;
}
static std::chrono::steady_clock::time_point prevTime;
static std::chrono::steady_clock::time_point now;
static float dt = 0.f;
static float dtSum = 0.f;
static uint32_t jsbInvocationTotalCount = 0;
static uint32_t jsbInvocationTotalFrames = 0;
bool downsampleEnabled = g_app->isDownsampleEnabled();
if (downsampleEnabled)
g_app->getRenderTexture()->prepare();
g_app->getScheduler()->update(dt);
EventDispatcher::dispatchTickEvent(dt);
if (downsampleEnabled)
g_app->getRenderTexture()->draw();
PoolManager::getInstance()->getCurrentPool()->clear();
now = std::chrono::steady_clock::now();
dt = std::chrono::duration_cast<std::chrono::microseconds>(now - prevTime).count() / 1000000.f;
prevTime = std::chrono::steady_clock::now();
if (_isOpenDebugView)
{
dtSum += dt;
++jsbInvocationTotalFrames;
jsbInvocationTotalCount += __jsbInvocationCount;
if (dtSum > 1.0f)
{
dtSum = 0.0f;
setJSBInvocationCountJNI(jsbInvocationTotalCount / jsbInvocationTotalFrames);
jsbInvocationTotalCount = 0;
jsbInvocationTotalFrames = 0;
}
}
__jsbInvocationCount = 0;
}
JNIEXPORT void JNICALL JNI_RENDER(nativeOnPause)()
{
if (g_isGameFinished)
{
return;
}
if (g_app)
g_app->onPause();
}
JNIEXPORT void JNICALL JNI_RENDER(nativeOnResume)()
{
if (g_isGameFinished)
{
return;
}
if (g_app)
g_app->onResume();
}
JNIEXPORT void JNICALL JNI_RENDER(nativeInsertText)(JNIEnv *env, jclass thiz, jstring text)
{
// REFINE
}
JNIEXPORT void JNICALL JNI_RENDER(nativeDeleteBackward)(JNIEnv *env, jclass thiz)
{
// REFINE
}
JNIEXPORT jstring JNICALL JNI_RENDER(nativeGetContentText)()
{
// REFINE
}
JNIEXPORT void JNICALL JNI_RENDER(nativeOnSurfaceChanged)(JNIEnv *env, jclass thiz, jint w, jint h)
{
// REFINE
}
/***********************************************************
* Cocos2dxAccelerometer native functions implementation.
***********************************************************/
JNIEXPORT void JNICALL JNI_ACCELEROMETER(onSensorChanged)(JNIEnv *env, jclass thiz, jfloat x, jfloat y, jfloat z, jlong timeStamp)
{
// REFINE
}
/***********************************************************
* Touches native functions implementation.
***********************************************************/
static void dispatchTouchEventWithOnePoint(JNIEnv *env, cocos2d::TouchEvent::Type type, jint id, jfloat x, jfloat y)
{
if (g_isGameFinished)
{
return;
}
cocos2d::TouchEvent touchEvent;
touchEvent.type = type;
uint8_t devicePixelRatio = Application::getInstance()->getDevicePixelRatio();
cocos2d::TouchInfo touchInfo;
touchInfo.index = id;
touchInfo.x = x / devicePixelRatio;
touchInfo.y = y / devicePixelRatio;
touchEvent.touches.push_back(touchInfo);
cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent);
}
static void dispatchTouchEventWithPoints(JNIEnv *env, cocos2d::TouchEvent::Type type, jintArray ids, jfloatArray xs, jfloatArray ys)
{
if (g_isGameFinished)
{
return;
}
cocos2d::TouchEvent touchEvent;
touchEvent.type = type;
int size = env->GetArrayLength(ids);
jint id[size];
jfloat x[size];
jfloat y[size];
env->GetIntArrayRegion(ids, 0, size, id);
env->GetFloatArrayRegion(xs, 0, size, x);
env->GetFloatArrayRegion(ys, 0, size, y);
uint8_t devicePixelRatio = Application::getInstance()->getDevicePixelRatio();
for (int i = 0; i < size; i++)
{
cocos2d::TouchInfo touchInfo;
touchInfo.index = id[i];
touchInfo.x = x[i] / devicePixelRatio;
touchInfo.y = y[i] / devicePixelRatio;
touchEvent.touches.push_back(touchInfo);
}
cocos2d::EventDispatcher::dispatchTouchEvent(touchEvent);
}
JNIEXPORT void JNICALL JNI_RENDER(nativeTouchesBegin)(JNIEnv *env, jclass thiz, jint id, jfloat x, jfloat y)
{
if (g_isGameFinished)
{
return;
}
dispatchTouchEventWithOnePoint(env, cocos2d::TouchEvent::Type::BEGAN, id, x, y);
}
JNIEXPORT void JNICALL JNI_RENDER(nativeTouchesEnd)(JNIEnv *env, jclass thiz, jint id, jfloat x, jfloat y)
{
if (g_isGameFinished)
{
return;
}
dispatchTouchEventWithOnePoint(env, cocos2d::TouchEvent::Type::ENDED, id, x, y);
}
JNIEXPORT void JNICALL JNI_RENDER(nativeTouchesMove)(JNIEnv *env, jclass thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
if (g_isGameFinished)
{
return;
}
dispatchTouchEventWithPoints(env, cocos2d::TouchEvent::Type::MOVED, ids, xs, ys);
}
JNIEXPORT void JNICALL JNI_RENDER(nativeTouchesCancel)(JNIEnv *env, jclass thiz, jintArray ids, jfloatArray xs, jfloatArray ys)
{
if (g_isGameFinished)
{
return;
}
dispatchTouchEventWithPoints(env, cocos2d::TouchEvent::Type::CANCELLED, ids, xs, ys);
}
JNIEXPORT jboolean JNICALL JNI_RENDER(nativeKeyEvent)(JNIEnv *env, jclass thiz, jint keyCode, jboolean isPressed)
{
if (g_isGameFinished)
{
return JNI_TRUE;
}
int keyInWeb = -1;
// key values in web, refer to http://docs.cocos.com/creator/api/en/enums/KEY.html
switch (keyCode)
{
case KEYCODE_BACK:
keyInWeb = 6;
break;
case KEYCODE_ENTER:
keyInWeb = 13;
break;
case KEYCODE_MENU:
keyInWeb = 18;
break;
case KEYCODE_DPAD_UP:
keyInWeb = 1003;
break;
case KEYCODE_DPAD_DOWN:
keyInWeb = 1004;
break;
case KEYCODE_DPAD_LEFT:
keyInWeb = 1000;
break;
case KEYCODE_DPAD_RIGHT:
keyInWeb = 1001;
break;
case KEYCODE_DPAD_CENTER:
keyInWeb = 1005;
break;
default:
keyInWeb = 0; // If the key can't be identified, this value is 0
}
KeyboardEvent event;
event.key = keyInWeb;
event.action = isPressed ? KeyboardEvent::Action::PRESS : KeyboardEvent::Action::RELEASE;
EventDispatcher::dispatchKeyboardEvent(event);
return JNI_TRUE;
}
/***********************************************************
* Cocos2dxHelper native functions implementation.
@ -476,8 +169,8 @@ extern "C"
JNIEXPORT void JNICALL JNI_HELPER(nativeSetApkPath)(JNIEnv *env, jclass thiz, jstring apkPath)
{
LOGD("nativeSetApkPath: %s", apkPath);
g_apkPath = JniHelper::jstring2string(apkPath);
LOGD("nativeSetApkPath: %s", g_apkPath.c_str());
}
JNIEXPORT void JNICALL JNI_HELPER(nativeSetContext)(JNIEnv *env, jclass thiz, jobject context, jobject assetManager)
@ -522,8 +215,6 @@ extern "C"
se::AutoHandleScope hs;
std::string coder = "jc.wallet.currentAccount().address";
se::ScriptEngine::getInstance()->evalString(coder.c_str(), coder.size(), &rval);
// jsb_run_code("jc.wallet.currentAccount().address", &rval);
cocos2d::log("\n cpp init wallet: %s \n", &rval);
}
} // end of extern "C"

View File

@ -356,6 +356,7 @@ void Application::loadKeyLocal(const std::string &account, std::string *outItem)
void Application::toWallet(const std::string &url) {
UIApplication *app = [UIApplication sharedApplication];
NSString *uri = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding];
NSLog(@"open url: %@", uri);
[app openURL:[NSURL URLWithString:uri] options:@{} completionHandler:^(BOOL success) {
if (success) {
NSLog(@"Opened url");

View File

@ -1380,14 +1380,10 @@ static bool JSB_preRegistClient(se::State &s)
SE_PRECONDITION2(ok, false, "Error processing msg");
string key = "tmp_client_info";
string value;
ok = localStorageGetItem(key, &value);
string pk = getpk();
ok = load_encrypt_item(key, &value);
if (!ok) {
value = ramdonKey();
string str_encrypt = aes_encrypt(value.c_str(), pk.c_str());
localStorageSetItem(key, str_encrypt);
} else {
value = aes_decrypt(value.c_str(), pk.c_str());
store_encrypt_item(key, value);
}
string result = simple_sign(msg.c_str(), value.c_str());
s.rval().setString(result);
@ -1399,6 +1395,64 @@ static bool JSB_preRegistClient(se::State &s)
}
SE_BIND_FUNC(JSB_preRegistClient)
static bool JSB_prepareRelayRSAKey(se::State &s)
{
string key_str = rsa_key_pair();
size_t pos = key_str.find("|");
string pk;
string sk;
if (pos != std::string::npos) {
pk = key_str.substr(pos + 1, key_str.length());
sk = key_str.substr(0, pos);
store_encrypt_item("relay_rsa_sk", sk);
s.rval().setString(pk);
return true;
}
return false;
}
SE_BIND_FUNC(JSB_prepareRelayRSAKey)
static bool JSB_parseRelayAESKey(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
if (argc > 0)
{
string key_encrypted;
ok = seval_to_std_string(args[0], &key_encrypted);
SE_PRECONDITION2(ok, false, "Error processing key_encrypted");
string key = "tmp_client_info";
string sk_rsa;
ok = load_encrypt_item("relay_rsa_sk", &sk_rsa);
SE_PRECONDITION2(ok, false, "No local RSA private key found");
string key_aes = rsa_decrypt(key_encrypted.c_str(), sk_rsa.c_str());
CCLOG("aes key: %s", key_aes.c_str());
store_encrypt_item("relay_aes_key", key_aes);
return true;
}
SE_REPORT_ERROR("wrong number of arguments: %d, was expecting %d", (int)argc, 1);
return false;
}
SE_BIND_FUNC(JSB_parseRelayAESKey)
static bool JSB_loadRelayAESKey(se::State &s)
{
const auto &args = s.args();
size_t argc = args.size();
CC_UNUSED bool ok = true;
string key;
ok = load_encrypt_item("relay_aes_key", &key);
SE_PRECONDITION2(ok, false, "No local AES key found");
s.rval().setString(key);
return true;
}
SE_BIND_FUNC(JSB_loadRelayAESKey)
static bool JSB_saveLocalStorage(se::State &s)
{
const auto &args = s.args();
@ -1579,6 +1633,24 @@ static bool JSB_walletDecrypt(se::State &s)
}
SE_BIND_FUNC(JSB_walletDecrypt)
void store_encrypt_item(string key, string value) {
string pk = getpk();
string str_encrypt = aes_encrypt(value.c_str(), pk.c_str());
localStorageSetItem(key, str_encrypt);
}
bool load_encrypt_item(string key, string *value) {
string pk = getpk();
CC_UNUSED bool ok = true;
ok = localStorageGetItem(key, value);
if (ok) {
string tmp = aes_decrypt(value->c_str(), pk.c_str());
value->assign(tmp);
}
return ok;
}
std::string encrypt_aes(std::string &content, std::string &key)
{
return aes_encrypt(content.c_str(), key.c_str());
@ -1659,6 +1731,12 @@ bool jsb_register_global_variables(se::Object *global)
__jsbObj->defineFunction("loadLocalStorage", _SE(JSB_loadLocalStorage));
__jsbObj->defineFunction("saveLocalStorage", _SE(JSB_saveLocalStorage));
__jsbObj->defineFunction("prepareRelayRSAKey", _SE(JSB_prepareRelayRSAKey));
__jsbObj->defineFunction("parseRelayAESKey", _SE(JSB_parseRelayAESKey));
__jsbObj->defineFunction("loadRelayAESKey", _SE(JSB_loadRelayAESKey));
//
__jsbObj->defineFunction("setPreferredFramesPerSecond", _SE(JSB_setPreferredFramesPerSecond));
global->defineFunction("__getPlatform", _SE(JSBCore_platform));

View File

@ -54,3 +54,5 @@ std::string encrypt_aes(std::string &content, std::string &key);
std::string decrypt_aes(std::string &content, std::string &key);
std::string generate_clientid(std::string &content);
std::string getpk();
void store_encrypt_item(std::string key, std::string value);
bool load_encrypt_item(std::string key, std::string *value);

View File

@ -51,3 +51,9 @@ char *generate_client_key(const char *password, const char *openid, const char *
char *simple_sign(const char *content, const char *key);
char *ramdonKey(void);
char *rsa_key_pair(void);
char *rsa_encrypt(const char *content, const char *p_key);
char *rsa_decrypt(const char *content, const char *s_key);

Binary file not shown.

View File

@ -51,3 +51,9 @@ char *generate_client_key(const char *password, const char *openid, const char *
char *simple_sign(const char *content, const char *key);
char *ramdonKey(void);
char *rsa_key_pair(void);
char *rsa_encrypt(const char *content, const char *p_key);
char *rsa_decrypt(const char *content, const char *s_key);

Binary file not shown.

View File

@ -51,3 +51,9 @@ char *generate_client_key(const char *password, const char *openid, const char *
char *simple_sign(const char *content, const char *key);
char *ramdonKey(void);
char *rsa_key_pair(void);
char *rsa_encrypt(const char *content, const char *p_key);
char *rsa_decrypt(const char *content, const char *s_key);

Binary file not shown.

View File

@ -51,3 +51,9 @@ char *generate_client_key(const char *password, const char *openid, const char *
char *simple_sign(const char *content, const char *key);
char *ramdonKey(void);
char *rsa_key_pair(void);
char *rsa_encrypt(const char *content, const char *p_key);
char *rsa_decrypt(const char *content, const char *s_key);

Binary file not shown.

View File

@ -51,3 +51,9 @@ char *generate_client_key(const char *password, const char *openid, const char *
char *simple_sign(const char *content, const char *key);
char *ramdonKey(void);
char *rsa_key_pair(void);
char *rsa_encrypt(const char *content, const char *p_key);
char *rsa_decrypt(const char *content, const char *s_key);

Binary file not shown.