From 384954795c4f3c0641f4b0eb7cff6f27ebfbb5de Mon Sep 17 00:00:00 2001 From: Eike Rathke Date: Mon, 26 Feb 2018 12:14:03 +0100 Subject: Move convenience abstractions to DocPasswordHelper, tdf#104250 follow-up Change-Id: If0775ccf14b631918e51342a767412948e812c87 --- comphelper/qa/unit/test_hash.cxx | 3 +- comphelper/source/misc/docpasswordhelper.cxx | 48 ++++++++++++++++++++++++++++ comphelper/source/misc/hash.cxx | 46 -------------------------- 3 files changed, 50 insertions(+), 47 deletions(-) (limited to 'comphelper') diff --git a/comphelper/qa/unit/test_hash.cxx b/comphelper/qa/unit/test_hash.cxx index e15b906c190f..11dcbd0bd10f 100644 --- a/comphelper/qa/unit/test_hash.cxx +++ b/comphelper/qa/unit/test_hash.cxx @@ -8,6 +8,7 @@ */ #include +#include #include #include @@ -111,7 +112,7 @@ void TestHash::testSHA512_saltspin() const OUString aPass("pwd"); const OUString aAlgo("SHA-512"); const OUString aSalt("876MLoKTq42+/DLp415iZQ=="); - const OUString aHash = comphelper::Hash::calculateHashBase64( aPass, aSalt, 100000, aAlgo); + const OUString aHash = comphelper::DocPasswordHelper::GetOoxHashAsBase64( aPass, aSalt, 100000, aAlgo); const OUString aStr("5l3mgNHXpWiFaBPv5Yso1Xd/UifWvQWmlDnl/hsCYbFT2sJCzorjRmBCQ/3qeDu6Q/4+GIE8a1DsdaTwYh1q2g=="); CPPUNIT_ASSERT_EQUAL(aStr, aHash); } diff --git a/comphelper/source/misc/docpasswordhelper.cxx b/comphelper/source/misc/docpasswordhelper.cxx index 81e1e996816f..e6055b29cebc 100644 --- a/comphelper/source/misc/docpasswordhelper.cxx +++ b/comphelper/source/misc/docpasswordhelper.cxx @@ -23,6 +23,9 @@ #include #include +#include +#include +#include #include #include #include @@ -256,6 +259,51 @@ Sequence< sal_Int8 > DocPasswordHelper::GetXLHashAsSequence( } +css::uno::Sequence DocPasswordHelper::GetOoxHashAsSequence( + const rtl::OUString& rPassword, + const rtl::OUString& rSaltValue, + sal_uInt32 nSpinCount, + const rtl::OUString& rAlgorithmName) +{ + comphelper::HashType eType; + if (rAlgorithmName == "SHA-512") + eType = comphelper::HashType::SHA512; + else if (rAlgorithmName == "SHA-256") + eType = comphelper::HashType::SHA256; + else if (rAlgorithmName == "SHA-1") + eType = comphelper::HashType::SHA1; + else if (rAlgorithmName == "MD5") + eType = comphelper::HashType::MD5; + else + return css::uno::Sequence(); + + std::vector aSaltVec; + if (!rSaltValue.isEmpty()) + { + css::uno::Sequence aSaltSeq; + comphelper::Base64::decode( aSaltSeq, rSaltValue); + aSaltVec = comphelper::sequenceToContainer>( aSaltSeq); + } + + std::vector hash( comphelper::Hash::calculateHash( rPassword, aSaltVec, nSpinCount, eType)); + + return comphelper::containerToSequence( hash); +} + +OUString DocPasswordHelper::GetOoxHashAsBase64( + const rtl::OUString& rPassword, + const rtl::OUString& rSaltValue, + sal_uInt32 nSpinCount, + const rtl::OUString& rAlgorithmName) +{ + css::uno::Sequence aSeq( GetOoxHashAsSequence( rPassword, rSaltValue, nSpinCount, rAlgorithmName)); + + OUStringBuffer aBuf; + comphelper::Base64::encode( aBuf, aSeq); + return aBuf.makeStringAndClear(); +} + + /*static*/ uno::Sequence< sal_Int8 > DocPasswordHelper::GenerateRandomByteSequence( sal_Int32 nLength ) { uno::Sequence< sal_Int8 > aResult( nLength ); diff --git a/comphelper/source/misc/hash.cxx b/comphelper/source/misc/hash.cxx index 0a91536347b9..b629d8d17530 100644 --- a/comphelper/source/misc/hash.cxx +++ b/comphelper/source/misc/hash.cxx @@ -8,8 +8,6 @@ */ #include -#include -#include #include #include #include @@ -231,50 +229,6 @@ std::vector Hash::calculateHash( return calculateHash( pPassBytes, nPassBytesLen, rSaltValue.data(), rSaltValue.size(), nSpinCount, eType); } -css::uno::Sequence Hash::calculateHashSequence( - const rtl::OUString& rPassword, - const rtl::OUString& rSaltValue, - sal_uInt32 nSpinCount, - const rtl::OUString& rAlgorithmName) -{ - HashType eType; - if (rAlgorithmName == "SHA-512") - eType = HashType::SHA512; - else if (rAlgorithmName == "SHA-256") - eType = HashType::SHA256; - else if (rAlgorithmName == "SHA-1") - eType = HashType::SHA1; - else if (rAlgorithmName == "MD5") - eType = HashType::MD5; - else - return css::uno::Sequence(); - - std::vector aSaltVec; - if (!rSaltValue.isEmpty()) - { - css::uno::Sequence aSaltSeq; - comphelper::Base64::decode( aSaltSeq, rSaltValue); - aSaltVec = comphelper::sequenceToContainer>( aSaltSeq); - } - - std::vector hash( calculateHash( rPassword, aSaltVec, nSpinCount, eType)); - - return comphelper::containerToSequence( hash); -} - -OUString Hash::calculateHashBase64( - const rtl::OUString& rPassword, - const rtl::OUString& rSaltValue, - sal_uInt32 nSpinCount, - const rtl::OUString& rAlgorithmName) -{ - css::uno::Sequence aSeq( calculateHashSequence( rPassword, rSaltValue, nSpinCount, rAlgorithmName)); - - OUStringBuffer aBuf; - comphelper::Base64::encode( aBuf, aSeq); - return aBuf.makeStringAndClear(); -} - } /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit