mirror of
https://github.com/lux-tel/libsession-util.git
synced 2026-10-30 20:25:11 +00:00
# Conflicts: # external/CMakeLists.txt # external/oxen-encoding # include/session/config.hpp # include/session/config/base.hpp # include/session/config/convo_info_volatile.hpp # include/session/config/protos.hpp # include/session/hash.hpp # include/session/session_encrypt.hpp # include/session/types.hpp # include/session/util.hpp # src/CMakeLists.txt # src/config/base.cpp # src/config/convo_info_volatile.cpp # src/config/encrypt.cpp # src/config/internal.cpp # src/config/internal.hpp # src/config/protos.cpp # src/hash.cpp # src/session_encrypt.cpp # tests/CMakeLists.txt # tests/swarm-auth-test.cpp # tests/test_blinding.cpp # tests/test_bugs.cpp # tests/test_config_contacts.cpp # tests/test_config_convo_info_volatile.cpp # tests/test_config_user_groups.cpp # tests/test_config_userprofile.cpp # tests/test_configdata.cpp # tests/test_group_info.cpp # tests/test_group_keys.cpp # tests/test_group_members.cpp # tests/test_multi_encrypt.cpp # tests/test_session_encrypt.cpp # tests/utils.hpp
61 lines
2.0 KiB
C++
61 lines
2.0 KiB
C++
#pragma once
|
|
|
|
#include <oxenc/common.h>
|
|
|
|
#include <array>
|
|
#include <optional>
|
|
#include <span>
|
|
#include <vector>
|
|
|
|
#include "types.hpp"
|
|
|
|
namespace session::hash {
|
|
|
|
/// API: hash/hash
|
|
///
|
|
/// Wrapper around the crypto_generichash_blake2b function for generating a hash that takes a span
|
|
/// to write the hash into. The size of the hash is determined from the size of the span.
|
|
///
|
|
/// Inputs:
|
|
/// - `hash` -- writable span in which to write the hash. The size of the span must be between 16
|
|
/// and 64.
|
|
/// - `msg` -- the message to generate a hash for.
|
|
/// - `key` -- an optional key to be used when generating the hash. Can be omitted or an empty
|
|
/// string for an unkeyed hash. Must be less than 64 bytes long.
|
|
void hash(
|
|
std::span<unsigned char> hash,
|
|
std::span<const unsigned char> msg,
|
|
std::optional<std::span<const unsigned char>> key = std::nullopt);
|
|
|
|
/// API: hash/hash
|
|
///
|
|
/// Wrapper around the crypto_generichash_blake2b function that returns a vector of the requested
|
|
/// size containing the hash.
|
|
///
|
|
/// Inputs:
|
|
/// - `size` -- length of the hash to be generated.
|
|
/// - `msg` -- the message to generate a hash for.
|
|
/// - `key` -- an optional key to be used when generating the hash. Can be omitted or an empty
|
|
/// string for an unkeyed hash.
|
|
///
|
|
/// Outputs:
|
|
/// - a `size` byte hash.
|
|
std::vector<unsigned char> hash(
|
|
const size_t size,
|
|
std::span<const unsigned char> msg,
|
|
std::optional<std::span<const unsigned char>> key = std::nullopt);
|
|
|
|
// Helper callable usable with unordered_map and similar to hash an array of chars by simply copying
|
|
// the first sizeof(size_t) bytes, suitable for use with pre-hashed values.
|
|
struct identity_hasher {
|
|
template <oxenc::basic_char Char, size_t N>
|
|
requires(N >= sizeof(size_t))
|
|
constexpr size_t operator()(const std::array<Char, N>& v) const noexcept {
|
|
size_t out;
|
|
std::copy(v.begin(), v.begin() + sizeof(out), reinterpret_cast<Char*>(&out));
|
|
return out;
|
|
}
|
|
};
|
|
|
|
} // namespace session::hash
|