merge the formfield patch from ooo-build
[ooovba.git] / svtools / source / misc1 / PasswordHelper.cxx
blobb06bcdd49b886292184f213d1cf498819150cc01
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: PasswordHelper.cxx,v $
10 * $Revision: 1.5 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_svtools.hxx"
35 #ifndef GCC
36 #endif
37 #include "PasswordHelper.hxx"
38 #include <rtl/digest.h>
39 #include <tools/string.hxx>
41 using namespace com::sun::star;
43 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
45 rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
47 rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
48 if (aError != rtl_Digest_E_None)
50 rPassHash.realloc(0);
54 void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
56 xub_StrLen nSize(sPass.Len());
57 sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
59 for (xub_StrLen i = 0; i < nSize; ++i)
61 sal_Unicode ch(sPass.GetChar(i));
62 pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
63 pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
66 GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
68 delete[] pCharBuffer;
71 void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
73 xub_StrLen nSize(sPass.Len());
74 sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
76 for (xub_StrLen i = 0; i < nSize; ++i)
78 sal_Unicode ch(sPass.GetChar(i));
79 pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
80 pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
83 GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
85 delete[] pCharBuffer;
88 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
90 GetHashPasswordLittleEndian(rPassHash, sPass);
93 bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const String& sNewPass)
95 bool bResult = false;
97 uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
98 GetHashPasswordLittleEndian(aNewPass, sNewPass);
99 if (aNewPass == rOldPassHash)
100 bResult = true;
101 else
103 GetHashPasswordBigEndian(aNewPass, sNewPass);
104 bResult = (aNewPass == rOldPassHash);
107 return bResult;