From e2c3bbabb0a12f58585bb441d91ee8882225b0ee Mon Sep 17 00:00:00 2001 From: Xavier Sellier Date: Mon, 10 Dec 2018 14:58:47 -0500 Subject: [PATCH] Feature: Add SHA256 for PoolByteArray --- core/variant_call.cpp | 15 +++++++++++++++ doc/classes/PoolByteArray.xml | 5 +++++ 2 files changed, 20 insertions(+) diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 0c6e43fe365..86b0b856e29 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -36,6 +36,7 @@ #include "core/object.h" #include "core/os/os.h" #include "core/script_language.h" +#include "thirdparty/misc/sha256.h" typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args); typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args); @@ -583,6 +584,19 @@ struct _VariantCall { r_ret = decompressed; } + static void _call_PoolByteArray_sha256_string(Variant &r_ret, Variant &p_self, const Variant **p_args) { + PoolByteArray *ba = reinterpret_cast(p_self._data._mem); + PoolByteArray::Read r = ba->read(); + String s; + unsigned char hash[32]; + sha256_context sha256; + sha256_init(&sha256); + sha256_hash(&sha256, (unsigned char *)r.ptr(), ba->size()); + sha256_done(&sha256, hash); + s = String::hex_encode_buffer(hash, 32); + r_ret = s; + } + VCALL_LOCALMEM0R(PoolByteArray, size); VCALL_LOCALMEM2(PoolByteArray, set); VCALL_LOCALMEM1R(PoolByteArray, get); @@ -1723,6 +1737,7 @@ void register_variant_methods() { ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_ascii, varray()); ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, get_string_from_utf8, varray()); + ADDFUNC0R(POOL_BYTE_ARRAY, STRING, PoolByteArray, sha256_string, varray()); ADDFUNC1R(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, compress, INT, "compression_mode", varray(0)); ADDFUNC2R(POOL_BYTE_ARRAY, POOL_BYTE_ARRAY, PoolByteArray, decompress, INT, "buffer_size", INT, "compression_mode", varray(0)); diff --git a/doc/classes/PoolByteArray.xml b/doc/classes/PoolByteArray.xml index ae722b10538..4d38f9f9b44 100644 --- a/doc/classes/PoolByteArray.xml +++ b/doc/classes/PoolByteArray.xml @@ -114,6 +114,11 @@ Change the byte at the given index. + + + Return SHA256 string of the PoolByteArray. + +