From d58aa1e4eacff0e6086fe13ed435643b412d4c33 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 24 Sep 2023 16:56:31 +0800 Subject: [PATCH] docs: update README.md --- README.md | 123 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 88 insertions(+), 35 deletions(-) diff --git a/README.md b/README.md index e2118c5..f775cdb 100644 --- a/README.md +++ b/README.md @@ -1,45 +1,98 @@ -md5 -=== +# md5sum -Class to create MD5 checksum from file or string +> Class to create MD5 checksum from file or string. -Example +## Example + +First clone the source code. + +```bash +git clone https://github.com/dnomd343/md5sum.git +``` + +Then create `main.cc` and write the following demo code. ```c++ +#include +#include +#include "md5sum.h" + +using namespace md5; -#include "md5/md5.h" - -int main(int argc,char** argv){ - - char cstring[] = "Foo baz, testing."; - std::string str = cstring; - - /* MD5 from std::string */ - printf("md5sum: %s\n", md5( str ).c_str()); - - /* MD5 from c-string */ - printf("md5sum: %s\n", md5( cstring ).c_str()); - - /* Short MD5 from c-string */ - printf("md5sum6: %s\n", md5sum6( cstring ).c_str()); - - /* Short MD5 from std::string */ - printf("md5sum6: %s\n", md5sum6( str ).c_str()); - - /* MD5 from filename */ - printf("md5file: %s\n", md5file("README.md").c_str()); - - /* MD5 from opened file */ - std::FILE* file = std::fopen("README.md", "rb"); - printf("md5file: %s\n", md5file(file).c_str()); - std::fclose(file); - - /* we're done */ - return EXIT_SUCCESS; +int main() { + std::string foo = "hello world"; + + /* MD5 from std::string */ + std::cout << "md5sum: " << md5sum(foo) << std::endl; + + /* MD5 from c-string */ + std::cout << "md5sum: " << md5sum(foo.c_str(), foo.size()) << std::endl; + + /* MD5 from filename */ + std::cout << "md5file: " << md5file("md5sum/LICENSE") << std::endl; + + /* MD5 from opened file */ + std::FILE *file = std::fopen("md5sum/LICENSE", "rb"); + std::cout << "md5file: " << md5file(file) << std::endl; + std::fclose(file); } +``` +Compile them and run. + +```bash +> g++ -std=c++0x -I./md5sum -o demo main.cc md5sum/*.cc +> ./demo +md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 +md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 +md5file: 4ab93db71092d8b54266f83575c1b9e1 +md5file: 4ab93db71092d8b54266f83575c1b9e1 +``` + +A more elegant approach is to use cmake to compile, create `CMakeLists.txt` and write the following content. + +```cmake +cmake_minimum_required(VERSION 3.5) +project(demo LANGUAGES CXX) +set(CMAKE_CXX_STANDARD 11) + +include_directories(md5sum) +add_subdirectory(md5sum EXCLUDE_FROM_ALL) + +add_executable(demo main.cc) +target_link_libraries(demo md5sum) ``` -Compilation in g++ +Using the following command to build and run. + +```bash +> cmake -B ./cmake-build/ +-- The CXX compiler identification is AppleClang 15.0.0.15000040 +-- Detecting CXX compiler ABI info +-- Detecting CXX compiler ABI info - done +-- Check for working CXX compiler: /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++ - skipped +-- Detecting CXX compile features +-- Detecting CXX compile features - done +-- Configuring done (0.4s) +-- Generating done (0.0s) +-- Build files have been written to: ... +> cmake --build ./cmake-build/ +[ 20%] Building CXX object md5sum/CMakeFiles/md5sum.dir/md5.cc.o +[ 40%] Building CXX object md5sum/CMakeFiles/md5sum.dir/md5_impl.cc.o +[ 60%] Linking CXX static library libmd5sum.a +[ 60%] Built target md5sum +[ 80%] Building CXX object CMakeFiles/demo.dir/main.cc.o +[100%] Linking CXX executable demo +[100%] Built target demo +> ./cmake-build/demo +md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 +md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 +md5file: 4ab93db71092d8b54266f83575c1b9e1 +md5file: 4ab93db71092d8b54266f83575c1b9e1 +``` + +## License + +MIT ©2021 [@ulwanski](https://github.com/ulwanski) -g++ -std=c++0x -o md5 md5.cpp main.cpp +MIT ©2023 [@dnomd343](https://github.com/dnomd343)