Browse Source

Merge pull request #24269 from xsellier/feature/master-add-sha256

Add SHA256 for PoolByteArray
Hein-Pieter van Braam 6 years ago
parent
commit
f2d3d3e679
2 changed files with 20 additions and 0 deletions
  1. 15 0
      core/variant_call.cpp
  2. 5 0
      doc/classes/PoolByteArray.xml

+ 15 - 0
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);
@@ -587,6 +588,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<PoolByteArray *>(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);
@@ -1733,6 +1747,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));
 

+ 5 - 0
doc/classes/PoolByteArray.xml

@@ -112,6 +112,11 @@
 				Change the byte at the given index.
 			</description>
 		</method>
+		<method name="sha256_string">
+			<description>
+				Return SHA256 string of the PoolByteArray.
+			</description>
+		</method>
 		<method name="size">
 			<return type="int">
 			</return>