Browse Source

docs: update README.md

legacy
Dnomd343 1 year ago
parent
commit
d58aa1e4ea
  1. 103
      README.md

103
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.
<b>Example</b>
## Example
```c++
First clone the source code.
#include "md5/md5.h"
```bash
git clone https://github.com/dnomd343/md5sum.git
```
int main(int argc,char** argv){
Then create `main.cc` and write the following demo code.
char cstring[] = "Foo baz, testing.";
std::string str = cstring;
```c++
#include <string>
#include <iostream>
#include "md5sum.h"
/* MD5 from std::string */
printf("md5sum: %s\n", md5( str ).c_str());
using namespace md5;
/* MD5 from c-string */
printf("md5sum: %s\n", md5( cstring ).c_str());
int main() {
std::string foo = "hello world";
/* Short MD5 from c-string */
printf("md5sum6: %s\n", md5sum6( cstring ).c_str());
/* MD5 from std::string */
std::cout << "md5sum: " << md5sum(foo) << std::endl;
/* Short MD5 from std::string */
printf("md5sum6: %s\n", md5sum6( str ).c_str());
/* MD5 from c-string */
std::cout << "md5sum: " << md5sum(foo.c_str(), foo.size()) << std::endl;
/* MD5 from filename */
printf("md5file: %s\n", md5file("README.md").c_str());
std::cout << "md5file: " << md5file("md5sum/LICENSE") << std::endl;
/* MD5 from opened file */
std::FILE* file = std::fopen("README.md", "rb");
printf("md5file: %s\n", md5file(file).c_str());
std::FILE *file = std::fopen("md5sum/LICENSE", "rb");
std::cout << "md5file: " << md5file(file) << std::endl;
std::fclose(file);
/* we're done */
return EXIT_SUCCESS;
}
```
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
```
<b>Compilation in g++</b>
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)
```
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)
<i>g++ -std=c++0x -o md5 md5.cpp main.cpp</i>
MIT ©2023 [@dnomd343](https://github.com/dnomd343)

Loading…
Cancel
Save