From b5698dc588da5e00a81223cf9de733a8d0b49cf7 Mon Sep 17 00:00:00 2001 From: tanjie Date: Wed, 3 Jun 2015 21:07:25 +0800 Subject: [PATCH 1/2] =?UTF-8?q?xxtea=E5=8A=A0=E5=AF=86=EF=BC=8Cmd5,=20base?= =?UTF-8?q?64?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extensions/Android.mk | 14 +- extensions/CrossAppExt.h | 2 +- extensions/crypto/CACrypto.cpp | 129 ++++++++++++ extensions/crypto/CACrypto.h | 74 +++++++ extensions/crypto/base64/libbase64.c | 209 +++++++++++++++++++ extensions/crypto/base64/libbase64.h | 101 +++++++++ extensions/crypto/md5/md5.c | 295 +++++++++++++++++++++++++++ extensions/crypto/md5/md5.h | 45 ++++ extensions/crypto/xxtea/xxtea.c | 176 ++++++++++++++++ extensions/crypto/xxtea/xxtea.h | 56 +++++ 10 files changed, 1093 insertions(+), 8 deletions(-) create mode 100644 extensions/crypto/CACrypto.cpp create mode 100644 extensions/crypto/CACrypto.h create mode 100755 extensions/crypto/base64/libbase64.c create mode 100755 extensions/crypto/base64/libbase64.h create mode 100755 extensions/crypto/md5/md5.c create mode 100755 extensions/crypto/md5/md5.h create mode 100644 extensions/crypto/xxtea/xxtea.c create mode 100644 extensions/crypto/xxtea/xxtea.h diff --git a/extensions/Android.mk b/extensions/Android.mk index 9331fc859..4f62b77c3 100755 --- a/extensions/Android.mk +++ b/extensions/Android.mk @@ -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) diff --git a/extensions/CrossAppExt.h b/extensions/CrossAppExt.h index 1e3027b20..008e7b804 100755 --- a/extensions/CrossAppExt.h +++ b/extensions/CrossAppExt.h @@ -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__ */ diff --git a/extensions/crypto/CACrypto.cpp b/extensions/crypto/CACrypto.cpp new file mode 100644 index 000000000..d9a1eb556 --- /dev/null +++ b/extensions/crypto/CACrypto.cpp @@ -0,0 +1,129 @@ +// +// 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(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 < 16; ++i) + { + unsigned char c = bin[i]; + hex[ci++] = hextable[(c >> 4) & 0x0f]; + hex[ci++] = hextable[c & 0x0f]; + } + + return hex; +} + + +NS_CC_EXT_END \ No newline at end of file diff --git a/extensions/crypto/CACrypto.h b/extensions/crypto/CACrypto.h new file mode 100644 index 000000000..3f9046625 --- /dev/null +++ b/extensions/crypto/CACrypto.h @@ -0,0 +1,74 @@ +// +// CACrypto.h +// AppGift +// +// Created by tanjie on 15/6/3. +// +// + +#ifndef __AppGift__CACrypto__ +#define __AppGift__CACrypto__ + +#include + +#include +#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); + +#pragma mark - +#pragma mark private methods + +private: + CACrypto(void) {} + + static char* bin2hex(unsigned char* bin, int binLength); +}; + +NS_CC_EXT_END + +#endif /* defined(__AppGift__CACrypto__) */ diff --git a/extensions/crypto/base64/libbase64.c b/extensions/crypto/base64/libbase64.c new file mode 100755 index 000000000..a1796cc46 --- /dev/null +++ b/extensions/crypto/base64/libbase64.c @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ +/* ==================================================================== + * Copyright (c) 1995-1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group and was originally based + * on public domain software written at the National Center for + * Supercomputing Applications, University of Illinois, Urbana-Champaign. + * For more information on the Apache Group and the Apache HTTP server + * project, please see . + * + */ + +/* Base64 encoder/decoder. Originally Apache file ap_base64.c + */ + +#include + +#include "libbase64.h" + +/* aaaack but it's fast and const should make it shared text page. */ +static const unsigned char pr2six[256] = +{ + /* ASCII table */ + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 62, 64, 64, 64, 63, + 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 64, 64, 64, 64, 64, 64, + 64, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 64, 64, 64, 64, 64, + 64, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, + 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64, 64 +}; + +int Base64decode_len(const char *bufcoded) +{ + int nbytesdecoded; + register const unsigned char *bufin; + register int nprbytes; + + bufin = (const unsigned char *) bufcoded; + while (pr2six[*(bufin++)] <= 63); + + nprbytes = (int)((bufin - (const unsigned char *) bufcoded) - 1); + nbytesdecoded = ((nprbytes + 3) / 4) * 3; + + return nbytesdecoded + 1; +} + +int Base64decode(char *bufplain, const char *bufcoded) +{ + int nbytesdecoded; + register const unsigned char *bufin; + register unsigned char *bufout; + register int nprbytes; + + bufin = (const unsigned char *) bufcoded; + while (pr2six[*(bufin++)] <= 63); + nprbytes = (int)((bufin - (const unsigned char *) bufcoded) - 1); + nbytesdecoded = ((nprbytes + 3) / 4) * 3; + + bufout = (unsigned char *) bufplain; + bufin = (const unsigned char *) bufcoded; + + while (nprbytes > 4) { + *(bufout++) = + (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); + *(bufout++) = + (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); + *(bufout++) = + (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); + bufin += 4; + nprbytes -= 4; + } + + /* Note: (nprbytes == 1) would be an error, so just ingore that case */ + if (nprbytes > 1) { + *(bufout++) = + (unsigned char) (pr2six[*bufin] << 2 | pr2six[bufin[1]] >> 4); + } + if (nprbytes > 2) { + *(bufout++) = + (unsigned char) (pr2six[bufin[1]] << 4 | pr2six[bufin[2]] >> 2); + } + if (nprbytes > 3) { + *(bufout++) = + (unsigned char) (pr2six[bufin[2]] << 6 | pr2six[bufin[3]]); + } + + *(bufout++) = '\0'; + nbytesdecoded -= (4 - nprbytes) & 3; + return nbytesdecoded; +} + +static const char basis_64[] = +"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +int Base64encode_len(int len) +{ + return ((len + 2) / 3 * 4) + 1; +} + +int Base64encode(char *encoded, const char *string, int len) +{ + int i; + char *p; + + p = encoded; + for (i = 0; i < len - 2; i += 3) { + *p++ = basis_64[(string[i] >> 2) & 0x3F]; + *p++ = basis_64[((string[i] & 0x3) << 4) | + ((int) (string[i + 1] & 0xF0) >> 4)]; + *p++ = basis_64[((string[i + 1] & 0xF) << 2) | + ((int) (string[i + 2] & 0xC0) >> 6)]; + *p++ = basis_64[string[i + 2] & 0x3F]; + } + if (i < len) { + *p++ = basis_64[(string[i] >> 2) & 0x3F]; + if (i == (len - 1)) { + *p++ = basis_64[((string[i] & 0x3) << 4)]; + *p++ = '='; + } + else { + *p++ = basis_64[((string[i] & 0x3) << 4) | + ((int) (string[i + 1] & 0xF0) >> 4)]; + *p++ = basis_64[((string[i + 1] & 0xF) << 2)]; + } + *p++ = '='; + } + + *p++ = '\0'; + return (int)(p - encoded); +} diff --git a/extensions/crypto/base64/libbase64.h b/extensions/crypto/base64/libbase64.h new file mode 100755 index 000000000..12d10a755 --- /dev/null +++ b/extensions/crypto/base64/libbase64.h @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2003 Apple Computer, Inc. All rights reserved. + * + * @APPLE_LICENSE_HEADER_START@ + * + * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved. + * + * This file contains Original Code and/or Modifications of Original Code + * as defined in and that are subject to the Apple Public Source License + * Version 2.0 (the 'License'). You may not use this file except in + * compliance with the License. Please obtain a copy of the License at + * http://www.opensource.apple.com/apsl/ and read it before using this + * file. + * + * The Original Code and all software distributed under the License are + * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER + * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, + * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. + * Please see the License for the specific language governing rights and + * limitations under the License. + * + * @APPLE_LICENSE_HEADER_END@ + */ +/* ==================================================================== + * Copyright (c) 1995-1999 The Apache Group. All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * + * 3. All advertising materials mentioning features or use of this + * software must display the following acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * 4. The names "Apache Server" and "Apache Group" must not be used to + * endorse or promote products derived from this software without + * prior written permission. For written permission, please contact + * apache@apache.org. + * + * 5. Products derived from this software may not be called "Apache" + * nor may "Apache" appear in their names without prior written + * permission of the Apache Group. + * + * 6. Redistributions of any form whatsoever must retain the following + * acknowledgment: + * "This product includes software developed by the Apache Group + * for use in the Apache HTTP server project (http://www.apache.org/)." + * + * THIS SOFTWARE IS PROVIDED BY THE APACHE GROUP ``AS IS'' AND ANY + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE APACHE GROUP OR + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED + * OF THE POSSIBILITY OF SUCH DAMAGE. + * ==================================================================== + * + * This software consists of voluntary contributions made by many + * individuals on behalf of the Apache Group and was originally based + * on public domain software written at the National Center for + * Supercomputing Applications, University of Illinois, Urbana-Champaign. + * For more information on the Apache Group and the Apache HTTP server + * project, please see . + * + */ + + + +#ifndef _BASE64_H_ +#define _BASE64_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + int Base64encode_len(int len); + int Base64encode(char * coded_dst, const char *plain_src,int len_plain_src); + + int Base64decode_len(const char * coded_src); + int Base64decode(char * plain_dst, const char *coded_src); + +#ifdef __cplusplus +} +#endif + +#endif //_BASE64_H_ diff --git a/extensions/crypto/md5/md5.c b/extensions/crypto/md5/md5.c new file mode 100755 index 000000000..7d43a603a --- /dev/null +++ b/extensions/crypto/md5/md5.c @@ -0,0 +1,295 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * (This is a heavily cut-down "BSD license".) + * + * This differs from Colin Plumb's older public domain implementation in that + * no exactly 32-bit integer data type is required (any 32-bit or wider + * unsigned integer data type will do), there's no compile-time endianness + * configuration, and the function prototypes match OpenSSL's. No code from + * Colin Plumb's implementation has been reused; this comment merely compares + * the properties of the two independent implementations. + * + * The primary goals of this implementation are portability and ease of use. + * It is meant to be fast, but not as fast as possible. Some known + * optimizations are not included to reduce source code size and avoid + * compile-time configuration. + */ + +#ifndef HAVE_OPENSSL + +#include + +#include "md5.h" + +/* + * The basic MD5 functions. + * + * F and G are optimized compared to their RFC 1321 definitions for + * architectures that lack an AND-NOT instruction, just like in Colin Plumb's + * implementation. + */ +#define F(x, y, z) ((z) ^ ((x) & ((y) ^ (z)))) +#define G(x, y, z) ((y) ^ ((z) & ((x) ^ (y)))) +#define H(x, y, z) ((x) ^ (y) ^ (z)) +#define I(x, y, z) ((y) ^ ((x) | ~(z))) + +/* + * The MD5 transformation for all four rounds. + */ +#define STEP(f, a, b, c, d, x, t, s) \ + (a) += f((b), (c), (d)) + (x) + (t); \ + (a) = (((a) << (s)) | (((a) & 0xffffffff) >> (32 - (s)))); \ + (a) += (b); + +/* + * SET reads 4 input bytes in little-endian byte order and stores them + * in a properly aligned word in host byte order. + * + * The check for little-endian architectures that tolerate unaligned + * memory accesses is just an optimization. Nothing will break if it + * doesn't work. + */ +#if defined(__i386__) || defined(__x86_64__) || defined(__vax__) +#define SET(n) \ + (*(MD5_u32plus *)&ptr[(n) * 4]) +#define GET(n) \ + SET(n) +#else +#define SET(n) \ + (ctx->block[(n)] = \ + (MD5_u32plus)ptr[(n) * 4] | \ + ((MD5_u32plus)ptr[(n) * 4 + 1] << 8) | \ + ((MD5_u32plus)ptr[(n) * 4 + 2] << 16) | \ + ((MD5_u32plus)ptr[(n) * 4 + 3] << 24)) +#define GET(n) \ + (ctx->block[(n)]) +#endif + +/* + * This processes one or more 64-byte data blocks, but does NOT update + * the bit counters. There are no alignment requirements. + */ +static void *body(MD5_CTX *ctx, void *data, unsigned long size) +{ + unsigned char *ptr; + MD5_u32plus a, b, c, d; + MD5_u32plus saved_a, saved_b, saved_c, saved_d; + + ptr = data; + + a = ctx->a; + b = ctx->b; + c = ctx->c; + d = ctx->d; + + do { + saved_a = a; + saved_b = b; + saved_c = c; + saved_d = d; + +/* Round 1 */ + STEP(F, a, b, c, d, SET(0), 0xd76aa478, 7) + STEP(F, d, a, b, c, SET(1), 0xe8c7b756, 12) + STEP(F, c, d, a, b, SET(2), 0x242070db, 17) + STEP(F, b, c, d, a, SET(3), 0xc1bdceee, 22) + STEP(F, a, b, c, d, SET(4), 0xf57c0faf, 7) + STEP(F, d, a, b, c, SET(5), 0x4787c62a, 12) + STEP(F, c, d, a, b, SET(6), 0xa8304613, 17) + STEP(F, b, c, d, a, SET(7), 0xfd469501, 22) + STEP(F, a, b, c, d, SET(8), 0x698098d8, 7) + STEP(F, d, a, b, c, SET(9), 0x8b44f7af, 12) + STEP(F, c, d, a, b, SET(10), 0xffff5bb1, 17) + STEP(F, b, c, d, a, SET(11), 0x895cd7be, 22) + STEP(F, a, b, c, d, SET(12), 0x6b901122, 7) + STEP(F, d, a, b, c, SET(13), 0xfd987193, 12) + STEP(F, c, d, a, b, SET(14), 0xa679438e, 17) + STEP(F, b, c, d, a, SET(15), 0x49b40821, 22) + +/* Round 2 */ + STEP(G, a, b, c, d, GET(1), 0xf61e2562, 5) + STEP(G, d, a, b, c, GET(6), 0xc040b340, 9) + STEP(G, c, d, a, b, GET(11), 0x265e5a51, 14) + STEP(G, b, c, d, a, GET(0), 0xe9b6c7aa, 20) + STEP(G, a, b, c, d, GET(5), 0xd62f105d, 5) + STEP(G, d, a, b, c, GET(10), 0x02441453, 9) + STEP(G, c, d, a, b, GET(15), 0xd8a1e681, 14) + STEP(G, b, c, d, a, GET(4), 0xe7d3fbc8, 20) + STEP(G, a, b, c, d, GET(9), 0x21e1cde6, 5) + STEP(G, d, a, b, c, GET(14), 0xc33707d6, 9) + STEP(G, c, d, a, b, GET(3), 0xf4d50d87, 14) + STEP(G, b, c, d, a, GET(8), 0x455a14ed, 20) + STEP(G, a, b, c, d, GET(13), 0xa9e3e905, 5) + STEP(G, d, a, b, c, GET(2), 0xfcefa3f8, 9) + STEP(G, c, d, a, b, GET(7), 0x676f02d9, 14) + STEP(G, b, c, d, a, GET(12), 0x8d2a4c8a, 20) + +/* Round 3 */ + STEP(H, a, b, c, d, GET(5), 0xfffa3942, 4) + STEP(H, d, a, b, c, GET(8), 0x8771f681, 11) + STEP(H, c, d, a, b, GET(11), 0x6d9d6122, 16) + STEP(H, b, c, d, a, GET(14), 0xfde5380c, 23) + STEP(H, a, b, c, d, GET(1), 0xa4beea44, 4) + STEP(H, d, a, b, c, GET(4), 0x4bdecfa9, 11) + STEP(H, c, d, a, b, GET(7), 0xf6bb4b60, 16) + STEP(H, b, c, d, a, GET(10), 0xbebfbc70, 23) + STEP(H, a, b, c, d, GET(13), 0x289b7ec6, 4) + STEP(H, d, a, b, c, GET(0), 0xeaa127fa, 11) + STEP(H, c, d, a, b, GET(3), 0xd4ef3085, 16) + STEP(H, b, c, d, a, GET(6), 0x04881d05, 23) + STEP(H, a, b, c, d, GET(9), 0xd9d4d039, 4) + STEP(H, d, a, b, c, GET(12), 0xe6db99e5, 11) + STEP(H, c, d, a, b, GET(15), 0x1fa27cf8, 16) + STEP(H, b, c, d, a, GET(2), 0xc4ac5665, 23) + +/* Round 4 */ + STEP(I, a, b, c, d, GET(0), 0xf4292244, 6) + STEP(I, d, a, b, c, GET(7), 0x432aff97, 10) + STEP(I, c, d, a, b, GET(14), 0xab9423a7, 15) + STEP(I, b, c, d, a, GET(5), 0xfc93a039, 21) + STEP(I, a, b, c, d, GET(12), 0x655b59c3, 6) + STEP(I, d, a, b, c, GET(3), 0x8f0ccc92, 10) + STEP(I, c, d, a, b, GET(10), 0xffeff47d, 15) + STEP(I, b, c, d, a, GET(1), 0x85845dd1, 21) + STEP(I, a, b, c, d, GET(8), 0x6fa87e4f, 6) + STEP(I, d, a, b, c, GET(15), 0xfe2ce6e0, 10) + STEP(I, c, d, a, b, GET(6), 0xa3014314, 15) + STEP(I, b, c, d, a, GET(13), 0x4e0811a1, 21) + STEP(I, a, b, c, d, GET(4), 0xf7537e82, 6) + STEP(I, d, a, b, c, GET(11), 0xbd3af235, 10) + STEP(I, c, d, a, b, GET(2), 0x2ad7d2bb, 15) + STEP(I, b, c, d, a, GET(9), 0xeb86d391, 21) + + a += saved_a; + b += saved_b; + c += saved_c; + d += saved_d; + + ptr += 64; + } while (size -= 64); + + ctx->a = a; + ctx->b = b; + ctx->c = c; + ctx->d = d; + + return ptr; +} + +void MD5_Init(MD5_CTX *ctx) +{ + ctx->a = 0x67452301; + ctx->b = 0xefcdab89; + ctx->c = 0x98badcfe; + ctx->d = 0x10325476; + + ctx->lo = 0; + ctx->hi = 0; +} + +void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size) +{ + MD5_u32plus saved_lo; + unsigned long used, free; + + saved_lo = ctx->lo; + if ((ctx->lo = (saved_lo + size) & 0x1fffffff) < saved_lo) + ctx->hi++; + ctx->hi += size >> 29; + + used = saved_lo & 0x3f; + + if (used) { + free = 64 - used; + + if (size < free) { + memcpy(&ctx->buffer[used], data, size); + return; + } + + memcpy(&ctx->buffer[used], data, free); + data = (unsigned char *)data + free; + size -= free; + body(ctx, ctx->buffer, 64); + } + + if (size >= 64) { + data = body(ctx, data, size & ~(unsigned long)0x3f); + size &= 0x3f; + } + + memcpy(ctx->buffer, data, size); +} + +void MD5_Final(unsigned char *result, MD5_CTX *ctx) +{ + unsigned long used, free; + + used = ctx->lo & 0x3f; + + ctx->buffer[used++] = 0x80; + + free = 64 - used; + + if (free < 8) { + memset(&ctx->buffer[used], 0, free); + body(ctx, ctx->buffer, 64); + used = 0; + free = 64; + } + + memset(&ctx->buffer[used], 0, free - 8); + + ctx->lo <<= 3; + ctx->buffer[56] = ctx->lo; + ctx->buffer[57] = ctx->lo >> 8; + ctx->buffer[58] = ctx->lo >> 16; + ctx->buffer[59] = ctx->lo >> 24; + ctx->buffer[60] = ctx->hi; + ctx->buffer[61] = ctx->hi >> 8; + ctx->buffer[62] = ctx->hi >> 16; + ctx->buffer[63] = ctx->hi >> 24; + + body(ctx, ctx->buffer, 64); + + result[0] = ctx->a; + result[1] = ctx->a >> 8; + result[2] = ctx->a >> 16; + result[3] = ctx->a >> 24; + result[4] = ctx->b; + result[5] = ctx->b >> 8; + result[6] = ctx->b >> 16; + result[7] = ctx->b >> 24; + result[8] = ctx->c; + result[9] = ctx->c >> 8; + result[10] = ctx->c >> 16; + result[11] = ctx->c >> 24; + result[12] = ctx->d; + result[13] = ctx->d >> 8; + result[14] = ctx->d >> 16; + result[15] = ctx->d >> 24; + + memset(ctx, 0, sizeof(*ctx)); +} + +#endif diff --git a/extensions/crypto/md5/md5.h b/extensions/crypto/md5/md5.h new file mode 100755 index 000000000..f1a685764 --- /dev/null +++ b/extensions/crypto/md5/md5.h @@ -0,0 +1,45 @@ +/* + * This is an OpenSSL-compatible implementation of the RSA Data Security, Inc. + * MD5 Message-Digest Algorithm (RFC 1321). + * + * Homepage: + * http://openwall.info/wiki/people/solar/software/public-domain-source-code/md5 + * + * Author: + * Alexander Peslyak, better known as Solar Designer + * + * This software was written by Alexander Peslyak in 2001. No copyright is + * claimed, and the software is hereby placed in the public domain. + * In case this attempt to disclaim copyright and place the software in the + * public domain is deemed null and void, then the software is + * Copyright (c) 2001 Alexander Peslyak and it is hereby released to the + * general public under the following terms: + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted. + * + * There's ABSOLUTELY NO WARRANTY, express or implied. + * + * See md5.c for more information. + */ + +#ifdef HAVE_OPENSSL +#include +#elif !defined(_MD5_H) +#define _MD5_H + +/* Any 32-bit or wider unsigned integer data type will do */ +typedef unsigned int MD5_u32plus; + +typedef struct { + MD5_u32plus lo, hi; + MD5_u32plus a, b, c, d; + unsigned char buffer[64]; + MD5_u32plus block[16]; +} MD5_CTX; + +extern void MD5_Init(MD5_CTX *ctx); +extern void MD5_Update(MD5_CTX *ctx, void *data, unsigned long size); +extern void MD5_Final(unsigned char *result, MD5_CTX *ctx); + +#endif diff --git a/extensions/crypto/xxtea/xxtea.c b/extensions/crypto/xxtea/xxtea.c new file mode 100644 index 000000000..81cd39abe --- /dev/null +++ b/extensions/crypto/xxtea/xxtea.c @@ -0,0 +1,176 @@ +/*********************************************************************** + + Copyright 2006-2009 Ma Bingyao + Copyright 2013 Gao Chunhui, Liu Tao + + These sources is free software. Redistributions of source code must + retain the above copyright notice. Redistributions in binary form + must reproduce the above copyright notice. You can redistribute it + freely. You can use it with any free or commercial software. + + These sources is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. Without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + github: https://github.com/liut/pecl-xxtea + + *************************************************************************/ + +#include "xxtea.h" +#include +#include + +static void xxtea_long_encrypt(xxtea_long *v, xxtea_long len, xxtea_long *k) +{ + xxtea_long n = len - 1; + xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = 0, e; + if (n < 1) { + return; + } + while (0 < q--) { + sum += XXTEA_DELTA; + e = sum >> 2 & 3; + for (p = 0; p < n; p++) { + y = v[p + 1]; + z = v[p] += XXTEA_MX; + } + y = v[0]; + z = v[n] += XXTEA_MX; + } +} + +static void xxtea_long_decrypt(xxtea_long *v, xxtea_long len, xxtea_long *k) +{ + xxtea_long n = len - 1; + xxtea_long z = v[n], y = v[0], p, q = 6 + 52 / (n + 1), sum = q * XXTEA_DELTA, e; + if (n < 1) { + return; + } + while (sum != 0) { + e = sum >> 2 & 3; + for (p = n; p > 0; p--) { + z = v[p - 1]; + y = v[p] -= XXTEA_MX; + } + z = v[n]; + y = v[0] -= XXTEA_MX; + sum -= XXTEA_DELTA; + } +} + +static xxtea_byte *fix_key_length(const xxtea_byte *key, xxtea_long key_len) +{ + xxtea_byte *tmp = (xxtea_byte *)malloc(16); + memcpy(tmp, key, key_len); + memset(tmp + key_len, '\0', 16 - key_len); + return tmp; +} + +static xxtea_long *xxtea_to_long_array(const xxtea_byte *data, xxtea_long len, int include_length, xxtea_long *ret_len) { + xxtea_long i, n, *result; + + n = len >> 2; + n = (((len & 3) == 0) ? n : n + 1); + if (include_length) { + result = (xxtea_long *)malloc((n + 1) << 2); + result[n] = len; + *ret_len = n + 1; + } else { + result = (xxtea_long *)malloc(n << 2); + *ret_len = n; + } + memset(result, 0, n << 2); + for (i = 0; i < len; i++) { + result[i >> 2] |= (xxtea_long)data[i] << ((i & 3) << 3); + } + + return result; +} + +static xxtea_byte *xxtea_to_byte_array(const xxtea_long *data, xxtea_long len, int include_length, xxtea_long *ret_len) { + xxtea_long i, n, m; + xxtea_byte *result; + + n = len << 2; + if (include_length) { + m = data[len - 1]; + if ((m < n - 7) || (m > n - 4)) return NULL; + n = m; + } + result = (xxtea_byte *)malloc(n + 1); + for (i = 0; i < n; i++) { + result[i] = (xxtea_byte)((data[i >> 2] >> ((i & 3) << 3)) & 0xff); + } + result[n] = '\0'; + *ret_len = n; + + return result; +} + +static xxtea_byte *do_xxtea_encrypt(const xxtea_byte *data, xxtea_long len, const xxtea_byte *key, xxtea_long *ret_len) { + xxtea_byte *result; + xxtea_long *v, *k, v_len, k_len; + + v = xxtea_to_long_array(data, len, 1, &v_len); + k = xxtea_to_long_array(key, 16, 0, &k_len); + xxtea_long_encrypt(v, v_len, k); + result = xxtea_to_byte_array(v, v_len, 0, ret_len); + free(v); + free(k); + + return result; +} + +static xxtea_byte *do_xxtea_decrypt(const xxtea_byte *data, xxtea_long len, const xxtea_byte *key, xxtea_long *ret_len) { + xxtea_byte *result; + xxtea_long *v, *k, v_len, k_len; + + v = xxtea_to_long_array(data, len, 0, &v_len); + k = xxtea_to_long_array(key, 16, 0, &k_len); + xxtea_long_decrypt(v, v_len, k); + result = xxtea_to_byte_array(v, v_len, 1, ret_len); + free(v); + free(k); + + return result; +} + +xxtea_byte *xxtea_encrypt(const xxtea_byte *data, xxtea_long data_len, const xxtea_byte *key, xxtea_long key_len, xxtea_long *ret_length) +{ + xxtea_byte *result; + + *ret_length = 0; + + if (key_len < 16) { + xxtea_byte *key2 = fix_key_length(key, key_len); + result = do_xxtea_encrypt(data, data_len, key2, ret_length); + free(key2); + } + else + { + result = do_xxtea_encrypt(data, data_len, key, ret_length); + } + + return result; +} + +xxtea_byte *xxtea_decrypt(const xxtea_byte *data, xxtea_long data_len, const xxtea_byte *key, xxtea_long key_len, xxtea_long *ret_length) +{ + xxtea_byte *result; + + *ret_length = 0; + + if (key_len < 16) { + xxtea_byte *key2 = fix_key_length(key, key_len); + result = do_xxtea_decrypt(data, data_len, key2, ret_length); + free(key2); + } + else + { + result = do_xxtea_decrypt(data, data_len, key, ret_length); + } + + return result; +} + +/* }}} */ diff --git a/extensions/crypto/xxtea/xxtea.h b/extensions/crypto/xxtea/xxtea.h new file mode 100644 index 000000000..b09dc3349 --- /dev/null +++ b/extensions/crypto/xxtea/xxtea.h @@ -0,0 +1,56 @@ +/*********************************************************************** + + Copyright 2006-2009 Ma Bingyao + Copyright 2013 Gao Chunhui, Liu Tao + + These sources is free software. Redistributions of source code must + retain the above copyright notice. Redistributions in binary form + must reproduce the above copyright notice. You can redistribute it + freely. You can use it with any free or commercial software. + + These sources is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY. Without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + github: https://github.com/liut/pecl-xxtea + + *************************************************************************/ + +#ifndef XXTEA_H +#define XXTEA_H + +#include /* for size_t & NULL declarations */ + +#if defined(_MSC_VER) + +typedef unsigned __int32 xxtea_long; + +#else + +#if defined(__FreeBSD__) && __FreeBSD__ < 5 +/* FreeBSD 4 doesn't have stdint.h file */ +#include +#else +#include +#endif + +typedef uint32_t xxtea_long; +typedef unsigned char xxtea_byte; + +#endif /* end of if defined(_MSC_VER) */ + +#define XXTEA_MX (z >> 5 ^ y << 2) + (y >> 3 ^ z << 4) ^ (sum ^ y) + (k[p & 3 ^ e] ^ z) +#define XXTEA_DELTA 0x9e3779b9 + +#ifdef __cplusplus +extern "C" { +#endif + +xxtea_byte *xxtea_encrypt(const xxtea_byte *data, xxtea_long data_len, const xxtea_byte *key, xxtea_long key_len, xxtea_long *ret_length); +xxtea_byte *xxtea_decrypt(const xxtea_byte *data, xxtea_long data_len, const xxtea_byte *key, xxtea_long key_len, xxtea_long *ret_length); + +#ifdef __cplusplus +} +#endif + +#endif From 3757d2651080d85e0c473c861bf687af3fb50fe2 Mon Sep 17 00:00:00 2001 From: tanjie Date: Thu, 4 Jun 2015 14:12:44 +0800 Subject: [PATCH 2/2] fix bug & add hex2bin --- extensions/crypto/CACrypto.cpp | 48 ++++++++++++++++++++++++++++++++-- extensions/crypto/CACrypto.h | 4 +-- 2 files changed, 48 insertions(+), 4 deletions(-) diff --git a/extensions/crypto/CACrypto.cpp b/extensions/crypto/CACrypto.cpp index d9a1eb556..291aa1844 100644 --- a/extensions/crypto/CACrypto.cpp +++ b/extensions/crypto/CACrypto.cpp @@ -106,7 +106,7 @@ const string CACrypto::MD5String(void* input, int inputLength) return ret; } -char* CACrypto::bin2hex(unsigned char* bin, int binLength) +char *CACrypto::bin2hex(unsigned char* bin, int binLength) { static const char* hextable = "0123456789abcdef"; @@ -115,7 +115,7 @@ char* CACrypto::bin2hex(unsigned char* bin, int binLength) memset(hex, 0, sizeof(char) * hexLength); int ci = 0; - for (int i = 0; i < 16; ++i) + for (int i = 0; i < binLength; ++i) { unsigned char c = bin[i]; hex[ci++] = hextable[(c >> 4) & 0x0f]; @@ -125,5 +125,49 @@ char* CACrypto::bin2hex(unsigned char* bin, int binLength) 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 \ No newline at end of file diff --git a/extensions/crypto/CACrypto.h b/extensions/crypto/CACrypto.h index 3f9046625..6048d5ec1 100644 --- a/extensions/crypto/CACrypto.h +++ b/extensions/crypto/CACrypto.h @@ -60,13 +60,13 @@ class CACrypto 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) {} - - static char* bin2hex(unsigned char* bin, int binLength); }; NS_CC_EXT_END