增加一个生成uuid的方法

This commit is contained in:
cebgcontract 2022-07-20 17:46:19 +08:00
parent ec8a695e7a
commit 6c3f960907
5 changed files with 98 additions and 29 deletions

View File

@ -279,6 +279,8 @@
D5253E77287D67D800DD257B /* sysendian.h in Headers */ = {isa = PBXBuildFile; fileRef = D5253E71287D67D800DD257B /* sysendian.h */; }; D5253E77287D67D800DD257B /* sysendian.h in Headers */ = {isa = PBXBuildFile; fileRef = D5253E71287D67D800DD257B /* sysendian.h */; };
D5253E78287D67D800DD257B /* sha256.c in Sources */ = {isa = PBXBuildFile; fileRef = D5253E73287D67D800DD257B /* sha256.c */; }; D5253E78287D67D800DD257B /* sha256.c in Sources */ = {isa = PBXBuildFile; fileRef = D5253E73287D67D800DD257B /* sha256.c */; };
D5253E79287D67D800DD257B /* crypto_scrypt.c in Sources */ = {isa = PBXBuildFile; fileRef = D5253E74287D67D800DD257B /* crypto_scrypt.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 */; }; 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 */; }; D5538BA0287D6F9C000BDFB6 /* native-crypto.h in Headers */ = {isa = PBXBuildFile; fileRef = D5538B9E287D6F9B000BDFB6 /* native-crypto.h */; };
ED18118623D6A9B600DED444 /* edtaa3func.h in Headers */ = {isa = PBXBuildFile; fileRef = ED18118323D6A9B500DED444 /* edtaa3func.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 = "<group>"; }; D5253E72287D67D800DD257B /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = net.daringfireball.markdown; path = readme.md; sourceTree = "<group>"; };
D5253E73287D67D800DD257B /* sha256.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha256.c; sourceTree = "<group>"; }; D5253E73287D67D800DD257B /* sha256.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = sha256.c; sourceTree = "<group>"; };
D5253E74287D67D800DD257B /* crypto_scrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = crypto_scrypt.c; sourceTree = "<group>"; }; D5253E74287D67D800DD257B /* crypto_scrypt.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = crypto_scrypt.c; sourceTree = "<group>"; };
D53541242880633800372C0F /* base64.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = base64.h; sourceTree = "<group>"; };
D53541252880633900372C0F /* base64.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = base64.c; sourceTree = "<group>"; };
D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "native-crypto.cpp"; sourceTree = "<group>"; }; D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = "native-crypto.cpp"; sourceTree = "<group>"; };
D5538B9E287D6F9B000BDFB6 /* native-crypto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "native-crypto.h"; sourceTree = "<group>"; }; D5538B9E287D6F9B000BDFB6 /* native-crypto.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "native-crypto.h"; sourceTree = "<group>"; };
ED18118323D6A9B500DED444 /* edtaa3func.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = edtaa3func.h; path = ../external/sources/edtaa3func/edtaa3func.h; sourceTree = "<group>"; }; ED18118323D6A9B500DED444 /* edtaa3func.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = edtaa3func.h; path = ../external/sources/edtaa3func/edtaa3func.h; sourceTree = "<group>"; };
@ -1373,11 +1377,13 @@
children = ( children = (
D5253E6F287D67D800DD257B /* crypto_scrypt.h */, D5253E6F287D67D800DD257B /* crypto_scrypt.h */,
D5253E70287D67D800DD257B /* sha256.h */, D5253E70287D67D800DD257B /* sha256.h */,
D5253E73287D67D800DD257B /* sha256.c */,
D53541252880633900372C0F /* base64.c */,
D53541242880633800372C0F /* base64.h */,
D5253E71287D67D800DD257B /* sysendian.h */, D5253E71287D67D800DD257B /* sysendian.h */,
D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */, D5538B9D287D6F9B000BDFB6 /* native-crypto.cpp */,
D5538B9E287D6F9B000BDFB6 /* native-crypto.h */, D5538B9E287D6F9B000BDFB6 /* native-crypto.h */,
D5253E72287D67D800DD257B /* readme.md */, D5253E72287D67D800DD257B /* readme.md */,
D5253E73287D67D800DD257B /* sha256.c */,
D5253E74287D67D800DD257B /* crypto_scrypt.c */, D5253E74287D67D800DD257B /* crypto_scrypt.c */,
); );
name = scrypt; name = scrypt;
@ -1578,6 +1584,7 @@
1A28FF5C1F20AFAB007A1D9D /* NSURLRequest+SRWebSocketPrivate.h in Headers */, 1A28FF5C1F20AFAB007A1D9D /* NSURLRequest+SRWebSocketPrivate.h in Headers */,
ED5A63FB236C384C007A0CF0 /* WebSocketServer.h in Headers */, ED5A63FB236C384C007A0CF0 /* WebSocketServer.h in Headers */,
1A28FF6E1F20AFAB007A1D9D /* SRError.h in Headers */, 1A28FF6E1F20AFAB007A1D9D /* SRError.h in Headers */,
D53541262880633900372C0F /* base64.h in Headers */,
46FDDBC4202ADDCE00931238 /* ccConfig.h in Headers */, 46FDDBC4202ADDCE00931238 /* ccConfig.h in Headers */,
46AE3FE02092F3A600F3A228 /* env.h in Headers */, 46AE3FE02092F3A600F3A228 /* env.h in Headers */,
469303A32046AE05004A3D6C /* Object.hpp in Headers */, 469303A32046AE05004A3D6C /* Object.hpp in Headers */,
@ -1691,6 +1698,7 @@
46FDDB8C202ADDCE00931238 /* CCAutoreleasePool.cpp in Sources */, 46FDDB8C202ADDCE00931238 /* CCAutoreleasePool.cpp in Sources */,
461786622052607E008256E1 /* jsb_socketio.cpp in Sources */, 461786622052607E008256E1 /* jsb_socketio.cpp in Sources */,
4693039F2046AE05004A3D6C /* Object.mm in Sources */, 4693039F2046AE05004A3D6C /* Object.mm in Sources */,
D53541272880633900372C0F /* base64.c in Sources */,
461786562052301A008256E1 /* CCScheduler.cpp in Sources */, 461786562052301A008256E1 /* CCScheduler.cpp in Sources */,
46FDDB8E202ADDCE00931238 /* ccTypes.cpp in Sources */, 46FDDB8E202ADDCE00931238 /* ccTypes.cpp in Sources */,
50ABBD4D1925AB0000A911A9 /* MathUtil.cpp in Sources */, 50ABBD4D1925AB0000A911A9 /* MathUtil.cpp in Sources */,

View File

@ -37,6 +37,7 @@
#include "sysendian.h" #include "sysendian.h"
#include "crypto_scrypt.h" #include "crypto_scrypt.h"
#include <openssl/rand.h>
static void blkcpy(uint8_t *, uint8_t *, size_t); static void blkcpy(uint8_t *, uint8_t *, size_t);
static void blkxor(uint8_t *, uint8_t *, size_t); static void blkxor(uint8_t *, uint8_t *, size_t);
@ -282,3 +283,35 @@ err0:
/* Failure! */ /* Failure! */
return (-1); 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;
}

View File

@ -43,5 +43,6 @@
int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t, int crypto_scrypt(const uint8_t *, size_t, const uint8_t *, size_t, uint64_t,
uint32_t, uint32_t, uint8_t *, size_t); uint32_t, uint32_t, uint8_t *, size_t);
int uuid_v4_gen(char *buffer);
#endif /* !_CRYPTO_SCRYPT_H_ */ #endif /* !_CRYPTO_SCRYPT_H_ */

View File

@ -6,22 +6,21 @@
*/ */
#include "native-crypto.h" #include "native-crypto.h"
extern "C" { extern "C" {
#include "crypto_scrypt.h" #include "crypto_scrypt.h"
#include "base64.h" #include "base64.h"
} }
#include <math.h> #include <cmath>
#include <stdlib.h> #include <cstdlib>
#include <string.h> #include <cstring>
#include <random>
#include <sstream>
#define COMPRESSED_PUBKEY_LENGTH 33 int fast_crypto_scrypt(const uint8_t *passwd, size_t passwdlen, const uint8_t *salt, size_t saltlen,
#define DECOMPRESSED_PUBKEY_LENGTH 65 uint64_t N,
#define PRIVKEY_LENGTH 64 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); return crypto_scrypt(passwd, passwdlen, salt, saltlen, N, r, p, buf, buflen);
} }
@ -37,7 +36,8 @@ int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N,
int passwordBufLen = Base64decode((char *) passwordBuf, szPassword); int passwordBufLen = Base64decode((char *) passwordBuf, szPassword);
int saltBufLen = Base64decode((char *) saltBuf, szSalt); int saltBufLen = Base64decode((char *) saltBuf, szSalt);
auto *buffer = (uint8_t *) malloc(sizeof(char) * size); 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); int b64encSize = Base64encode_len(size);
char *szB64Encoded = (char *) malloc(sizeof(char) * b64encSize); char *szB64Encoded = (char *) malloc(sizeof(char) * b64encSize);
@ -50,3 +50,35 @@ int crypto_scrypt_base64(std::string *password, std::string *salt, uint64_t N,
return result; return result;
} }
std::string get_uuid() {
static std::random_device dev;
static std::mt19937 rng(dev());
std::uniform_int_distribution<int> 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();
}
}

View File

@ -41,19 +41,14 @@
#include <stdint.h> #include <stdint.h>
#include <stdlib.h> #include <stdlib.h>
#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, 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); 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); uint32_t r, uint32_t p, size_t size, std::string *value);
#ifdef __cplusplus
}
#endif
std::string uuid_v4();
#endif // native_crypto_h #endif // native_crypto_h