Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / comphelper / hash.hxx
bloba3ad468d3eb5902b1d52087e77d1b87017761b7e
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_COMPHELPER_HASH_HXX
11 #define INCLUDED_COMPHELPER_HASH_HXX
13 #include <comphelper/comphelperdllapi.h>
14 #include <rtl/digest.h>
16 #include <memory>
17 #include <vector>
19 namespace rtl {
20 class OUString;
23 namespace comphelper {
25 enum class HashType
27 MD5,
28 SHA1,
29 SHA256,
30 SHA384,
31 SHA512
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;
40 struct HashImpl;
42 class COMPHELPER_DLLPUBLIC Hash
44 private:
45 std::unique_ptr<HashImpl> mpImpl;
47 public:
49 enum class IterCount
51 NONE, /// Iteration count not added to hash iterations.
52 PREPEND, /// Iteration count prepended to hash iterations.
53 APPEND /// Iteration count appended to hash iterations.
56 Hash(HashType eType);
57 ~Hash();
59 void update(const unsigned char* pInput, size_t length);
61 std::vector<unsigned char> finalize();
63 static std::vector<unsigned char> calculateHash(const unsigned char* pInput, size_t length, HashType eType);
65 /** Calculate hash value with salt (pSalt,nSaltLen) prepended to password
66 (pInput,nLength) and repeated iterations run if nSpinCount>0.
68 This implements the algorithms as specified in
69 https://msdn.microsoft.com/en-us/library/dd920692 or
70 https://msdn.microsoft.com/en-us/library/dd924776 and
71 https://msdn.microsoft.com/en-us/library/dd925430
73 @param pSalt
74 may be nullptr thus no salt prepended
76 @param nSpinCount
77 If >0, repeat nSpinCount iterations. For each iteration, the
78 previous iteration's result plus a 4 byte value (0-based,
79 little endian) containing the number of the iteration prepended
80 or appended to the hash value is the input for the next
81 iteration.
83 @param eIterCount
84 If IterCount::APPEND, append iteration count as per
85 https://msdn.microsoft.com/en-us/library/dd920692
86 If IterCount::PREPEND, prepend iteration count as per
87 https://msdn.microsoft.com/en-us/library/dd924776 and
88 https://msdn.microsoft.com/en-us/library/dd925430
89 If IterCount::NONE, do not add the iteration count to hash
90 iterations.
92 @return the raw hash value
94 static std::vector<unsigned char> calculateHash(
95 const unsigned char* pInput, size_t nLength,
96 const unsigned char* pSalt, size_t nSaltLen,
97 sal_uInt32 nSpinCount,
98 IterCount eIterCount,
99 HashType eType);
101 /** Convenience function to calculate a salted hash with iterations.
103 @param rPassword
104 UTF-16 encoded string, hashed byte-wise as unsigned char.
106 @param rSaltValue
107 Salt that will be prepended to password data.
109 static std::vector<unsigned char> calculateHash(
110 const rtl::OUString& rPassword,
111 const std::vector<unsigned char>& rSaltValue,
112 sal_uInt32 nSpinCount,
113 IterCount eIterCount,
114 HashType eType);
116 size_t getLength() const;
121 #endif // INCLUDED_COMPHELPER_HASH_HXX
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */