├── .gitattributes ├── .github ├── FUNDING.yml └── workflows │ ├── bob.yml │ └── trigger-site-rebuild.yml ├── .gitignore ├── LICENSE.md ├── README.md ├── crypt ├── api │ └── crypt.script_api ├── ext.manifest └── src │ └── crypt.cpp ├── docs └── index.md ├── example ├── example.collection └── example.script ├── game.project └── input └── game.input_binding /.gitattributes: -------------------------------------------------------------------------------- 1 | # Defold Protocol Buffer Text Files (https://github.com/github/linguist/issues/5091) 2 | *.animationset linguist-language=JSON5 3 | *.atlas linguist-language=JSON5 4 | *.camera linguist-language=JSON5 5 | *.collection linguist-language=JSON5 6 | *.collectionfactory linguist-language=JSON5 7 | *.collectionproxy linguist-language=JSON5 8 | *.collisionobject linguist-language=JSON5 9 | *.cubemap linguist-language=JSON5 10 | *.display_profiles linguist-language=JSON5 11 | *.factory linguist-language=JSON5 12 | *.font linguist-language=JSON5 13 | *.gamepads linguist-language=JSON5 14 | *.go linguist-language=JSON5 15 | *.gui linguist-language=JSON5 16 | *.input_binding linguist-language=JSON5 17 | *.label linguist-language=JSON5 18 | *.material linguist-language=JSON5 19 | *.mesh linguist-language=JSON5 20 | *.model linguist-language=JSON5 21 | *.particlefx linguist-language=JSON5 22 | *.render linguist-language=JSON5 23 | *.sound linguist-language=JSON5 24 | *.sprite linguist-language=JSON5 25 | *.spinemodel linguist-language=JSON5 26 | *.spinescene linguist-language=JSON5 27 | *.texture_profiles linguist-language=JSON5 28 | *.tilemap linguist-language=JSON5 29 | *.tilesource linguist-language=JSON5 30 | 31 | # Defold JSON Files 32 | *.buffer linguist-language=JSON 33 | 34 | # Defold GLSL Shaders 35 | *.fp linguist-language=GLSL 36 | *.vp linguist-language=GLSL 37 | 38 | # Defold Lua Files 39 | *.editor_script linguist-language=Lua 40 | *.render_script linguist-language=Lua 41 | *.script linguist-language=Lua 42 | *.gui_script linguist-language=Lua 43 | -------------------------------------------------------------------------------- /.github/FUNDING.yml: -------------------------------------------------------------------------------- 1 | github: defold 2 | patreon: Defold 3 | custom: ['https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NBNBHTUW4GS4C'] 4 | -------------------------------------------------------------------------------- /.github/workflows/bob.yml: -------------------------------------------------------------------------------- 1 | name: Build with bob 2 | 3 | on: 4 | push: 5 | pull_request_target: 6 | schedule: 7 | # nightly at 05:00 on the 1st and 15th 8 | - cron: 0 5 1,15 * * 9 | 10 | env: 11 | VERSION_FILENAME: 'info.json' 12 | BUILD_SERVER: 'https://build.defold.com' 13 | 14 | jobs: 15 | build_with_bob: 16 | strategy: 17 | matrix: 18 | platform: [armv7-android, x86_64-linux, x86_64-win32, x86-win32, js-web] 19 | runs-on: ubuntu-latest 20 | 21 | name: Build 22 | steps: 23 | - uses: actions/checkout@v2 24 | - uses: actions/setup-java@v1 25 | with: 26 | java-version: '11.0.2' 27 | 28 | - name: Get Defold version 29 | run: | 30 | TMPVAR=`curl -s http://d.defold.com/stable/${{env.VERSION_FILENAME}} | jq -r '.sha1'` 31 | echo "DEFOLD_VERSION=${TMPVAR}" >> $GITHUB_ENV 32 | echo "Found version ${TMPVAR}" 33 | 34 | - name: Download bob.jar 35 | run: | 36 | wget -q http://d.defold.com/archive/stable/${{env.DEFOLD_VERSION}}/bob/bob.jar 37 | java -jar bob.jar --version 38 | 39 | - name: Resolve libraries 40 | run: java -jar bob.jar resolve --email a@b.com --auth 123456 41 | - name: Build 42 | run: java -jar bob.jar --platform=${{ matrix.platform }} build --archive --build-server=${{env.BUILD_SERVER}} 43 | - name: Bundle 44 | run: java -jar bob.jar --platform=${{ matrix.platform }} bundle 45 | 46 | # macOS is not technically needed for building, but we want to test bundling as well, since we're also testing the manifest merging 47 | build_with_bob_macos: 48 | strategy: 49 | matrix: 50 | platform: [armv7-darwin, x86_64-darwin] 51 | runs-on: macOS-latest 52 | 53 | name: Build 54 | steps: 55 | - uses: actions/checkout@v2 56 | - uses: actions/setup-java@v1 57 | with: 58 | java-version: '11.0.2' 59 | 60 | - name: Get Defold version 61 | run: | 62 | TMPVAR=`curl -s http://d.defold.com/stable/${{env.VERSION_FILENAME}} | jq -r '.sha1'` 63 | echo "DEFOLD_VERSION=${TMPVAR}" >> $GITHUB_ENV 64 | echo "Found version ${TMPVAR}" 65 | 66 | - name: Download bob.jar 67 | run: | 68 | wget -q http://d.defold.com/archive/stable/${{env.DEFOLD_VERSION}}/bob/bob.jar 69 | java -jar bob.jar --version 70 | 71 | - name: Resolve libraries 72 | run: java -jar bob.jar resolve --email a@b.com --auth 123456 73 | - name: Build 74 | run: java -jar bob.jar --platform=${{ matrix.platform }} build --archive --build-server=${{env.BUILD_SERVER}} 75 | - name: Bundle 76 | run: java -jar bob.jar --platform=${{ matrix.platform }} bundle 77 | -------------------------------------------------------------------------------- /.github/workflows/trigger-site-rebuild.yml: -------------------------------------------------------------------------------- 1 | name: Trigger site rebuild 2 | 3 | on: 4 | push: 5 | branches: 6 | - 'master' 7 | 8 | jobs: 9 | site-rebuild: 10 | runs-on: ubuntu-latest 11 | 12 | steps: [ 13 | { 14 | name: 'Repository dispatch', 15 | uses: defold/repository-dispatch@1.2.1, 16 | with: { 17 | repo: 'defold/defold.github.io', 18 | token: '${{ secrets.SERVICES_GITHUB_TOKEN }}', 19 | user: 'services@defold.se', 20 | action: 'extension-websocket' 21 | } 22 | }] 23 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /.internal 2 | /build 3 | .externalToolBuilders 4 | .DS_Store 5 | Thumbs.db 6 | .lock-wscript 7 | *.pyc 8 | .project 9 | .cproject 10 | builtins -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Defold 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![Actions Status Alpha](https://github.com/defold/extension-crypt/actions/workflows/bob.yml/badge.svg)](https://github.com/defold/extension-crypt/actions) 2 | 3 | # Cryptographic functions for Defold 4 | 5 | Defold [native extension](https://www.defold.com/manuals/extensions/) for interacting with various hash and encode/decode algorithms. 6 | 7 | [Manual, API and setup instructions](https://www.defold.com/extension-crypt/) is available on the official Defold site. 8 | -------------------------------------------------------------------------------- /crypt/api/crypt.script_api: -------------------------------------------------------------------------------- 1 | - name: crypt 2 | type: table 3 | desc: Functions and constants for interacting with various hash and encode/decode algorithms 4 | 5 | members: 6 | 7 | #***************************************************************************************************** 8 | #***** HASH ****************************************************************************************** 9 | #***************************************************************************************************** 10 | 11 | - name: hash_sha1 12 | type: function 13 | 14 | parameters: 15 | - name: buffer 16 | type: string 17 | 18 | returns: 19 | - name: output 20 | type: string 21 | 22 | #***************************************************************************************************** 23 | 24 | - name: hash_sha256 25 | type: function 26 | 27 | parameters: 28 | - name: buffer 29 | type: string 30 | 31 | returns: 32 | - name: output 33 | type: string 34 | 35 | #***************************************************************************************************** 36 | - name: hash_sha512 37 | type: function 38 | 39 | parameters: 40 | - name: buffer 41 | type: string 42 | 43 | returns: 44 | - name: output 45 | type: string 46 | 47 | #***************************************************************************************************** 48 | - name: hash_md5 49 | type: function 50 | 51 | parameters: 52 | - name: buffer 53 | type: string 54 | 55 | returns: 56 | - name: output 57 | type: string 58 | 59 | #***************************************************************************************************** 60 | #***** ENCODE/DECODE ********************************************************************************* 61 | #***************************************************************************************************** 62 | 63 | - name: encode_base64 64 | type: function 65 | 66 | parameters: 67 | - name: input 68 | type: string 69 | 70 | returns: 71 | - name: output 72 | type: string 73 | 74 | 75 | #***************************************************************************************************** 76 | 77 | - name: decode_base64 78 | type: function 79 | 80 | parameters: 81 | - name: input 82 | type: string 83 | 84 | returns: 85 | - name: output 86 | type: string 87 | 88 | 89 | #***************************************************************************************************** 90 | 91 | - name: encrypt_xtea 92 | type: function 93 | 94 | parameters: 95 | - name: source 96 | type: string 97 | - name: key 98 | type: string 99 | desc: key should be <=16 100 | 101 | returns: 102 | - name: output 103 | type: string 104 | 105 | #***************************************************************************************************** 106 | 107 | - name: decrypt_xtea 108 | type: function 109 | 110 | parameters: 111 | - name: source 112 | type: string 113 | - name: key 114 | type: string 115 | desc: key should be <=16 116 | 117 | returns: 118 | - name: output 119 | type: string 120 | -------------------------------------------------------------------------------- /crypt/ext.manifest: -------------------------------------------------------------------------------- 1 | # C++ symbol in your extension 2 | name: "CryptExtension" 3 | -------------------------------------------------------------------------------- /crypt/src/crypt.cpp: -------------------------------------------------------------------------------- 1 | // Extension lib defines 2 | #define LIB_NAME "CryptExtension" 3 | #define MODULE_NAME "crypt" 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | 10 | static int Crypt_HashSha1(lua_State* L) 11 | { 12 | DM_LUA_STACK_CHECK(L, 1); 13 | 14 | size_t buflen; 15 | const char* buf = luaL_checklstring(L, 1, &buflen); 16 | uint8_t digest[20] = {0}; 17 | 18 | dmCrypt::HashSha1((const uint8_t*)buf, buflen, digest); 19 | lua_pushlstring(L, (char*)&digest, 20); 20 | return 1; 21 | } 22 | 23 | static int Crypt_HashSha256(lua_State* L) 24 | { 25 | DM_LUA_STACK_CHECK(L, 1); 26 | 27 | size_t buflen; 28 | const char* buf = luaL_checklstring(L, 1, &buflen); 29 | uint8_t digest[32] = {0}; 30 | 31 | dmCrypt::HashSha256((const uint8_t*)buf, buflen, digest); 32 | lua_pushlstring(L, (char*)&digest, 32); 33 | return 1; 34 | } 35 | 36 | static int Crypt_HashSha512(lua_State* L) 37 | { 38 | DM_LUA_STACK_CHECK(L, 1); 39 | 40 | size_t buflen; 41 | const char* buf = luaL_checklstring(L, 1, &buflen); 42 | uint8_t digest[64] = {0}; 43 | 44 | dmCrypt::HashSha512((const uint8_t*)buf, buflen, digest); 45 | lua_pushlstring(L, (char*)&digest, 64); 46 | return 1; 47 | } 48 | 49 | static int Crypt_HashMd5(lua_State* L) 50 | { 51 | DM_LUA_STACK_CHECK(L, 1); 52 | 53 | size_t buflen; 54 | const char* buf = luaL_checklstring(L, 1, &buflen); 55 | uint8_t digest[16] = {0}; 56 | 57 | dmCrypt::HashMd5((const uint8_t*)buf, buflen, digest); 58 | lua_pushlstring(L, (char*)&digest, 16); 59 | return 1; 60 | } 61 | 62 | static int Crypt_Base64Encode(lua_State* L) 63 | { 64 | DM_LUA_STACK_CHECK(L, 1); 65 | 66 | size_t srclen; 67 | const char* src = luaL_checklstring(L, 1, &srclen); 68 | 69 | // 4 characters to represent every 3 bytes with padding applied 70 | // for binary data which isn't an exact multiple of 3 bytes. 71 | // https://stackoverflow.com/a/7609180/1266551 72 | uint32_t dstlen = srclen * 4 / 3 + 4; 73 | uint8_t* dst = (uint8_t*)malloc(dstlen); 74 | 75 | if (dmCrypt::Base64Encode((const uint8_t*)src, srclen, dst, &dstlen)) 76 | { 77 | lua_pushlstring(L, (char*)dst, dstlen); 78 | } 79 | else 80 | { 81 | lua_pushnil(L); 82 | } 83 | free(dst); 84 | return 1; 85 | } 86 | 87 | static int Crypt_Base64Decode(lua_State* L) 88 | { 89 | DM_LUA_STACK_CHECK(L, 1); 90 | 91 | size_t srclen; 92 | const char* src = luaL_checklstring(L, 1, &srclen); 93 | 94 | uint32_t dstlen = srclen * 3 / 4; 95 | uint8_t* dst = (uint8_t*)malloc(dstlen); 96 | 97 | if (dmCrypt::Base64Decode((const uint8_t*)src, srclen, dst, &dstlen)) 98 | { 99 | lua_pushlstring(L, (char*)dst, dstlen); 100 | } 101 | else 102 | { 103 | lua_pushnil(L); 104 | } 105 | free(dst); 106 | return 1; 107 | } 108 | 109 | static int Crypt_EncryptXTEA(lua_State* L) 110 | { 111 | DM_LUA_STACK_CHECK(L, 1); 112 | 113 | size_t srclen; 114 | const char* src = luaL_checklstring(L, 1, &srclen); 115 | 116 | size_t keylen; 117 | const char* key = luaL_checklstring(L, 2, &keylen); 118 | if (keylen > 16) 119 | { 120 | return DM_LUA_ERROR("key should be <=16"); 121 | } 122 | 123 | uint32_t dstlen = srclen; 124 | char* dst = (char*) malloc(dstlen); 125 | memcpy(dst, src, dstlen); 126 | dmCrypt::Result result = dmCrypt::Encrypt(dmCrypt::ALGORITHM_XTEA, (uint8_t*)dst, dstlen, (uint8_t*)key, keylen); 127 | if (result != dmCrypt::RESULT_OK) 128 | { 129 | free(dst); 130 | return DM_LUA_ERROR( "error when encrypt"); 131 | } 132 | else 133 | { 134 | lua_pushlstring(L, dst, dstlen); 135 | free(dst); 136 | } 137 | 138 | return 1; 139 | } 140 | 141 | static int Crypt_DecryptXTEA(lua_State* L) 142 | { 143 | DM_LUA_STACK_CHECK(L, 1); 144 | 145 | size_t srclen; 146 | const char* src = luaL_checklstring(L, 1, &srclen); 147 | 148 | size_t keylen; 149 | const char* key = luaL_checklstring(L, 2, &keylen); 150 | 151 | if (keylen > 16) 152 | { 153 | return DM_LUA_ERROR("key should be <=16"); 154 | } 155 | 156 | uint32_t dstlen = srclen; 157 | char* dst = (char*) malloc(dstlen); 158 | memcpy(dst, src, dstlen); 159 | 160 | dmCrypt::Result result = dmCrypt::Decrypt(dmCrypt::ALGORITHM_XTEA, (uint8_t*)dst, dstlen, (uint8_t*)key, keylen); 161 | if(result != dmCrypt::RESULT_OK) 162 | { 163 | free(dst); 164 | return DM_LUA_ERROR("error when decrypt"); 165 | } 166 | else 167 | { 168 | lua_pushlstring(L, (char*)dst, dstlen); 169 | free(dst); 170 | } 171 | 172 | return 1; 173 | } 174 | 175 | static const luaL_reg Module_methods[] = 176 | { 177 | {"hash_sha1", Crypt_HashSha1}, 178 | {"hash_sha256", Crypt_HashSha256}, 179 | {"hash_sha512", Crypt_HashSha512}, 180 | {"hash_md5", Crypt_HashMd5}, 181 | {"encode_base64", Crypt_Base64Encode}, 182 | {"decode_base64", Crypt_Base64Decode}, 183 | {"encrypt_xtea", Crypt_EncryptXTEA}, 184 | {"decrypt_xtea", Crypt_DecryptXTEA}, 185 | {0, 0} 186 | }; 187 | 188 | static void LuaInit(lua_State* L) 189 | { 190 | int top = lua_gettop(L); 191 | 192 | // Register lua names 193 | luaL_register(L, MODULE_NAME, Module_methods); 194 | 195 | lua_pop(L, 1); 196 | assert(top == lua_gettop(L)); 197 | } 198 | 199 | dmExtension::Result InitializeCryptExtension(dmExtension::Params* params) 200 | { 201 | // Init Lua 202 | LuaInit(params->m_L); 203 | dmLogInfo("Registered %s Extension\n", MODULE_NAME); 204 | return dmExtension::RESULT_OK; 205 | } 206 | 207 | DM_DECLARE_EXTENSION(CryptExtension, LIB_NAME, 0, 0, InitializeCryptExtension, 0, 0, 0) 208 | -------------------------------------------------------------------------------- /docs/index.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: Defold Crypt documentation 3 | brief: This manual covers how to use various hashing and encoding algorithms in Defold. 4 | --- 5 | 6 | # Defold Crypt documentation 7 | This extension provides functions for interacting with various hash and encode/decode algorithms. The extension wraps the [dmCrypt API](https://defold.com/ref/stable/dmCrypt/) from the Defold SDK to Lua. 8 | 9 | 10 | ## Installation 11 | To use this library in your Defold project, add the following URL to your `game.project` dependencies: 12 | 13 | https://github.com/defold/extension-crypt/archive/master.zip 14 | 15 | We recommend using a link to a zip file of a [specific release](https://github.com/defold/extension-crypt/releases). 16 | 17 | 18 | ## Usage 19 | 20 | The API provides the following functions: 21 | * `crypt.hash_sha1(source)` 22 | * `crypt.hash_sha256(source)` 23 | * `crypt.hash_sha512(source)` 24 | * `crypt.hash_md5(source)` 25 | * `crypt.encode_base64(source)` 26 | * `crypt.decode_base64(source)` 27 | * `crypt.encrypt_xtea(source, key)` 28 | * `crypt.decrypt_xtea(source, key)` 29 | 30 | 31 | ## Source code 32 | 33 | The source code is available on [GitHub](https://github.com/defold/extension-crypt) 34 | 35 | 36 | ## API reference 37 | -------------------------------------------------------------------------------- /example/example.collection: -------------------------------------------------------------------------------- 1 | name: "main" 2 | scale_along_z: 0 3 | embedded_instances { 4 | id: "go" 5 | data: "components {\n" 6 | " id: \"example\"\n" 7 | " component: \"/example/example.script\"\n" 8 | " position {\n" 9 | " x: 0.0\n" 10 | " y: 0.0\n" 11 | " z: 0.0\n" 12 | " }\n" 13 | " rotation {\n" 14 | " x: 0.0\n" 15 | " y: 0.0\n" 16 | " z: 0.0\n" 17 | " w: 1.0\n" 18 | " }\n" 19 | "}\n" 20 | "" 21 | position { 22 | x: 0.0 23 | y: 0.0 24 | z: 0.0 25 | } 26 | rotation { 27 | x: 0.0 28 | y: 0.0 29 | z: 0.0 30 | w: 1.0 31 | } 32 | scale3 { 33 | x: 1.0 34 | y: 1.0 35 | z: 1.0 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /example/example.script: -------------------------------------------------------------------------------- 1 | local function hash1() 2 | local expected = {0x42,0x42,0x72,0x63,0xa1,0x9a,0x68,0x8c,0x5f,0x90,0x11,0x61,0x6f,0xb1,0x29,0x25,0x11,0x81,0x76,0x96} 3 | local buffer = "sha1 string to encrypt" 4 | 5 | local digest = crypt.hash_sha1(buffer) 6 | print("sha1 source:", buffer) 7 | 8 | local digest_str = "" 9 | for i=1,#digest do 10 | local byte = digest:byte(i, i) 11 | assert(byte == expected[i]) 12 | digest_str = digest_str .. tostring(byte) .. ((i < #digest) and "," or "") 13 | end 14 | print("sha1 digest:", digest_str) 15 | end 16 | 17 | local function hash256() 18 | local expected = {0x4E,0x95,0x18,0x57,0x54,0x22,0xC9,0x08,0x73,0x96,0x88,0x7C,0xE2,0x04,0x77,0xAB,0x5F,0x55,0x0A,0x4A,0xA3,0xD1,0x61,0xC5,0xC2,0x2A,0x99,0x6B,0x0A,0xBB,0x8B,0x35} 19 | local buffer = "This is a string" 20 | 21 | local digest = crypt.hash_sha256(buffer) 22 | print("sha256 source:", buffer) 23 | 24 | local digest_str = "" 25 | for i=1,#digest do 26 | local byte = digest:byte(i, i) 27 | assert(byte == expected[i]) 28 | digest_str = digest_str .. tostring(byte) .. ((i < #digest) and "," or "") 29 | end 30 | print("sha256 digest:", digest_str) 31 | end 32 | 33 | local function hash512() 34 | local expected = {0xd7,0x9e,0x74,0x75,0xf0,0x47,0xad,0x6a,0xbf,0xb9,0x8a,0xf2,0x0a,0x50,0x1f,0x1f,0x5c,0x03,0xa2,0x4a,0xc4,0xe4,0x22,0x32,0x1b,0xcc,0x7c,0xe6,0x6b,0x14,0xb6,0xfa,0x36,0x56,0x73,0x41,0x34,0x03,0xce,0x8e,0xb4,0x63,0xad,0x61,0xbe,0xbf,0xa2,0x2f,0x1a,0x2f,0x56,0xf6,0xf4,0x94,0x7c,0x25,0xd2,0xb1,0x84,0xd1,0xda,0x23,0x1c,0x27} 35 | local buffer = "sha512 string to encrypt" 36 | 37 | local digest = crypt.hash_sha512(buffer) 38 | print("sha512 source:", buffer) 39 | 40 | local digest_str = "" 41 | for i=1,#digest do 42 | local byte = digest:byte(i, i) 43 | assert(byte == expected[i]) 44 | digest_str = digest_str .. tostring(byte) .. ((i < #digest) and "," or "") 45 | end 46 | print("sha512 digest:", digest_str) 47 | end 48 | 49 | local function hashmd5() 50 | local expected = {0x32,0x2f,0xcc,0x89,0xb5,0xec,0xc5,0xe9,0x0a,0x9e,0x73,0xb0,0xe0,0x92,0xbd,0xfc} 51 | local buffer = "Md5 string to encrypt" 52 | 53 | local digest = crypt.hash_md5(buffer) 54 | print("Md5 source:", buffer) 55 | 56 | local digest_str = "" 57 | for i=1,#digest do 58 | local byte = digest:byte(i, i) 59 | assert(byte == expected[i]) 60 | digest_str = digest_str .. tostring(byte) .. ((i < #digest) and "," or "") 61 | end 62 | print("Md5 digest:", digest_str) 63 | end 64 | 65 | local function base64() 66 | for i = 1,20 do 67 | local source = ("a"):rep(i) 68 | print("b64 source:", source) 69 | local encoded = crypt.encode_base64(source) 70 | print("b64 encode:", encoded) 71 | local decoded = crypt.decode_base64(encoded) 72 | print("b64 decode:", decoded) 73 | assert(source == decoded) 74 | end 75 | 76 | local table = { foo = "bar" } 77 | pprint("table - source:", table) 78 | local encoded = crypt.encode_base64(sys.serialize(table)) 79 | print("table - sys.serialize + b64 encode:", encoded) 80 | local decoded = sys.deserialize(crypt.decode_base64(encoded)) 81 | pprint("table - b64 decode + sys.deserialize:", decoded) 82 | end 83 | 84 | local function encrypt_decrypt_xtea() 85 | local test_string = "test string \t test aaaAAAAaaa123456789" 86 | print("*****ENCRYPT/DECRYPT*****") 87 | print("input:" .. test_string) 88 | 89 | local keys = { 90 | "",--empty also worked 91 | "ABDCEFG12345", 92 | "DEFOLD CRYPT" 93 | } 94 | for _, key in ipairs(keys) do 95 | local encrypt = crypt.encrypt_xtea(test_string, key) 96 | local decrypt = crypt.decrypt_xtea(encrypt, key) 97 | print("key:" .. key) 98 | print("result:" .. encrypt) 99 | print("decrypt:" .. decrypt) 100 | assert(decrypt == test_string) 101 | assert(crypt.decrypt_xtea(encrypt, "bad key") ~= test_string) 102 | end 103 | 104 | local key17 = "1234567890abcdefg" 105 | local status, error =pcall(crypt.encrypt_xtea, test_string, key17) 106 | assert(not status) 107 | assert(error == "key should be <=16") 108 | status, error =pcall(crypt.decrypt_xtea, test_string, key17) 109 | assert(not status) 110 | assert(error == "key should be <=16") 111 | 112 | 113 | 114 | local test_string_zero_char = "test string\n\t\0 test aaaAAAAaaa123456789" 115 | local encrypt_0 = crypt.encrypt_xtea(test_string_zero_char, "a") 116 | local decrypt_0 = crypt.decrypt_xtea(encrypt_0, "a") 117 | assert(test_string_zero_char == decrypt_0) 118 | 119 | 120 | 121 | end 122 | 123 | function init(self) 124 | hash1() 125 | hash256() 126 | hash512() 127 | hashmd5() 128 | base64() 129 | encrypt_decrypt_xtea() 130 | end 131 | -------------------------------------------------------------------------------- /game.project: -------------------------------------------------------------------------------- 1 | [bootstrap] 2 | main_collection = /example/example.collectionc 3 | 4 | [script] 5 | shared_state = 1 6 | 7 | [display] 8 | width = 960 9 | height = 640 10 | 11 | [project] 12 | title = crypt 13 | 14 | [library] 15 | include_dirs = crypt 16 | -------------------------------------------------------------------------------- /input/game.input_binding: -------------------------------------------------------------------------------- 1 | mouse_trigger { 2 | input: MOUSE_BUTTON_1 3 | action: "touch" 4 | } 5 | --------------------------------------------------------------------------------