summaryrefslogtreecommitdiff
path: root/core/ustring.cpp
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-07-02 03:06:52 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-07-02 12:36:27 +0200
commit564d93ff10b19dd15df6ea049bd7c9a9c99680c6 (patch)
treeee6523844280ff5874e8d7aff7c95f498ae769e0 /core/ustring.cpp
parent0268a4869ded42079d3f4c255406711c726e3df4 (diff)
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.
Diffstat (limited to 'core/ustring.cpp')
-rw-r--r--core/ustring.cpp34
1 files changed, 10 insertions, 24 deletions
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 18c48b4dad..2b312191e2 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -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;
}