Skip to content

Commit d062b27

Browse files
committed
Replace bundled hashlib with gcrypt
The commit also adds a minimalistic, idiomatic C++ wrapper for gcrypt, which can be used in projects that use libzsync2 (e.g., libappimageupdate).
1 parent e9b65bb commit d062b27

27 files changed

+138
-3207
lines changed

CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,13 @@ else()
8383
message(FATAL_ERROR "Unsupported platform: ${CMAKE_SYSTEM_NAME}")
8484
endif()
8585

86+
# doing this on project level allows all subdirs to use the imported targets
87+
find_package(PkgConfig)
88+
89+
# we used to ship a minimal, zlib-licensed hash lib as part of zsync2
90+
# now, we use a system provided crypto library for this purpose
91+
pkg_check_modules(libgcrypt REQUIRED IMPORTED_TARGET libgcrypt)
92+
8693
# add libraries shipped along with the project
8794
add_subdirectory(lib)
8895

cmake/zsync2Config.cmake.in

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
@PACKAGE_INIT@
22

3-
include ( "${CMAKE_CURRENT_LIST_DIR}/zsync2Targets.cmake" )
3+
# doing this on project level allows all subdirs to use the imported targets
4+
find_package(PkgConfig)
5+
6+
# we used to ship a minimal, zlib-licensed hash lib as part of zsync2
7+
# now, we use a system provided crypto library for this purpose
8+
pkg_check_modules(libgcrypt REQUIRED IMPORTED_TARGET libgcrypt)
9+
10+
include ("${CMAKE_CURRENT_LIST_DIR}/zsync2Targets.cmake")

include/zshash.h

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
#ifndef ZSYNC2_ZSHASH_H
2+
#define ZSYNC2_ZSHASH_H
3+
4+
// system headers
5+
#include <assert.h>
6+
#include <memory>
7+
#include <sstream>
8+
9+
// library headers
10+
#include <gcrypt.h>
11+
12+
// own headers
13+
#include "zsutil.h"
14+
15+
namespace zsync2 {
16+
/**
17+
* Minimal idiomatic C++ wrapper around libgcrypt's hashing library.
18+
*/
19+
template<gcry_md_algos _algorithm>
20+
class ZSyncHash {
21+
private:
22+
static void _assertGcryptNoError(gcry_error_t error) {
23+
if (error != 0) {
24+
throw std::runtime_error(std::to_string(error));
25+
}
26+
}
27+
28+
public:
29+
ZSyncHash() {
30+
_assertGcryptNoError(gcry_md_open(&_handle, _algorithm, 0));
31+
};
32+
33+
explicit ZSyncHash(const std::string& data) : ZSyncHash() {
34+
add(data);
35+
}
36+
37+
template<typename Vector>
38+
void add(const Vector& vector) {
39+
gcry_md_write(_handle, vector.data(), vector.size());
40+
}
41+
42+
// this function may be called only once, the result will not change once the digest has been calculated
43+
std::string getHash() {
44+
const auto* buffer = gcry_md_read(_handle, _algorithm);
45+
const auto len = gcry_md_get_algo_dlen(_algorithm);
46+
47+
assert(buffer != nullptr);
48+
49+
return bytesToHex(buffer, len);
50+
}
51+
52+
~ZSyncHash() noexcept {
53+
gcry_md_close(_handle);
54+
}
55+
56+
private:
57+
gcry_md_hd_t _handle{};
58+
};
59+
}
60+
61+
#endif //ZSYNC2_ZSHASH_H

include/zsutil.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,5 +41,5 @@ namespace zsync2 {
4141

4242
std::string base64Decode(const std::string& in);
4343

44-
std::string bytesToHex(unsigned char *data, int len);
44+
std::string bytesToHex(const unsigned char *data, size_t len);
4545
}

lib/CMakeLists.txt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
set(CMAKE_MINIMUM_REQUIRED_VERSION 3.2)
1+
# for pkg-config IMPORTED_TARGET
2+
cmake_minimum_required(VERSION 3.6)
23

34
include_directories(.)
45

@@ -25,6 +26,3 @@ target_include_directories(args INTERFACE args)
2526
if(NOT TARGET gtest)
2627
add_subdirectory(gtest EXCLUDE_FROM_ALL)
2728
endif()
28-
29-
# minimal zlib-licensed hash library
30-
add_subdirectory(hashlib)

lib/hashlib/CMakeLists.txt

Lines changed: 0 additions & 51 deletions
This file was deleted.

lib/hashlib/include/hashlib/crc32.h

Lines changed: 0 additions & 69 deletions
This file was deleted.

lib/hashlib/include/hashlib/hash.h

Lines changed: 0 additions & 28 deletions
This file was deleted.

lib/hashlib/include/hashlib/hmac.h

Lines changed: 0 additions & 83 deletions
This file was deleted.

0 commit comments

Comments
 (0)