LUA_STRING CCCrypto::encodingBase64Lua(bool isDecoding,
                                       const char* input,
                                       int inputLength)
{
    CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine();
    engine->cleanLuaStack();
    lua_State* L = engine->getLuaState();
    
    int outputLength = inputLength * 2;
    char* output = static_cast<char*>(malloc(outputLength));
    int dataUsed = -1;
    
    if (isDecoding)
    {
        dataUsed = decodeBase64(input, output, outputLength);
    }
    else
    {
        dataUsed = encodeBase64(input, inputLength, output, outputLength);
    }
    if (dataUsed > 0 && dataUsed < outputLength)
    {
        lua_pushlstring(L, output, dataUsed);
    }
    else
    {
        lua_pushnil(L);
    }
    free(output);
    return 1;
}
LUA_STRING CCCrypto::MD5Lua(char* input, int inputLength, bool isRawOutput)
{
    static const char* hextable = "0123456789abcdef";
    
    unsigned char buffer[16];
    MD5(static_cast<void*>(input), inputLength, buffer);
    
    CCScriptEngineProtocol* engine = CCScriptEngineManager::sharedManager()->getScriptEngine();
    engine->cleanLuaStack();
    lua_State* L = engine->getLuaState();
    
    if (isRawOutput)
    {
        lua_pushlstring(L, (char*)buffer, 16);
    }
    else
    {
        char md5str[33];
        md5str[32] = 0;
        int ci = 0;
        for (int i = 0; i < 16; ++i)
        {
            unsigned char c = buffer[i];
            md5str[ci++] = hextable[(c >> 4) & 0x0f];
            md5str[ci++] = hextable[c & 0x0f];
        }
        lua_pushstring(L, md5str);
    }
    
    return 1;
}