xref: /aoo41x/main/svl/source/misc/PasswordHelper.cxx (revision 40df464e)
1*40df464eSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*40df464eSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*40df464eSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*40df464eSAndrew Rist  * distributed with this work for additional information
6*40df464eSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*40df464eSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*40df464eSAndrew Rist  * "License"); you may not use this file except in compliance
9*40df464eSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*40df464eSAndrew Rist  *
11*40df464eSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*40df464eSAndrew Rist  *
13*40df464eSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*40df464eSAndrew Rist  * software distributed under the License is distributed on an
15*40df464eSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*40df464eSAndrew Rist  * KIND, either express or implied.  See the License for the
17*40df464eSAndrew Rist  * specific language governing permissions and limitations
18*40df464eSAndrew Rist  * under the License.
19*40df464eSAndrew Rist  *
20*40df464eSAndrew Rist  *************************************************************/
21*40df464eSAndrew Rist 
22*40df464eSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_svl.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <svl/PasswordHelper.hxx>
28cdf0e10cSrcweir #include <rtl/digest.h>
29cdf0e10cSrcweir #include <tools/string.hxx>
30cdf0e10cSrcweir 
31cdf0e10cSrcweir using namespace com::sun::star;
32cdf0e10cSrcweir 
GetHashPassword(uno::Sequence<sal_Int8> & rPassHash,const sal_Char * pPass,sal_uInt32 nLen)33cdf0e10cSrcweir void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
34cdf0e10cSrcweir {
35cdf0e10cSrcweir     rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
36cdf0e10cSrcweir 
37cdf0e10cSrcweir 	rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
38cdf0e10cSrcweir 	if (aError != rtl_Digest_E_None)
39cdf0e10cSrcweir 	{
40cdf0e10cSrcweir 	    rPassHash.realloc(0);
41cdf0e10cSrcweir 	}
42cdf0e10cSrcweir }
43cdf0e10cSrcweir 
GetHashPasswordLittleEndian(uno::Sequence<sal_Int8> & rPassHash,const String & sPass)44cdf0e10cSrcweir void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
45cdf0e10cSrcweir {
46cdf0e10cSrcweir     xub_StrLen nSize(sPass.Len());
47cdf0e10cSrcweir     sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     for (xub_StrLen i = 0; i < nSize; ++i)
50cdf0e10cSrcweir     {
51cdf0e10cSrcweir         sal_Unicode ch(sPass.GetChar(i));
52cdf0e10cSrcweir         pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
53cdf0e10cSrcweir         pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
54cdf0e10cSrcweir     }
55cdf0e10cSrcweir 
56cdf0e10cSrcweir     GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
57cdf0e10cSrcweir 
58cdf0e10cSrcweir     delete[] pCharBuffer;
59cdf0e10cSrcweir }
60cdf0e10cSrcweir 
GetHashPasswordBigEndian(uno::Sequence<sal_Int8> & rPassHash,const String & sPass)61cdf0e10cSrcweir void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     xub_StrLen nSize(sPass.Len());
64cdf0e10cSrcweir     sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
65cdf0e10cSrcweir 
66cdf0e10cSrcweir     for (xub_StrLen i = 0; i < nSize; ++i)
67cdf0e10cSrcweir     {
68cdf0e10cSrcweir         sal_Unicode ch(sPass.GetChar(i));
69cdf0e10cSrcweir         pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
70cdf0e10cSrcweir         pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
71cdf0e10cSrcweir     }
72cdf0e10cSrcweir 
73cdf0e10cSrcweir     GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
74cdf0e10cSrcweir 
75cdf0e10cSrcweir     delete[] pCharBuffer;
76cdf0e10cSrcweir }
77cdf0e10cSrcweir 
GetHashPassword(uno::Sequence<sal_Int8> & rPassHash,const String & sPass)78cdf0e10cSrcweir void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
79cdf0e10cSrcweir {
80cdf0e10cSrcweir     GetHashPasswordLittleEndian(rPassHash, sPass);
81cdf0e10cSrcweir }
82cdf0e10cSrcweir 
CompareHashPassword(const uno::Sequence<sal_Int8> & rOldPassHash,const String & sNewPass)83cdf0e10cSrcweir bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const String& sNewPass)
84cdf0e10cSrcweir {
85cdf0e10cSrcweir     bool bResult = false;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
88cdf0e10cSrcweir     GetHashPasswordLittleEndian(aNewPass, sNewPass);
89cdf0e10cSrcweir     if (aNewPass == rOldPassHash)
90cdf0e10cSrcweir         bResult = true;
91cdf0e10cSrcweir     else
92cdf0e10cSrcweir     {
93cdf0e10cSrcweir         GetHashPasswordBigEndian(aNewPass, sNewPass);
94cdf0e10cSrcweir         bResult = (aNewPass == rOldPassHash);
95cdf0e10cSrcweir     }
96cdf0e10cSrcweir 
97cdf0e10cSrcweir     return bResult;
98cdf0e10cSrcweir }
99cdf0e10cSrcweir 
100