diff --git a/build/cocos2d_libs.xcodeproj/project.pbxproj b/build/cocos2d_libs.xcodeproj/project.pbxproj index 5c3980d..2a13272 100644 --- a/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -279,6 +279,8 @@ D5253E77287D67D800DD257B /* sysendian.h in Headers */ = {isa = PBXBuildFile; fileRef = D5253E71287D67D800DD257B /* sysendian.h */; }; D5253E78287D67D800DD257B /* sha256.c in Sources */ = {isa = PBXBuildFile; fileRef = D5253E73287D67D800DD257B /* sha256.c */; }; D5253E79287D67D800DD257B /* crypto_scrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = D5253E74287D67D800DD257B /* crypto_scrypt.c */; }; + D53541262880633900372C0F /* base64.h in Headers */ = {isa = PBXBuildFile; fileRef = D53541242880633800372C0F /* base64.h */; }; + D53541272880633900372C0F /* base64.c in Sources */ = {isa = PBXBuildFile; fileRef = D53541252880633900372C0F /* base64.c */; }; D5538B9F287D6F9C000BDFB6 /* native-crypto.cpp in Sources */ = {isa = PBXBuildFile; fileRef = D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */; }; D5538BA0287D6F9C000BDFB6 /* native-crypto.h in Headers */ = {isa = PBXBuildFile; fileRef = D5538B9E287D6F9B000BDFB6 /* native-crypto.h */; }; ED18118623D6A9B600DED444 /* edtaa3func.h in Headers */ = {isa = PBXBuildFile; fileRef = ED18118323D6A9B500DED444 /* edtaa3func.h */; }; @@ -641,6 +643,8 @@ D5253E72287D67D800DD257B /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = readme.md; sourceTree = ""; }; D5253E73287D67D800DD257B /* sha256.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha256.c; sourceTree = ""; }; D5253E74287D67D800DD257B /* crypto_scrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = crypto_scrypt.c; sourceTree = ""; }; + D53541242880633800372C0F /* base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = base64.h; sourceTree = ""; }; + D53541252880633900372C0F /* base64.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = base64.c; sourceTree = ""; }; D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "native-crypto.cpp"; sourceTree = ""; }; D5538B9E287D6F9B000BDFB6 /* native-crypto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "native-crypto.h"; sourceTree = ""; }; ED18118323D6A9B500DED444 /* edtaa3func.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = edtaa3func.h; path = ../external/sources/edtaa3func/edtaa3func.h; sourceTree = ""; }; @@ -1373,11 +1377,13 @@ children = ( D5253E6F287D67D800DD257B /* crypto_scrypt.h */, D5253E70287D67D800DD257B /* sha256.h */, + D5253E73287D67D800DD257B /* sha256.c */, + D53541252880633900372C0F /* base64.c */, + D53541242880633800372C0F /* base64.h */, D5253E71287D67D800DD257B /* sysendian.h */, D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */, D5538B9E287D6F9B000BDFB6 /* native-crypto.h */, D5253E72287D67D800DD257B /* readme.md */, - D5253E73287D67D800DD257B /* sha256.c */, D5253E74287D67D800DD257B /* crypto_scrypt.c */, ); name = scrypt; @@ -1578,6 +1584,7 @@ 1A28FF5C1F20AFAB007A1D9D /* NSURLRequest+SRWebSocketPrivate.h in Headers */, ED5A63FB236C384C007A0CF0 /* WebSocketServer.h in Headers */, 1A28FF6E1F20AFAB007A1D9D /* SRError.h in Headers */, + D53541262880633900372C0F /* base64.h in Headers */, 46FDDBC4202ADDCE00931238 /* ccConfig.h in Headers */, 46AE3FE02092F3A600F3A228 /* env.h in Headers */, 469303A32046AE05004A3D6C /* Object.hpp in Headers */, @@ -1691,6 +1698,7 @@ 46FDDB8C202ADDCE00931238 /* CCAutoreleasePool.cpp in Sources */, 461786622052607E008256E1 /* jsb_socketio.cpp in Sources */, 4693039F2046AE05004A3D6C /* Object.mm in Sources */, + D53541272880633900372C0F /* base64.c in Sources */, 461786562052301A008256E1 /* CCScheduler.cpp in Sources */, 46FDDB8E202ADDCE00931238 /* ccTypes.cpp in Sources */, 50ABBD4D1925AB0000A911A9 /* MathUtil.cpp in Sources */, diff --git a/cocos/scrypt/crypto_scrypt.c b/cocos/scrypt/crypto_scrypt.c index 16b5aff..d3c32c5 100644 --- a/cocos/scrypt/crypto_scrypt.c +++ b/cocos/scrypt/crypto_scrypt.c @@ -37,6 +37,7 @@ #include "sysendian.h" #include "crypto_scrypt.h" +#include static void blkcpy(uint8_t *, uint8_t *, size_t); static void blkxor(uint8_t *, uint8_t *, size_t); @@ -282,3 +283,35 @@ err0: /* Failure! */ return (-1); } + +int uuid_v4_gen(char *buffer) +{ + union + { + struct + { + uint32_t time_low; + uint16_t time_mid; + uint16_t time_hi_and_version; + uint8_t clk_seq_hi_res; + uint8_t clk_seq_low; + uint8_t node[6]; + }; + uint8_t __rnd[16]; + } uuid; + + int rc = RAND_bytes(uuid.__rnd, sizeof(uuid)); + + // Refer Section 4.2 of RFC-4122 + // https://tools.ietf.org/html/rfc4122#section-4.2 + uuid.clk_seq_hi_res = (uint8_t) ((uuid.clk_seq_hi_res & 0x3F) | 0x80); + uuid.time_hi_and_version = (uint16_t) ((uuid.time_hi_and_version & 0x0FFF) | 0x4000); + + snprintf(buffer, 38, "%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x", + uuid.time_low, uuid.time_mid, uuid.time_hi_and_version, + uuid.clk_seq_hi_res, uuid.clk_seq_low, + uuid.node[0], uuid.node[1], uuid.node[2], + uuid.node[3], uuid.node[4], uuid.node[5]); + + return rc; +} diff --git a/cocos/scrypt/crypto_scrypt.h b/cocos/scrypt/crypto_scrypt.h index cd73a9f..fff4f29 100644 --- a/cocos/scrypt/crypto_scrypt.h +++ b/cocos/scrypt/crypto_scrypt.h @@ -43,5 +43,6 @@ int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, uint32_t, uint32_t, uint8_t *, size_t); +int uuid_v4_gen(char *buffer); #endif /* !_CRYPTO_SCRYPT_H_ */ diff --git a/cocos/scrypt/native-crypto.cpp b/cocos/scrypt/native-crypto.cpp index cc06083..1f356e8 100644 --- a/cocos/scrypt/native-crypto.cpp +++ b/cocos/scrypt/native-crypto.cpp @@ -6,41 +6,41 @@ */ #include "native-crypto.h" + extern "C" { - #include "crypto_scrypt.h" - #include "base64.h" +#include "crypto_scrypt.h" +#include "base64.h" } -#include -#include -#include +#include +#include +#include +#include +#include -#define COMPRESSED_PUBKEY_LENGTH 33 -#define DECOMPRESSED_PUBKEY_LENGTH 65 -#define PRIVKEY_LENGTH 64 - -int fast_crypto_scrypt (const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N, - uint32_t r, uint32_t p, uint8_t *buf, size_t buflen) -{ +int fast_crypto_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, + uint64_t N, + uint32_t r, uint32_t p, uint8_t *buf, size_t buflen) { return crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen); } int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N, uint32_t r, uint32_t p, size_t size, std::string *value) { - char *szPassword = const_cast(password->c_str()); + char *szPassword = const_cast(password->c_str()); size_t passwordBufSize = Base64decode_len(szPassword); - char *szSalt = const_cast(salt->c_str()); + char *szSalt = const_cast(salt->c_str()); size_t saltBufSize = Base64decode_len(szSalt); auto *passwordBuf = (unsigned char *) malloc(sizeof(char) * passwordBufSize); auto *saltBuf = (unsigned char *) malloc(sizeof(char) * saltBufSize); - int passwordBufLen = Base64decode((char *)passwordBuf, szPassword); - int saltBufLen = Base64decode((char *)saltBuf, szSalt); + int passwordBufLen = Base64decode((char *) passwordBuf, szPassword); + int saltBufLen = Base64decode((char *) saltBuf, szSalt); auto *buffer = (uint8_t *) malloc(sizeof(char) * size); - int result = fast_crypto_scrypt((uint8_t *) passwordBuf, passwordBufLen, (uint8_t *) saltBuf, saltBufLen, N, r, p, buffer, size); + int result = fast_crypto_scrypt((uint8_t *) passwordBuf, passwordBufLen, (uint8_t *) saltBuf, + saltBufLen, N, r, p, buffer, size); int b64encSize = Base64encode_len(size); - char *szB64Encoded = (char *)malloc(sizeof(char) * b64encSize); + char *szB64Encoded = (char *) malloc(sizeof(char) * b64encSize); int b64encLen = Base64encode(szB64Encoded, (const char *) buffer, size); value->assign(szB64Encoded); free(buffer); @@ -50,3 +50,35 @@ int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N, return result; } + +std::string get_uuid() { + static std::random_device dev; + static std::mt19937 rng(dev()); + + std::uniform_int_distribution dist(0, 15); + + const char *v = "0123456789abcdef"; + const bool dash[] = {false, false, false, false, true, false, true, false, true, false, true, + false, false, false, false, false}; + + std::stringstream res; + for (bool i : dash) { + if (i) res << "-"; + res << v[dist(rng)]; + res << v[dist(rng)]; + } + return res.str(); +} + +std::string uuid_v4() { + char uuidv4[38]; + int rc = uuid_v4_gen(uuidv4); + if (rc == 1) { + std::stringstream ss; + ss << uuidv4; + return ss.str(); + } else { + return get_uuid(); + } +} + diff --git a/cocos/scrypt/native-crypto.h b/cocos/scrypt/native-crypto.h index cbc09b1..cec3605 100644 --- a/cocos/scrypt/native-crypto.h +++ b/cocos/scrypt/native-crypto.h @@ -41,19 +41,14 @@ #include #include -#ifdef __cplusplus -extern "C" { -#endif -int fast_crypto_scrypt (const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, uint64_t N, - uint32_t r, uint32_t p, uint8_t *buf, size_t buflen); +int fast_crypto_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen, + uint64_t N, + uint32_t r, uint32_t p, uint8_t *buf, size_t buflen); -int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N, +int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N, uint32_t r, uint32_t p, size_t size, std::string *value); -#ifdef __cplusplus -} -#endif - +std::string uuid_v4(); #endif // native_crypto_h