From 98b02209a438ddd4358eda58870100287c3924d1 Mon Sep 17 00:00:00 2001 From: George Marques Date: Thu, 23 Jun 2016 13:57:45 -0300 Subject: [PATCH] Add sha256_buffer() function to String --- core/ustring.cpp | 17 +++++++++++++++++ core/ustring.h | 1 + core/variant_call.cpp | 2 ++ 3 files changed, 20 insertions(+) diff --git a/core/ustring.cpp b/core/ustring.cpp index ea9a9d903e5..43e3a9a0bdd 100644 --- a/core/ustring.cpp +++ b/core/ustring.cpp @@ -2419,6 +2419,23 @@ Vector String::md5_buffer() const { return ret; }; +Vector 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); + + Vector ret; + ret.resize(32); + for (int i = 0; i < 32; i++) { + ret[i] = hash[i]; + } + + return ret; +} + String String::insert(int p_at_pos,String p_string) const { diff --git a/core/ustring.h b/core/ustring.h index 692cb4e37d4..bb57b11d882 100644 --- a/core/ustring.h +++ b/core/ustring.h @@ -196,6 +196,7 @@ public: String md5_text() const; String sha256_text() const; Vector md5_buffer() const; + Vector sha256_buffer() const; inline bool empty() const { return length() == 0; } diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 34e5164b242..c0f8930eedf 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -271,6 +271,7 @@ static void _call_##m_type##_##m_method(Variant& r_ret,Variant& p_self,const Var VCALL_LOCALMEM0R(String,md5_text); VCALL_LOCALMEM0R(String,sha256_text); VCALL_LOCALMEM0R(String,md5_buffer); + VCALL_LOCALMEM0R(String,sha256_buffer); VCALL_LOCALMEM0R(String,empty); VCALL_LOCALMEM0R(String,is_abs_path); VCALL_LOCALMEM0R(String,is_rel_path); @@ -1322,6 +1323,7 @@ _VariantCall::addfunc(Variant::m_vtype,Variant::m_ret,_SCS(#m_method),VCALL(m_cl ADDFUNC0(STRING,STRING,String,md5_text,varray()); ADDFUNC0(STRING,STRING,String,sha256_text,varray()); ADDFUNC0(STRING,RAW_ARRAY,String,md5_buffer,varray()); + ADDFUNC0(STRING,RAW_ARRAY,String,sha256_buffer,varray()); ADDFUNC0(STRING,BOOL,String,empty,varray()); ADDFUNC0(STRING,BOOL,String,is_abs_path,varray()); ADDFUNC0(STRING,BOOL,String,is_rel_path,varray());