mirror of
https://github.com/godotengine/godot.git
synced 2025-10-19 16:03:29 +00:00
CryptoCore class to access to base crypto utils.
Godot core needs MD5/SHA256/AES/Base64 which used to be provided by separate libraries. Since we bundle mbedtls in most cases, and we can easily only include the needed sources if we so desire, let's use it. To simplify library changes in the future, and better isolate header dependencies all functions have been wrapped around inside a class in `core/math/crypto_base.h`. If the mbedtls module is disabled, we only bundle the needed source files independently of the `builtin_mbedtls` option. If the module is enabled, the `builtin_mbedtls` option works as usual. Also remove some unused headers from StreamPeerMbedTLS which were causing build issues.
This commit is contained in:
parent
0268a4869d
commit
564d93ff10
26 changed files with 372 additions and 1380 deletions
|
@ -31,6 +31,7 @@
|
|||
#include "ustring.h"
|
||||
|
||||
#include "core/color.h"
|
||||
#include "core/math/crypto_core.h"
|
||||
#include "core/math/math_funcs.h"
|
||||
#include "core/os/memory.h"
|
||||
#include "core/print_string.h"
|
||||
|
@ -38,9 +39,6 @@
|
|||
#include "core/ucaps.h"
|
||||
#include "core/variant.h"
|
||||
|
||||
#include "thirdparty/misc/md5.h"
|
||||
#include "thirdparty/misc/sha256.h"
|
||||
|
||||
#include <wchar.h>
|
||||
|
||||
#ifndef NO_USE_STDLIB
|
||||
|
@ -2254,54 +2252,42 @@ uint64_t String::hash64() const {
|
|||
String String::md5_text() const {
|
||||
|
||||
CharString cs = utf8();
|
||||
MD5_CTX ctx;
|
||||
MD5Init(&ctx);
|
||||
MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
|
||||
MD5Final(&ctx);
|
||||
return String::md5(ctx.digest);
|
||||
unsigned char hash[16];
|
||||
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
|
||||
return String::hex_encode_buffer(hash, 16);
|
||||
}
|
||||
|
||||
String String::sha256_text() const {
|
||||
CharString cs = utf8();
|
||||
unsigned char hash[32];
|
||||
sha256_context ctx;
|
||||
sha256_init(&ctx);
|
||||
sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
|
||||
sha256_done(&ctx, hash);
|
||||
CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
|
||||
return String::hex_encode_buffer(hash, 32);
|
||||
}
|
||||
|
||||
Vector<uint8_t> String::md5_buffer() const {
|
||||
|
||||
CharString cs = utf8();
|
||||
MD5_CTX ctx;
|
||||
MD5Init(&ctx);
|
||||
MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
|
||||
MD5Final(&ctx);
|
||||
unsigned char hash[16];
|
||||
CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
|
||||
|
||||
Vector<uint8_t> ret;
|
||||
ret.resize(16);
|
||||
for (int i = 0; i < 16; i++) {
|
||||
ret.write[i] = ctx.digest[i];
|
||||
};
|
||||
|
||||
ret.write[i] = hash[i];
|
||||
}
|
||||
return ret;
|
||||
};
|
||||
|
||||
Vector<uint8_t> String::sha256_buffer() const {
|
||||
CharString cs = utf8();
|
||||
unsigned char hash[32];
|
||||
sha256_context ctx;
|
||||
sha256_init(&ctx);
|
||||
sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
|
||||
sha256_done(&ctx, hash);
|
||||
CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
|
||||
|
||||
Vector<uint8_t> ret;
|
||||
ret.resize(32);
|
||||
for (int i = 0; i < 32; i++) {
|
||||
ret.write[i] = hash[i];
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue