Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibCrypto/Hash/HashFunction.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2020, Ali Mohammad Pur <mpfard@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#pragma once
8
9
#include <AK/ByteBuffer.h>
10
#include <AK/Format.h>
11
#include <AK/StringView.h>
12
#include <AK/Types.h>
13
14
namespace Crypto::Hash {
15
16
template<size_t DigestS>
17
struct Digest {
18
    static_assert(DigestS % 8 == 0);
19
    constexpr static size_t Size = DigestS / 8;
20
    u8 data[Size];
21
22
0
    [[nodiscard]] ALWAYS_INLINE u8 const* immutable_data() const { return data; }
Unexecuted instantiation: Crypto::Hash::Digest<160ul>::immutable_data() const
Unexecuted instantiation: Crypto::Hash::Digest<384ul>::immutable_data() const
Unexecuted instantiation: Crypto::Hash::Digest<512ul>::immutable_data() const
Unexecuted instantiation: Crypto::Hash::Digest<128ul>::immutable_data() const
Unexecuted instantiation: Crypto::Hash::Digest<256ul>::immutable_data() const
23
0
    [[nodiscard]] ALWAYS_INLINE size_t data_length() const { return Size; }
Unexecuted instantiation: Crypto::Hash::Digest<160ul>::data_length() const
Unexecuted instantiation: Crypto::Hash::Digest<384ul>::data_length() const
Unexecuted instantiation: Crypto::Hash::Digest<512ul>::data_length() const
Unexecuted instantiation: Crypto::Hash::Digest<128ul>::data_length() const
Unexecuted instantiation: Crypto::Hash::Digest<256ul>::data_length() const
24
25
0
    [[nodiscard]] ALWAYS_INLINE ReadonlyBytes bytes() const { return { immutable_data(), data_length() }; }
Unexecuted instantiation: Crypto::Hash::Digest<160ul>::bytes() const
Unexecuted instantiation: Crypto::Hash::Digest<384ul>::bytes() const
Unexecuted instantiation: Crypto::Hash::Digest<512ul>::bytes() const
Unexecuted instantiation: Crypto::Hash::Digest<128ul>::bytes() const
Unexecuted instantiation: Crypto::Hash::Digest<256ul>::bytes() const
26
27
2.28k
    [[nodiscard]] bool operator==(Digest const& other) const = default;
28
410
    [[nodiscard]] bool operator!=(Digest const& other) const = default;
29
};
30
31
template<size_t BlockS, size_t DigestS, typename DigestT = Digest<DigestS>>
32
class HashFunction {
33
public:
34
    static_assert(BlockS % 8 == 0);
35
    static constexpr auto BlockSize = BlockS / 8;
36
37
    static_assert(DigestS % 8 == 0);
38
    static constexpr auto DigestSize = DigestS / 8;
39
40
    using DigestType = DigestT;
41
42
0
    constexpr static size_t block_size() { return BlockSize; }
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 512ul, Crypto::Hash::Digest<512ul> >::block_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 128ul, Crypto::Hash::Digest<128ul> >::block_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 160ul, Crypto::Hash::Digest<160ul> >::block_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 256ul, Crypto::Hash::Digest<256ul> >::block_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 384ul, Crypto::Hash::Digest<384ul> >::block_size()
43
0
    constexpr static size_t digest_size() { return DigestSize; }
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 512ul, Crypto::Hash::Digest<512ul> >::digest_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 128ul, Crypto::Hash::Digest<128ul> >::digest_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 160ul, Crypto::Hash::Digest<160ul> >::digest_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 256ul, Crypto::Hash::Digest<256ul> >::digest_size()
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 384ul, Crypto::Hash::Digest<384ul> >::digest_size()
44
45
    virtual void update(u8 const*, size_t) = 0;
46
47
    void update(Bytes buffer) { update(buffer.data(), buffer.size()); }
48
2.87k
    void update(ReadonlyBytes buffer) { update(buffer.data(), buffer.size()); }
Crypto::Hash::HashFunction<512ul, 128ul, Crypto::Hash::Digest<128ul> >::update(AK::Span<unsigned char const>)
Line
Count
Source
48
2.87k
    void update(ReadonlyBytes buffer) { update(buffer.data(), buffer.size()); }
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 160ul, Crypto::Hash::Digest<160ul> >::update(AK::Span<unsigned char const>)
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 256ul, Crypto::Hash::Digest<256ul> >::update(AK::Span<unsigned char const>)
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 384ul, Crypto::Hash::Digest<384ul> >::update(AK::Span<unsigned char const>)
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 512ul, Crypto::Hash::Digest<512ul> >::update(AK::Span<unsigned char const>)
49
0
    void update(ByteBuffer const& buffer) { update(buffer.data(), buffer.size()); }
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 512ul, Crypto::Hash::Digest<512ul> >::update(AK::Detail::ByteBuffer<32ul> const&)
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 160ul, Crypto::Hash::Digest<160ul> >::update(AK::Detail::ByteBuffer<32ul> const&)
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 384ul, Crypto::Hash::Digest<384ul> >::update(AK::Detail::ByteBuffer<32ul> const&)
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 128ul, Crypto::Hash::Digest<128ul> >::update(AK::Detail::ByteBuffer<32ul> const&)
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 256ul, Crypto::Hash::Digest<256ul> >::update(AK::Detail::ByteBuffer<32ul> const&)
Unexecuted instantiation: Crypto::Hash::HashFunction<0ul, 0ul, Crypto::Hash::MultiHashDigestVariant>::update(AK::Detail::ByteBuffer<32ul> const&)
50
0
    void update(StringView string) { update((u8 const*)string.characters_without_null_termination(), string.length()); }
51
52
    virtual DigestType peek() = 0;
53
    virtual DigestType digest() = 0;
54
55
    virtual void reset() = 0;
56
57
#ifndef KERNEL
58
    virtual ByteString class_name() const = 0;
59
#endif
60
61
protected:
62
0
    virtual ~HashFunction() = default;
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 128ul, Crypto::Hash::Digest<128ul> >::~HashFunction()
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 512ul, Crypto::Hash::Digest<512ul> >::~HashFunction()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 256ul, Crypto::Hash::Digest<256ul> >::~HashFunction()
Unexecuted instantiation: Crypto::Hash::HashFunction<1024ul, 384ul, Crypto::Hash::Digest<384ul> >::~HashFunction()
Unexecuted instantiation: Crypto::Hash::HashFunction<0ul, 0ul, Crypto::Hash::MultiHashDigestVariant>::~HashFunction()
Unexecuted instantiation: Crypto::Hash::HashFunction<512ul, 160ul, Crypto::Hash::Digest<160ul> >::~HashFunction()
63
};
64
}
65
66
template<size_t DigestS>
67
struct AK::Formatter<Crypto::Hash::Digest<DigestS>> : StandardFormatter {
68
    ErrorOr<void> format(FormatBuilder& builder, Crypto::Hash::Digest<DigestS> const& digest)
69
    {
70
        for (size_t i = 0; i < digest.Size; ++i) {
71
            if (i > 0 && i % 4 == 0)
72
                TRY(builder.put_padding('-', 1));
73
            TRY(builder.put_u64(digest.data[i], 16, false, false, true, false, FormatBuilder::Align::Right, 2));
74
        }
75
        return {};
76
    }
77
};