diff options
-rw-r--r-- | comphelper/source/misc/docpasswordhelper.cxx | 20 | ||||
-rw-r--r-- | include/comphelper/docpasswordhelper.hxx | 54 |
2 files changed, 67 insertions, 7 deletions
diff --git a/comphelper/source/misc/docpasswordhelper.cxx b/comphelper/source/misc/docpasswordhelper.cxx index 8a35c33ca92d..3f470520fbf5 100644 --- a/comphelper/source/misc/docpasswordhelper.cxx +++ b/comphelper/source/misc/docpasswordhelper.cxx @@ -259,9 +259,9 @@ Sequence< sal_Int8 > DocPasswordHelper::GetXLHashAsSequence( } -css::uno::Sequence<sal_Int8> DocPasswordHelper::GetOoxHashAsSequence( +std::vector<unsigned char> DocPasswordHelper::GetOoxHashAsVector( const rtl::OUString& rPassword, - const rtl::OUString& rSaltValue, + const std::vector<unsigned char>& rSaltValue, sal_uInt32 nSpinCount, comphelper::Hash::IterCount eIterCount, const rtl::OUString& rAlgorithmName) @@ -276,8 +276,19 @@ css::uno::Sequence<sal_Int8> DocPasswordHelper::GetOoxHashAsSequence( else if (rAlgorithmName == "MD5") eType = comphelper::HashType::MD5; else - return css::uno::Sequence<sal_Int8>(); + return std::vector<unsigned char>(); + + return comphelper::Hash::calculateHash( rPassword, rSaltValue, nSpinCount, eIterCount, eType); +} + +css::uno::Sequence<sal_Int8> DocPasswordHelper::GetOoxHashAsSequence( + const rtl::OUString& rPassword, + const rtl::OUString& rSaltValue, + sal_uInt32 nSpinCount, + comphelper::Hash::IterCount eIterCount, + const rtl::OUString& rAlgorithmName) +{ std::vector<unsigned char> aSaltVec; if (!rSaltValue.isEmpty()) { @@ -286,8 +297,7 @@ css::uno::Sequence<sal_Int8> DocPasswordHelper::GetOoxHashAsSequence( aSaltVec = comphelper::sequenceToContainer<std::vector<unsigned char>>( aSaltSeq); } - std::vector<unsigned char> hash( comphelper::Hash::calculateHash( rPassword, aSaltVec, nSpinCount, - eIterCount, eType)); + std::vector<unsigned char> hash( GetOoxHashAsVector( rPassword, aSaltVec, nSpinCount, eIterCount, rAlgorithmName)); return comphelper::containerToSequence<sal_Int8>( hash); } diff --git a/include/comphelper/docpasswordhelper.hxx b/include/comphelper/docpasswordhelper.hxx index e37a6c71de22..62c856c40c9a 100644 --- a/include/comphelper/docpasswordhelper.hxx +++ b/include/comphelper/docpasswordhelper.hxx @@ -183,7 +183,10 @@ public: /** Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the - OOXML sheetProtection and fileSharing elements. + OOXML sheetProtection and fileSharing elements, or + https://msdn.microsoft.com/en-us/library/dd924776 and + https://msdn.microsoft.com/en-us/library/dd925430 for Standard and + Agile Encryption. @param rPassword UTF-16 encoded string without leading BOM character @@ -225,7 +228,10 @@ public: /** Convenience function to calculate a salted hash with iterations as specified in https://msdn.microsoft.com/en-us/library/dd920692 for the - OOXML sheetProtection and fileSharing elements. + OOXML sheetProtection and fileSharing elements, or + https://msdn.microsoft.com/en-us/library/dd924776 and + https://msdn.microsoft.com/en-us/library/dd925430 for Standard and + Agile Encryption. @param rPassword UTF-16 encoded string without leading BOM character @@ -266,6 +272,50 @@ public: const rtl::OUString& rAlgorithmName); + /** Convenience function to calculate a salted hash with iterations as + specified in https://msdn.microsoft.com/en-us/library/dd920692 for the + OOXML sheetProtection and fileSharing elements, or + https://msdn.microsoft.com/en-us/library/dd924776 and + https://msdn.microsoft.com/en-us/library/dd925430 for Standard and + Agile Encryption. + + @param rPassword + UTF-16 encoded string without leading BOM character + + @param rSaltValue + A raw salt that will be prepended to password data. + + @param nSpinCount + If >0 the number of repeated iterations. + + @param eIterCount + If Hash::IterCount::APPEND, append iteration count as per + https://msdn.microsoft.com/en-us/library/dd920692 + If Hash::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 Hash::IterCount::NONE, do not add the iteration count to hash + iterations. + + @param rAlgorithmName + One of "SHA-512", "SHA-256", ... as listed for AlgorithmName in + https://msdn.microsoft.com/en-us/library/dd920692 + or "SHA512", "SHA256", ... as listed for HashAlgorithm in + https://msdn.microsoft.com/en-us/library/dd925810 + that have a valid match in comphelper::HashType. If not, an + empty sequence is returned. Not all algorithm names are + supported. + + @return the raw the hash value. + */ + static std::vector<unsigned char> GetOoxHashAsVector( + const rtl::OUString& rPassword, + const std::vector<unsigned char>& rSaltValue, + sal_uInt32 nSpinCount, + comphelper::Hash::IterCount eIterCount, + const rtl::OUString& rAlgorithmName); + + /** This helper function generates a random sequence of bytes of requested length. */ |