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