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
{
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;
40 class COMPHELPER_DLLPUBLIC Hash
43 std::unique_ptr
<HashImpl
> mpImpl
;
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.
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
72 may be nullptr thus no salt prepended
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
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
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
,
99 /** Convenience function to calculate a salted hash with iterations.
102 UTF-16 encoded string, hashed byte-wise as unsigned char.
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
,
114 size_t getLength() const;
119 #endif // INCLUDED_COMPHELPER_HASH_HXX
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */