Coverage Report

Created: 2026-09-28 10:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libreoffice/include/comphelper/hash.hxx
Line
Count
Source
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2
/*
3
 * This file is part of the LibreOffice project.
4
 *
5
 * This Source Code Form is subject to the terms of the Mozilla Public
6
 * License, v. 2.0. If a copy of the MPL was not distributed with this
7
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8
 */
9
10
#pragma once
11
12
#include <comphelper/comphelperdllapi.h>
13
#include <rtl/digest.h>
14
15
#include <memory>
16
#include <string>
17
#include <vector>
18
19
namespace rtl {
20
    class OUString;
21
}
22
23
namespace comphelper {
24
25
enum class HashType
26
{
27
    MD5,
28
    SHA1,
29
    SHA256,
30
    SHA384,
31
    SHA512
32
};
33
34
const sal_uInt32 MD5_HASH_LENGTH = RTL_DIGEST_LENGTH_MD5;
35
const sal_uInt32 SHA1_HASH_LENGTH = RTL_DIGEST_LENGTH_SHA1;
36
const sal_uInt32 SHA256_HASH_LENGTH = 32;
37
const sal_uInt32 SHA384_HASH_LENGTH = 48;
38
const sal_uInt32 SHA512_HASH_LENGTH = 64;
39
40
struct HashImpl;
41
42
COMPHELPER_DLLPUBLIC std::string hashToString(const std::vector<unsigned char>& rHash);
43
44
class COMPHELPER_DLLPUBLIC Hash
45
{
46
private:
47
    std::unique_ptr<HashImpl> mpImpl;
48
49
public:
50
51
    enum class IterCount
52
    {
53
        NONE,       /// Iteration count not added to hash iterations.
54
        PREPEND,    /// Iteration count prepended to hash iterations.
55
        APPEND      /// Iteration count appended to hash iterations.
56
    };
57
58
    Hash(HashType eType);
59
    ~Hash();
60
61
    void update(const void* pInput, size_t length);
62
63
    void update(std::vector<unsigned char> const& rInput)
64
0
    {
65
0
        update(rInput.data(), rInput.size());
66
0
    }
67
68
    void initialize();
69
70
    std::vector<unsigned char> finalize();
71
72
    static std::vector<unsigned char> calculateHash(const void* pInput, size_t length, HashType eType);
73
74
    /** Calculate hash value with salt (pSalt,nSaltLen) prepended to password
75
        (pInput,nLength) and repeated iterations run if nSpinCount>0.
76
77
        This implements the algorithms as specified in
78
        https://msdn.microsoft.com/en-us/library/dd920692 or
79
        https://msdn.microsoft.com/en-us/library/dd924776 and
80
        https://msdn.microsoft.com/en-us/library/dd925430
81
82
        @param  pSalt
83
                may be nullptr thus no salt prepended
84
85
        @param  nSpinCount
86
                If >0, repeat nSpinCount iterations. For each iteration, the
87
                previous iteration's result plus a 4 byte value (0-based,
88
                little endian) containing the number of the iteration prepended
89
                or appended to the hash value is the input for the next
90
                iteration.
91
92
        @param  eIterCount
93
                If IterCount::APPEND, append iteration count as per
94
                https://msdn.microsoft.com/en-us/library/dd920692
95
                If IterCount::PREPEND, prepend iteration count as per
96
                https://msdn.microsoft.com/en-us/library/dd924776 and
97
                https://msdn.microsoft.com/en-us/library/dd925430
98
                If IterCount::NONE, do not add the iteration count to hash
99
                iterations.
100
101
        @return the raw hash value
102
     */
103
    static std::vector<unsigned char> calculateHash(
104
            const void* pInput, size_t nLength,
105
            const void* pSalt, size_t nSaltLen,
106
            sal_uInt32 nSpinCount,
107
            IterCount eIterCount,
108
            HashType eType);
109
110
    /** Convenience function to calculate a salted hash with iterations.
111
112
        @param  rPassword
113
                UTF-16 encoded string, hashed byte-wise as unsigned char.
114
115
        @param  rSaltValue
116
                Salt that will be prepended to password data.
117
     */
118
    static std::vector<unsigned char> calculateHash(
119
            std::u16string_view rPassword,
120
            const std::vector<unsigned char>& rSaltValue,
121
            sal_uInt32 nSpinCount,
122
            IterCount eIterCount,
123
            HashType eType);
124
125
    size_t getLength() const;
126
};
127
128
}
129
130
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */