bump product version to 5.0.4.1
[LibreOffice.git] / svl / source / misc / PasswordHelper.cxx
blob43d320dfb8f17383ca0756d7ef05b99a6c33e0da
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
21 #include <svl/PasswordHelper.hxx>
22 #include <rtl/digest.h>
23 #include <boost/scoped_array.hpp>
25 using namespace com::sun::star;
27 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
29 rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
31 rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
32 if (aError != rtl_Digest_E_None)
34 rPassHash.realloc(0);
38 void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
40 sal_Int32 nSize(sPass.getLength());
41 boost::scoped_array<sal_Char> pCharBuffer(new sal_Char[nSize * sizeof(sal_Unicode)]);
43 for (sal_Int32 i = 0; i < nSize; ++i)
45 sal_Unicode ch(sPass[ i ]);
46 pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
47 pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
50 GetHashPassword(rPassHash, pCharBuffer.get(), nSize * sizeof(sal_Unicode));
53 void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
55 sal_Int32 nSize(sPass.getLength());
56 boost::scoped_array<sal_Char> pCharBuffer(new sal_Char[nSize * sizeof(sal_Unicode)]);
58 for (sal_Int32 i = 0; i < nSize; ++i)
60 sal_Unicode ch(sPass[ i ]);
61 pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
62 pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
65 GetHashPassword(rPassHash, pCharBuffer.get(), nSize * sizeof(sal_Unicode));
68 void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
70 GetHashPasswordLittleEndian(rPassHash, sPass);
73 bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const OUString& sNewPass)
75 bool bResult = false;
77 uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
78 GetHashPasswordLittleEndian(aNewPass, sNewPass);
79 if (aNewPass == rOldPassHash)
80 bResult = true;
81 else
83 GetHashPasswordBigEndian(aNewPass, sNewPass);
84 bResult = (aNewPass == rOldPassHash);
87 return bResult;
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */