libsidplayfp 3.0.0
hashlib Directory Reference

Files

 
config.hpp
 
core.hpp
 
md5.hpp

Detailed Description

[简体中文](README.zh-CN.md) | English

hashlib

[License](LICENSE) [Language]()

Cra3z' hashlib is a C++11 header-only hash algorithm library which like Python's hashlib.

supported algorithms:

Usage

Using headers

hashlib is a header-only library, so you can simply copy the headers in the directory include/hashlib to where you need it. However, it is recommended to embed hashlib as a subproject in your project, or install it first and then import it via CMake's find_package.

find_package(hashlib REQUIRED)
target_link_libraries(<your-target> hashlib::hashlib)

Single header

You can also generate a single header hashlib.hpp which combines all headers in the directory include/hashlib, see Generate single header.

Using C++20 module

hashlib supports C++20 module (need the standard module std); you can generate the module file, and then import it, see Generate C++20 module.

import std;
import hashlib;
int main() {
hashlib::md5 md5;
md5.update("hello world");
std::println("{}", md5.hexdigest());
}

Building & Installation

Requirements

mkdir <your-build-dir>
cd <your-build-dir>
cmake -S . -B <your-build-dir>
cmake --build <your-build-dir>
cmake --install <your-build-dir> --prefix <your-install-dir>

CMake Options

Option Default Description
HASHLIB_TESTS ON enable tests
HASHLIB_BUILD_SINGLE_HEADER OFF generate single header
HASHLIB_BUILD_MODULE OFF generate C++20 module

Generate single header

using the following command, the single header hashlib.hpp which combines all headers in the directory include/hashlib will be generated in your build directory.

cmake -S . -B <your-build-dir> -DHASHLIB_BUILD_SINGLE_HEADER=ON

Generate C++20 module

using the following command, the module file hashlib.cppm will be generated in your build directory.

cmake -S . -B <your-build-dir> -DHASHLIB_BUILD_MODULE=ON

using conan package manager

You can also install hashlib by conan package manager.

conan install --requires=hashlib/[*]

Examples

#include <iostream>
#include <hashlib/md5.hpp>
int main() {
hashlib::md5 md5{std::string("hello world")};
std::cout << md5.hexdigest() << '\n'; // 5eb63bbbe01eeed093cb22bb8f5acdc3
}
#include <iostream>
#include <hashlib/md5.hpp>
int main() {
{
hashlib::md5 md5;
md5.update(std::string("hello"));
md5.update(std::string(" world"));
std::cout << md5.hexdigest() << '\n'; // 5eb63bbbe01eeed093cb22bb8f5acdc3
}
{
hashlib::md5 md5;
md5 << std::string("hello") << std::string(" world");
std::cout << md5.hexdigest() << '\n'; // 5eb63bbbe01eeed093cb22bb8f5acdc3
}
}
#include <iostream>
#include <fstream>
#include <hashlib/sha1.hpp>
int main() {
std::ifstream file("example.txt", std::ios::in | std::ios::binary);
if (!file) {
std::cerr << "Failed to open file" << std::endl;
return EXIT_FAILURE;
}
hashlib::sha1 sha1;
sha1.update(
std::istreambuf_iterator<char>{file},
std::istreambuf_iterator<char>{}
);
std::cout << sha1.hexdigest() << '\n'; // output the sha1 digest of the file example.txt
}