chaoyli closed pull request #420: Add Md5Digest to util
URL: https://github.com/apache/incubator-doris/pull/420
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/be/src/exprs/encryption_functions.cpp 
b/be/src/exprs/encryption_functions.cpp
index a1e856f6..3ca2fe03 100644
--- a/be/src/exprs/encryption_functions.cpp
+++ b/be/src/exprs/encryption_functions.cpp
@@ -17,8 +17,8 @@
 
 #include "exprs/encryption_functions.h"
 
-#include <openssl/md5.h>
 #include "util/aes_util.h"
+#include "util/md5.h"
 #include "exprs/anyval_util.h"
 #include "exprs/expr.h"
 #include "util/debug_util.h"
@@ -102,49 +102,26 @@ StringVal EncryptionFunctions::to_base64(FunctionContext* 
ctx, const StringVal &
 
 StringVal EncryptionFunctions::md5sum(
         FunctionContext* ctx, int num_args, const StringVal* args) {
-    MD5_CTX md5_ctx;
-    MD5_Init(&md5_ctx);
+    Md5Digest digest;
     for (int i = 0; i < num_args; ++i) {
         const StringVal& arg = args[i];
         if (arg.is_null) {
             continue;
         }
-        MD5_Update(&md5_ctx, arg.ptr, arg.len);
+        digest.update(arg.ptr, arg.len);
     }
-    unsigned char buf[MD5_DIGEST_LENGTH];
-    MD5_Final(buf, &md5_ctx);
-    unsigned char hex_buf[2 * MD5_DIGEST_LENGTH];
-
-    static char dig_vec_lower[] = "0123456789abcdef";
-    unsigned char* to = hex_buf;
-    for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-        *to++= dig_vec_lower[buf[i] >> 4];
-        *to++= dig_vec_lower[buf[i] & 0x0F];
-    }
-
-    return AnyValUtil::from_buffer_temp(ctx, (char*)hex_buf, 2 * 
MD5_DIGEST_LENGTH);
+    digest.digest();
+    return AnyValUtil::from_buffer_temp(ctx, digest.hex().c_str(), 
digest.hex().size());
 }
 
 StringVal EncryptionFunctions::md5(FunctionContext* ctx, const StringVal& src) 
{
     if (src.is_null) {
         return StringVal::null();
     }
-    MD5_CTX md5_ctx;
-    MD5_Init(&md5_ctx);
-    MD5_Update(&md5_ctx, src.ptr, src.len);
-
-    unsigned char buf[MD5_DIGEST_LENGTH];
-    MD5_Final(buf, &md5_ctx);
-    unsigned char hex_buf[2 * MD5_DIGEST_LENGTH];
-
-    static char dig_vec_lower[] = "0123456789abcdef";
-    unsigned char* to = hex_buf;
-    for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
-        *to++= dig_vec_lower[buf[i] >> 4];
-        *to++= dig_vec_lower[buf[i] & 0x0F];
-    }
-
-    return AnyValUtil::from_buffer_temp(ctx, (char*)hex_buf, 2 * 
MD5_DIGEST_LENGTH);
+    Md5Digest digest;
+    digest.update(src.ptr, src.len);
+    digest.digest();
+    return AnyValUtil::from_buffer_temp(ctx, digest.hex().c_str(), 
digest.hex().size());
 }
 
 }
diff --git a/be/src/util/CMakeLists.txt b/be/src/util/CMakeLists.txt
index 6b9ab136..7cb57c2f 100644
--- a/be/src/util/CMakeLists.txt
+++ b/be/src/util/CMakeLists.txt
@@ -72,6 +72,7 @@ add_library(Util STATIC
   uid_util.cpp
   aes_util.cpp
   string_util.cpp
+  md5.cpp
 )
 
 #ADD_BE_TEST(integer-array-test)
diff --git a/be/src/util/md5.cpp b/be/src/util/md5.cpp
new file mode 100644
index 00000000..81babde1
--- /dev/null
+++ b/be/src/util/md5.cpp
@@ -0,0 +1,45 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "util/md5.h"
+
+namespace doris {
+
+Md5Digest::Md5Digest() {
+    MD5_Init(&_md5_ctx);
+}
+
+void Md5Digest::update(const void* data, size_t length) {
+    MD5_Update(&_md5_ctx, data, length);
+}
+
+void Md5Digest::digest() {
+    unsigned char buf[MD5_DIGEST_LENGTH];
+    MD5_Final(buf, &_md5_ctx);
+
+    char hex_buf[2 * MD5_DIGEST_LENGTH];
+
+    static char dig_vec_lower[] = "0123456789abcdef";
+    char* to = hex_buf;
+    for (int i = 0; i < MD5_DIGEST_LENGTH; ++i) {
+        *to++= dig_vec_lower[buf[i] >> 4];
+        *to++= dig_vec_lower[buf[i] & 0x0F];
+    }
+    _hex.assign(hex_buf, 2 * MD5_DIGEST_LENGTH);
+}
+
+}
diff --git a/be/src/util/md5.h b/be/src/util/md5.h
new file mode 100644
index 00000000..43c67d32
--- /dev/null
+++ b/be/src/util/md5.h
@@ -0,0 +1,41 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#pragma once
+
+#include <string>
+
+#include <openssl/md5.h>
+
+namespace doris {
+
+class Md5Digest {
+public:
+    Md5Digest();
+
+    void update(const void* data, size_t length);
+    void digest();
+
+    const std::string& hex() const {
+        return _hex;
+    }
+private:
+    MD5_CTX _md5_ctx;
+    std::string _hex;
+};
+
+}
diff --git a/be/test/util/CMakeLists.txt b/be/test/util/CMakeLists.txt
index 91eda18e..0ac6e774 100644
--- a/be/test/util/CMakeLists.txt
+++ b/be/test/util/CMakeLists.txt
@@ -37,3 +37,4 @@ ADD_BE_TEST(byte_buffer_test2)
 ADD_BE_TEST(uid_util_test)
 ADD_BE_TEST(arena_test)
 ADD_BE_TEST(aes_util_test)
+ADD_BE_TEST(md5_test)
diff --git a/be/test/util/md5_test.cpp b/be/test/util/md5_test.cpp
new file mode 100644
index 00000000..1f11e2df
--- /dev/null
+++ b/be/test/util/md5_test.cpp
@@ -0,0 +1,48 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#include "util/md5.h"
+
+#include <gtest/gtest.h>
+
+namespace doris {
+
+class Md5Test : public testing::Test {
+public:
+    Md5Test() { }
+    virtual ~Md5Test() { }
+};
+
+TEST_F(Md5Test, empty) {
+    Md5Digest digest;
+    digest.digest();
+    ASSERT_STREQ("d41d8cd98f00b204e9800998ecf8427e", digest.hex().c_str());
+}
+
+TEST_F(Md5Test, normal) {
+    Md5Digest digest;
+    digest.update("abcdefg", 7);
+    digest.digest();
+    ASSERT_STREQ("7ac66c0f148de9519b8bd264312c4d64", digest.hex().c_str());
+}
+
+}
+
+int main(int argc, char* argv[]) {
+    ::testing::InitGoogleTest(&argc, argv);
+    return RUN_ALL_TESTS();
+}


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to