nss: upgrade to release 3.73
[LibreOffice.git] / include / comphelper / hash.hxx
blob54ab3a25105cb85266f622d1d136a82b56a5dca3
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 SHA512
33 const sal_uInt32 MD5_HASH_LENGTH = RTL_DIGEST_LENGTH_MD5;
34 const sal_uInt32 SHA1_HASH_LENGTH = RTL_DIGEST_LENGTH_SHA1;
35 const sal_uInt32 SHA256_HASH_LENGTH = 32;
36 const sal_uInt32 SHA512_HASH_LENGTH = 64;
38 struct HashImpl;
40 class COMPHELPER_DLLPUBLIC Hash
42 private:
43 std::unique_ptr<HashImpl> mpImpl;
45 public:
47 enum class IterCount
49 NONE, /// Iteration count not added to hash iterations.
50 PREPEND, /// Iteration count prepended to hash iterations.
51 APPEND /// Iteration count appended to hash iterations.
54 Hash(HashType eType);
55 ~Hash();
57 void update(const unsigned char* pInput, size_t length);
59 std::vector<unsigned char> finalize();
61 static std::vector<unsigned char> calculateHash(const unsigned char* pInput, size_t length, HashType eType);
63 /** Calculate hash value with salt (pSalt,nSaltLen) prepended to password
64 (pInput,nLength) and repeated iterations run if nSpinCount>0.
66 This implements the algorithms as specified in
67 https://msdn.microsoft.com/en-us/library/dd920692 or
68 https://msdn.microsoft.com/en-us/library/dd924776 and
69 https://msdn.microsoft.com/en-us/library/dd925430
71 @param pSalt
72 may be nullptr thus no salt prepended
74 @param nSpinCount
75 If >0, repeat nSpinCount iterations. For each iteration, the
76 previous iteration's result plus a 4 byte value (0-based,
77 little endian) containing the number of the iteration prepended
78 or appended to the hash value is the input for the next
79 iteration.
81 @param eIterCount
82 If IterCount::APPEND, append iteration count as per
83 https://msdn.microsoft.com/en-us/library/dd920692
84 If IterCount::PREPEND, prepend iteration count as per
85 https://msdn.microsoft.com/en-us/library/dd924776 and
86 https://msdn.microsoft.com/en-us/library/dd925430
87 If IterCount::NONE, do not add the iteration count to hash
88 iterations.
90 @return the raw hash value
92 static std::vector<unsigned char> calculateHash(
93 const unsigned char* pInput, size_t nLength,
94 const unsigned char* pSalt, size_t nSaltLen,
95 sal_uInt32 nSpinCount,
96 IterCount eIterCount,
97 HashType eType);
99 /** Convenience function to calculate a salted hash with iterations.
101 @param rPassword
102 UTF-16 encoded string, hashed byte-wise as unsigned char.
104 @param rSaltValue
105 Salt that will be prepended to password data.
107 static std::vector<unsigned char> calculateHash(
108 const rtl::OUString& rPassword,
109 const std::vector<unsigned char>& rSaltValue,
110 sal_uInt32 nSpinCount,
111 IterCount eIterCount,
112 HashType eType);
114 size_t getLength() const;
119 #endif // INCLUDED_COMPHELPER_HASH_HXX
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */