xref: /aoo42x/main/svl/source/misc/PasswordHelper.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 
31 #include <svl/PasswordHelper.hxx>
32 #include <rtl/digest.h>
33 #include <tools/string.hxx>
34 
35 using namespace com::sun::star;
36 
37 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
38 {
39     rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
40 
41 	rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
42 	if (aError != rtl_Digest_E_None)
43 	{
44 	    rPassHash.realloc(0);
45 	}
46 }
47 
48 void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
49 {
50     xub_StrLen nSize(sPass.Len());
51     sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
52 
53     for (xub_StrLen i = 0; i < nSize; ++i)
54     {
55         sal_Unicode ch(sPass.GetChar(i));
56         pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
57         pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
58     }
59 
60     GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
61 
62     delete[] pCharBuffer;
63 }
64 
65 void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
66 {
67     xub_StrLen nSize(sPass.Len());
68     sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
69 
70     for (xub_StrLen i = 0; i < nSize; ++i)
71     {
72         sal_Unicode ch(sPass.GetChar(i));
73         pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
74         pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
75     }
76 
77     GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
78 
79     delete[] pCharBuffer;
80 }
81 
82 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
83 {
84     GetHashPasswordLittleEndian(rPassHash, sPass);
85 }
86 
87 bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const String& sNewPass)
88 {
89     bool bResult = false;
90 
91     uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
92     GetHashPasswordLittleEndian(aNewPass, sNewPass);
93     if (aNewPass == rOldPassHash)
94         bResult = true;
95     else
96     {
97         GetHashPasswordBigEndian(aNewPass, sNewPass);
98         bResult = (aNewPass == rOldPassHash);
99     }
100 
101     return bResult;
102 }
103 
104