summaryrefslogtreecommitdiffstats
path: root/include/comphelper/hash.hxx
blob: 52ad5e5cdf1e54020bdd9523b89be2844568f965 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 */

#ifndef INCLUDED_COMPHELPER_HASH_HXX
#define INCLUDED_COMPHELPER_HASH_HXX

#include <comphelper/comphelperdllapi.h>

#include <memory>
#include <vector>

namespace rtl {
    class OUString;
}

namespace comphelper {

enum class HashType
{
    MD5,
    SHA1,
    SHA256,
    SHA512
};

struct HashImpl;

class COMPHELPER_DLLPUBLIC Hash
{
private:
    std::unique_ptr<HashImpl> mpImpl;

public:

    enum class IterCount
    {
        NONE,       /// Iteration count not added to hash iterations.
        PREPEND,    /// Iteration count prepended to hash iterations.
        APPEND      /// Iteration count appended to hash iterations.
    };

    Hash(HashType eType);
    ~Hash();

    void update(const unsigned char* pInput, size_t length);

    std::vector<unsigned char> finalize();

    static std::vector<unsigned char> calculateHash(const unsigned char* pInput, size_t length, HashType eType);

    /** Calculate hash value with salt (pSalt,nSaltLen) prepended to password
        (pInput,nLength) and repeated iterations run if nSpinCount>0.

        This implements the algorithms as specified in
        https://msdn.microsoft.com/en-us/library/dd920692 or
        https://msdn.microsoft.com/en-us/library/dd924776 and
        https://msdn.microsoft.com/en-us/library/dd925430

        @param  pSalt
                may be nullptr thus no salt prepended

        @param  nSpinCount
                If >0, repeat nSpinCount iterations. For each iteration, the
                previous iteration's result plus a 4 byte value (0-based,
                little endian) containing the number of the iteration prepended
                or appended to the hash value is the input for the next
                iteration.

        @param  eIterCount
                If IterCount::APPEND, append iteration count as per
                https://msdn.microsoft.com/en-us/library/dd920692
                If IterCount::PREPEND, prepend iteration count as per
                https://msdn.microsoft.com/en-us/library/dd924776 and
                https://msdn.microsoft.com/en-us/library/dd925430
                If IterCount::NONE, do not add the iteration count to hash
                iterations.

        @return the raw hash value
     */
    static std::vector<unsigned char> calculateHash(
            const unsigned char* pInput, size_t nLength,
            const unsigned char* pSalt, size_t nSaltLen,
            sal_uInt32 nSpinCount,
            IterCount eIterCount,
            HashType eType);

    /** Convenience function to calculate a salted hash with iterations.

        @param  rPassword
                UTF-16 encoded string, hashed byte-wise as unsigned char.

        @param  rSaltValue
                Salt that will be prepended to password data.
     */
    static std::vector<unsigned char> calculateHash(
            const rtl::OUString& rPassword,
            const std::vector<unsigned char>& rSaltValue,
            sal_uInt32 nSpinCount,
            IterCount eIterCount,
            HashType eType);

    size_t getLength() const;
};

}

#endif // INCLUDED_COMPHELPER_HASH_HXX

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */