mirror of https://github.com/dnomd343/md5sum.git
Dnomd343
1 year ago
1 changed files with 88 additions and 35 deletions
@ -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 |
||||
|
|
||||
|
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++ |
```c++ |
||||
|
#include <string> |
||||
|
#include <iostream> |
||||
|
#include "md5sum.h" |
||||
|
|
||||
|
using namespace md5; |
||||
|
|
||||
|
int main() { |
||||
|
std::string foo = "hello world"; |
||||
|
|
||||
#include "md5/md5.h" |
/* MD5 from std::string */ |
||||
|
std::cout << "md5sum: " << md5sum(foo) << std::endl; |
||||
|
|
||||
int main(int argc,char** argv){ |
/* MD5 from c-string */ |
||||
|
std::cout << "md5sum: " << md5sum(foo.c_str(), foo.size()) << std::endl; |
||||
|
|
||||
char cstring[] = "Foo baz, testing."; |
/* MD5 from filename */ |
||||
std::string str = cstring; |
std::cout << "md5file: " << md5file("md5sum/LICENSE") << std::endl; |
||||
|
|
||||
/* MD5 from std::string */ |
/* MD5 from opened file */ |
||||
printf("md5sum: %s\n", md5( str ).c_str()); |
std::FILE *file = std::fopen("md5sum/LICENSE", "rb"); |
||||
|
std::cout << "md5file: " << md5file(file) << std::endl; |
||||
|
std::fclose(file); |
||||
|
} |
||||
|
``` |
||||
|
|
||||
/* MD5 from c-string */ |
Compile them and run. |
||||
printf("md5sum: %s\n", md5( cstring ).c_str()); |
|
||||
|
|
||||
/* Short MD5 from c-string */ |
```bash |
||||
printf("md5sum6: %s\n", md5sum6( cstring ).c_str()); |
> g++ -std=c++0x -I./md5sum -o demo main.cc md5sum/*.cc |
||||
|
> ./demo |
||||
|
md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 |
||||
|
md5sum: 5eb63bbbe01eeed093cb22bb8f5acdc3 |
||||
|
md5file: 4ab93db71092d8b54266f83575c1b9e1 |
||||
|
md5file: 4ab93db71092d8b54266f83575c1b9e1 |
||||
|
``` |
||||
|
|
||||
/* Short MD5 from std::string */ |
A more elegant approach is to use cmake to compile, create `CMakeLists.txt` and write the following content. |
||||
printf("md5sum6: %s\n", md5sum6( str ).c_str()); |
|
||||
|
|
||||
/* MD5 from filename */ |
```cmake |
||||
printf("md5file: %s\n", md5file("README.md").c_str()); |
cmake_minimum_required(VERSION 3.5) |
||||
|
project(demo LANGUAGES CXX) |
||||
|
set(CMAKE_CXX_STANDARD 11) |
||||
|
|
||||
/* MD5 from opened file */ |
include_directories(md5sum) |
||||
std::FILE* file = std::fopen("README.md", "rb"); |
add_subdirectory(md5sum EXCLUDE_FROM_ALL) |
||||
printf("md5file: %s\n", md5file(file).c_str()); |
|
||||
std::fclose(file); |
|
||||
|
|
||||
/* we're done */ |
add_executable(demo main.cc) |
||||
return EXIT_SUCCESS; |
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 |
||||
``` |
``` |
||||
|
|
||||
<b>Compilation in g++</b> |
## 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…
Reference in new issue