Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions extensions/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@ Json/lib_json/json_writer.cpp \
sqlite3/include/sqlite3.c \
device/Device_android/CADevice.cpp \
studio/CAStudioViewParser.cpp \
crypto/CACrypto.cpp \
crypto/base64/libbase64.c \
crypto/xxtea/xxtea.c

LOCAL_WHOLE_STATIC_LIBRARIES := CrossApp_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_static
LOCAL_WHOLE_STATIC_LIBRARIES += cocos_curl_static
LOCAL_WHOLE_STATIC_LIBRARIES += libwebsockets_static

LOCAL_CFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_EXPORT_CFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_CPPFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1
LOCAL_EXPORT_CPPFLAGS += -DCC_ENABLE_CHIPMUNK_INTEGRATION=1

LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH) \
$(LOCAL_PATH)/network \
$(LOCAL_PATH)/LocalStorage \
$(LOCAL_PATH)/Json \
$(LOCAL_PATH)/Json/lib_json \
$(LOCAL_PATH)/crypto \
$(LOCAL_PATH)/crypto/xxtea \
$(LOCAL_PATH)/crypto/md5 \
$(LOCAL_PATH)/crypto/base64 \
$(LOCAL_PATH)/GUI



LOCAL_CFLAGS := -fexceptions

include $(BUILD_STATIC_LIBRARY)
Expand Down
2 changes: 1 addition & 1 deletion extensions/CrossAppExt.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
#include "sqlite3/include/sqlite3.h"
#include "LocalStorage/LocalStorage.h"


#include "crypto/CACrypto.h"
//other
#include "device/CADevice.h"
#endif /* __CrossApp_EXT_H__ */
173 changes: 173 additions & 0 deletions extensions/crypto/CACrypto.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
//
// CACrypto.cpp
// AppGift
//
// Created by tanjie on 15/6/3.
//
//

#include "CACrypto.h"

extern "C" {
#include "crypto/base64/libbase64.h"
#include "crypto/xxtea/xxtea.h"
#include "crypto/md5/md5.h"
}

NS_CC_EXT_BEGIN

unsigned char* CACrypto::encryptXXTEA(unsigned char* plaintext,
int plaintextLength,
unsigned char* key,
int keyLength,
int* resultLength)
{
xxtea_long len;
unsigned char* result = xxtea_encrypt(plaintext, (xxtea_long)plaintextLength, key, (xxtea_long)keyLength, &len);
*resultLength = (int)len;
return result;
}

unsigned char* CACrypto::decryptXXTEA(unsigned char* ciphertext,
int ciphertextLength,
unsigned char* key,
int keyLength,
int* resultLength)
{
xxtea_long len;
unsigned char* result = xxtea_decrypt(ciphertext, (xxtea_long)ciphertextLength, key, (xxtea_long)keyLength, &len);
*resultLength = (int)len;
return result;
}

int CACrypto::encodeBase64Len(const char* input, int inputLength)
{
return Base64encode_len(inputLength);
}

int CACrypto::encodeBase64(const char* input,
int inputLength,
char* output,
int outputBufferLength)
{
CCAssert(Base64encode_len(inputLength) <= outputBufferLength, "CACrypto::encodeBase64() - outputBufferLength too small");
return Base64encode(output, input, inputLength);
}

int CACrypto::decodeBase64Len(const char* input)
{
return Base64decode_len(input);
}

int CACrypto::decodeBase64(const char* input,
char* output,
int outputBufferLength)
{
CCAssert(Base64decode_len(input) <= outputBufferLength, "CACrypto::decodeBase64() - outputBufferLength too small");
return Base64decode(output, input);
}

void CACrypto::MD5(void* input, int inputLength, unsigned char* output)
{
MD5_CTX ctx;
MD5_Init(&ctx);
MD5_Update(&ctx, input, inputLength);
MD5_Final(output, &ctx);
}

void CACrypto::MD5File(const char* path, unsigned char* output)
{
FILE *file = fopen(path, "rb");
if (file == NULL)
return;

MD5_CTX ctx;
MD5_Init(&ctx);

size_t i;
const size_t BUFFER_SIZE = 1024;
char buffer[BUFFER_SIZE];
while ((i = fread(buffer, 1, BUFFER_SIZE, file)) > 0) {
MD5_Update(&ctx, buffer, (unsigned) i);
}

fclose(file);
MD5_Final(output, &ctx);
}

const string CACrypto::MD5String(void* input, int inputLength)
{
unsigned char buffer[MD5_BUFFER_LENGTH];
MD5(static_cast<void*>(input), inputLength, buffer);

char* hex = bin2hex(buffer, MD5_BUFFER_LENGTH);
string ret(hex);
delete[] hex;
return ret;
}

char *CACrypto::bin2hex(unsigned char* bin, int binLength)
{
static const char* hextable = "0123456789abcdef";

int hexLength = binLength * 2 + 1;
char* hex = new char[hexLength];
memset(hex, 0, sizeof(char) * hexLength);

int ci = 0;
for (int i = 0; i < binLength; ++i)
{
unsigned char c = bin[i];
hex[ci++] = hextable[(c >> 4) & 0x0f];
hex[ci++] = hextable[c & 0x0f];
}

return hex;
}

char *CACrypto::hex2bin(char* hex, int hexLength)
{
if (hexLength % 2) {
CCLog("hexLength should be even number.");
return NULL;
}

int binLength = hexLength / 2 + 1;
char *bin = new char[binLength];
memset(bin, 0, sizeof(char) * binLength);

int ci = 0;
for (int i = 0; i < hexLength; i += 2) {
char high = hex[i];
char low = hex[i+1];

if (high >= '0' && high <= '9') {
high = high - '0';
} else if (high >= 'A' && high <= 'F') {
high = high - 'A' + 10;
} else if (high >= 'a' && high <= 'f') {
high = high - 'a' + 10;
} else {
assert(0);
high = 0;
}

if (low >= '0' && low <= '9') {
low = low - '0';
} else if (low >= 'A' && low <= 'F') {
low = low - 'A' + 10;
} else if (low >= 'a' && low <= 'f') {
low = low - 'a' + 10;
} else {
assert(0);
low = 0;
}

bin[ci++] = (high << 4) | (low);
}

return bin;
}


NS_CC_EXT_END
74 changes: 74 additions & 0 deletions extensions/crypto/CACrypto.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//
// CACrypto.h
// AppGift
//
// Created by tanjie on 15/6/3.
//
//

#ifndef __AppGift__CACrypto__
#define __AppGift__CACrypto__

#include <stdio.h>

#include <iostream>
#include "CrossApp.h"
#include "CrossAppExt.h"

NS_CC_EXT_BEGIN

class CACrypto
{
public:
static const int MD5_BUFFER_LENGTH = 16;

/** @brief Encrypt data with XXTEA algorithm, return ciphertext, free ciphertext after used */
static unsigned char* encryptXXTEA(unsigned char* plaintext,
int plaintextLength,
unsigned char* key,
int keyLength,
int* resultLength);

/** @brief Decrypt data with XXTEA algorithm, return plaintext, free plaintext after used */
static unsigned char* decryptXXTEA(unsigned char* ciphertext,
int ciphertextLength,
unsigned char* key,
int keyLength,
int* resultLength);

/** @brief Get length of encoding data with Base64 algorithm */
static int encodeBase64Len(const char* input, int inputLength);

/** @brief Encoding data with Base64 algorithm, return encoded string length */
static int encodeBase64(const char* input, int inputLength,
char* output, int outputBufferLength);

/** @brief Get length of Decoding Base 64 */
static int decodeBase64Len(const char* input);

/** @brief Decoding Base64 string to data, return decoded data length */
static int decodeBase64(const char* input,
char* output, int outputBufferLength);

/** @brief Calculate MD5, get MD5 code (not string) */
static void MD5(void* input, int inputLength,
unsigned char* output);

static void MD5File(const char* path, unsigned char* output);



static const string MD5String(void* input, int inputLength);

static char *bin2hex(unsigned char* bin, int binLength);
static char *hex2bin(char* hex, int hexLength);
#pragma mark -
#pragma mark private methods

private:
CACrypto(void) {}
};

NS_CC_EXT_END

#endif /* defined(__AppGift__CACrypto__) */
Loading