diff options
author | RĂ©mi Verschelde <remi@verschelde.fr> | 2022-02-14 12:15:10 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-02-14 12:15:10 +0100 |
commit | 6e73aa0497b9cfc1c28863e655991106e677e69c (patch) | |
tree | 15191afb3e9dcfe51a7d271e0120c03797293874 /core/io | |
parent | 8317753c24f6a05d4fdc8541fecf1cd2ec189b41 (diff) | |
parent | f4a80f9ca7dd7241d418db341aa97613bebd6b8d (diff) |
Merge pull request #57887 from Faless/crypto/4.x_os_get_entropy
Diffstat (limited to 'core/io')
-rw-r--r-- | core/io/resource_uid.cpp | 23 | ||||
-rw-r--r-- | core/io/resource_uid.h | 5 |
2 files changed, 12 insertions, 16 deletions
diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp index 776756e64e..d0335bed3a 100644 --- a/core/io/resource_uid.cpp +++ b/core/io/resource_uid.cpp @@ -31,7 +31,7 @@ #include "resource_uid.h" #include "core/config/project_settings.h" -#include "core/crypto/crypto.h" +#include "core/crypto/crypto_core.h" #include "core/io/dir_access.h" #include "core/io/file_access.h" @@ -82,20 +82,14 @@ ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const { return ID(uid & 0x7FFFFFFFFFFFFFFF); } -ResourceUID::ID ResourceUID::create_id() const { - mutex.lock(); - if (crypto.is_null()) { - crypto = Ref<Crypto>(Crypto::create()); - } - mutex.unlock(); +ResourceUID::ID ResourceUID::create_id() { while (true) { - PackedByteArray bytes = crypto->generate_random_bytes(8); - ERR_FAIL_COND_V(bytes.size() != 8, INVALID_ID); - const uint64_t *ptr64 = (const uint64_t *)bytes.ptr(); - ID id = int64_t((*ptr64) & 0x7FFFFFFFFFFFFFFF); - mutex.lock(); + ID id = INVALID_ID; + MutexLock lock(mutex); + Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id)); + ERR_FAIL_COND_V(err != OK, INVALID_ID); + id &= 0x7FFFFFFFFFFFFFFF; bool exists = unique_ids.has(id); - mutex.unlock(); if (!exists) { return id; } @@ -261,6 +255,9 @@ ResourceUID *ResourceUID::singleton = nullptr; ResourceUID::ResourceUID() { ERR_FAIL_COND(singleton != nullptr); singleton = this; + crypto = memnew(CryptoCore::RandomGenerator); + ((CryptoCore::RandomGenerator *)crypto)->init(); } ResourceUID::~ResourceUID() { + memdelete((CryptoCore::RandomGenerator *)crypto); } diff --git a/core/io/resource_uid.h b/core/io/resource_uid.h index 9f2ab5245b..1ea44b9d06 100644 --- a/core/io/resource_uid.h +++ b/core/io/resource_uid.h @@ -35,7 +35,6 @@ #include "core/string/string_name.h" #include "core/templates/ordered_hash_map.h" -class Crypto; class ResourceUID : public Object { GDCLASS(ResourceUID, Object) public: @@ -47,7 +46,7 @@ public: static String get_cache_file(); private: - mutable Ref<Crypto> crypto; + void *crypto; // CryptoCore::RandomGenerator (avoid including crypto_core.h) Mutex mutex; struct Cache { CharString cs; @@ -67,7 +66,7 @@ public: String id_to_text(ID p_id) const; ID text_to_id(const String &p_text) const; - ID create_id() const; + ID create_id(); bool has_id(ID p_id) const; void add_id(ID p_id, const String &p_path); void set_id(ID p_id, const String &p_path); |