1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef INCLUDED_COMPHELPER_HASH_HXX
11 #define INCLUDED_COMPHELPER_HASH_HXX
13 #include <comphelper/comphelperdllapi.h>
14 #include <rtl/digest.h>
23 namespace comphelper
{
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;
42 class COMPHELPER_DLLPUBLIC Hash
45 std::unique_ptr
<HashImpl
> mpImpl
;
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.
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
74 may be nullptr thus no salt prepended
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
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
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
,
101 /** Convenience function to calculate a salted hash with iterations.
104 UTF-16 encoded string, hashed byte-wise as unsigned char.
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
,
116 size_t getLength() const;
121 #endif // INCLUDED_COMPHELPER_HASH_HXX
123 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */