comphelper/qa/unit/test_hash.cxx | 4 ++-- comphelper/source/misc/hash.cxx | 33 +++++++++++++++++++++++---------- include/comphelper/hash.hxx | 27 ++++++++++++++++++++++++++- sc/inc/tabprotection.hxx | 6 ++++++ sc/source/core/data/tabprotection.cxx | 2 ++ 5 files changed, 59 insertions(+), 13 deletions(-)
New commits: commit 89182507aaf336d16ba1e89305bc2ea3b1e6949f Author: Eike Rathke <[email protected]> Date: Sat Feb 24 11:45:34 2018 +0100 Hash Base64, Sequence and cleansing, tdf#104250 prep Change-Id: I58d48b8caa780138b8431bec9db20c9d0e9abce7 diff --git a/comphelper/qa/unit/test_hash.cxx b/comphelper/qa/unit/test_hash.cxx index beec2c537cf4..e15b906c190f 100644 --- a/comphelper/qa/unit/test_hash.cxx +++ b/comphelper/qa/unit/test_hash.cxx @@ -111,8 +111,8 @@ void TestHash::testSHA512_saltspin() const OUString aPass("pwd"); const OUString aAlgo("SHA-512"); const OUString aSalt("876MLoKTq42+/DLp415iZQ=="); - const OUString aHash = comphelper::Hash::calculateHash( aPass, aSalt, 100000, aAlgo); - OUString aStr("5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="); + const OUString aHash = comphelper::Hash::calculateHashBase64( aPass, aSalt, 100000, aAlgo); + const OUString aStr("5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="); CPPUNIT_ASSERT_EQUAL(aStr, aHash); } diff --git a/comphelper/source/misc/hash.cxx b/comphelper/source/misc/hash.cxx index 7a8c3fecd7e9..0a91536347b9 100644 --- a/comphelper/source/misc/hash.cxx +++ b/comphelper/source/misc/hash.cxx @@ -168,7 +168,6 @@ std::vector<unsigned char> Hash::calculateHash( return calculateHash( pInput, nLength, eType); Hash aHash(eType); - std::vector<unsigned char> hash; if (nSaltLen) { std::vector<unsigned char> initialData( nSaltLen + nLength); @@ -181,7 +180,7 @@ std::vector<unsigned char> Hash::calculateHash( { aHash.update( pInput, nLength); } - hash = aHash.finalize(); + std::vector<unsigned char> hash( aHash.finalize()); if (nSpinCount) { @@ -232,7 +231,7 @@ std::vector<unsigned char> Hash::calculateHash( return calculateHash( pPassBytes, nPassBytesLen, rSaltValue.data(), rSaltValue.size(), nSpinCount, eType); } -OUString Hash::calculateHash( +css::uno::Sequence<sal_Int8> Hash::calculateHashSequence( const rtl::OUString& rPassword, const rtl::OUString& rSaltValue, sal_uInt32 nSpinCount, @@ -248,17 +247,31 @@ OUString Hash::calculateHash( else if (rAlgorithmName == "MD5") eType = HashType::MD5; else - return OUString(); + return css::uno::Sequence<sal_Int8>(); - css::uno::Sequence<sal_Int8> aSaltSeq; - comphelper::Base64::decode( aSaltSeq, rSaltValue); + std::vector<unsigned char> aSaltVec; + if (!rSaltValue.isEmpty()) + { + css::uno::Sequence<sal_Int8> aSaltSeq; + comphelper::Base64::decode( aSaltSeq, rSaltValue); + aSaltVec = comphelper::sequenceToContainer<std::vector<unsigned char>>( aSaltSeq); + } + + std::vector<unsigned char> hash( calculateHash( rPassword, aSaltVec, nSpinCount, eType)); + + return comphelper::containerToSequence<sal_Int8>( hash); +} - std::vector<unsigned char> hash = calculateHash( rPassword, - comphelper::sequenceToContainer<std::vector<unsigned char>>( aSaltSeq), - nSpinCount, eType); +OUString Hash::calculateHashBase64( + const rtl::OUString& rPassword, + const rtl::OUString& rSaltValue, + sal_uInt32 nSpinCount, + const rtl::OUString& rAlgorithmName) +{ + css::uno::Sequence<sal_Int8> aSeq( calculateHashSequence( rPassword, rSaltValue, nSpinCount, rAlgorithmName)); OUStringBuffer aBuf; - comphelper::Base64::encode( aBuf, comphelper::containerToSequence<sal_Int8>( hash)); + comphelper::Base64::encode( aBuf, aSeq); return aBuf.makeStringAndClear(); } diff --git a/include/comphelper/hash.hxx b/include/comphelper/hash.hxx index 07998ad02736..b3e97553060d 100644 --- a/include/comphelper/hash.hxx +++ b/include/comphelper/hash.hxx @@ -12,6 +12,8 @@ #include <comphelper/comphelperdllapi.h> +#include <com/sun/star/uno/Sequence.hxx> + #include <memory> #include <vector> @@ -97,10 +99,33 @@ public: that have a valid match in HashType. If not, an empty string is returned. Not all algorithm names are supported. + @return the raw hash value as sal_Int8 sequence. + */ + static css::uno::Sequence<sal_Int8> calculateHashSequence( + const rtl::OUString& rPassword, + const rtl::OUString& rSaltValue, + sal_uInt32 nSpinCount, + const rtl::OUString& rAlgorithmName); + + /** Convenience function to calculate a salted hash with iterations. + + @param rPassword + UTF-16LE encoded string without leading BOM character + + @param rSaltValue + Base64 encoded salt that will be decoded and prepended to password + data. + + @param rAlgorithmName + One of "SHA-512", "SHA-256", ... as listed in + https://msdn.microsoft.com/en-us/library/dd920692 + that have a valid match in HashType. If not, an empty string is + returned. Not all algorithm names are supported. + @return the base64 encoded string of the hash value, that can be compared against a stored base64 encoded hash value. */ - static rtl::OUString calculateHash( + static rtl::OUString calculateHashBase64( const rtl::OUString& rPassword, const rtl::OUString& rSaltValue, sal_uInt32 nSpinCount, commit 4446ac904ebaa0b2ba0fed17f2f8fd39d3a3c7fb Author: Eike Rathke <[email protected]> Date: Fri Feb 23 20:34:27 2018 +0100 Hold ScOoxPasswordHash at ScTableProtectionImpl, tdf#104250 prep Change-Id: I8a3e7e0a29058708ac0a2c5b78afc69bf7bf04a6 diff --git a/sc/source/core/data/tabprotection.cxx b/sc/source/core/data/tabprotection.cxx index 9a4a8bdde26d..9eefbfce4c97 100644 --- a/sc/source/core/data/tabprotection.cxx +++ b/sc/source/core/data/tabprotection.cxx @@ -132,6 +132,7 @@ private: bool mbProtected; ScPasswordHash meHash1; ScPasswordHash meHash2; + ScOoxPasswordHash maPasswordHash; ::std::vector< ScEnhancedProtection > maEnhancedProtection; }; @@ -198,6 +199,7 @@ ScTableProtectionImpl::ScTableProtectionImpl(const ScTableProtectionImpl& r) : mbProtected(r.mbProtected), meHash1(r.meHash1), meHash2(r.meHash2), + maPasswordHash(r.maPasswordHash), maEnhancedProtection(r.maEnhancedProtection) { } commit f1fb9932492249ec3f1546e79be8b3be7fb195f3 Author: Eike Rathke <[email protected]> Date: Fri Feb 23 20:31:18 2018 +0100 Add ScOoxPasswordHash::clear(), tdf#104250 prep Change-Id: Iaf3bb2ad74fe99435c74e9ea018168ff7e92a977 diff --git a/sc/inc/tabprotection.hxx b/sc/inc/tabprotection.hxx index 0b3a3ed62105..6a776807a191 100644 --- a/sc/inc/tabprotection.hxx +++ b/sc/inc/tabprotection.hxx @@ -49,6 +49,12 @@ struct ScOoxPasswordHash ScOoxPasswordHash() : mnSpinCount(0) {} bool hasPassword() const { return !maHashValue.isEmpty(); } + void clear() + { + // Keep algorithm and spin count. + maHashValue.clear(); + maSaltValue.clear(); + } }; namespace ScPassHashHelper _______________________________________________ Libreoffice-commits mailing list [email protected] https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits
