Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
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/.
8 : : *
9 : : * This file incorporates work covered by the following license notice:
10 : : *
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 .
18 : : */
19 : :
20 : :
21 : : #include <svl/PasswordHelper.hxx>
22 : : #include <rtl/digest.h>
23 : : #include <tools/string.hxx>
24 : :
25 : : using namespace com::sun::star;
26 : :
27 : 0 : void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
28 : : {
29 : 0 : rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
30 : :
31 : 0 : rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
32 [ # # ]: 0 : if (aError != rtl_Digest_E_None)
33 : : {
34 : 0 : rPassHash.realloc(0);
35 : : }
36 : 0 : }
37 : :
38 : 0 : void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
39 : : {
40 : 0 : xub_StrLen nSize(sPass.Len());
41 : 0 : sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
42 : :
43 [ # # ]: 0 : for (xub_StrLen i = 0; i < nSize; ++i)
44 : : {
45 : 0 : sal_Unicode ch(sPass.GetChar(i));
46 : 0 : pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
47 : 0 : pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
48 : : }
49 : :
50 : 0 : GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
51 : :
52 [ # # ]: 0 : delete[] pCharBuffer;
53 : 0 : }
54 : :
55 : 0 : void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
56 : : {
57 : 0 : xub_StrLen nSize(sPass.Len());
58 : 0 : sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
59 : :
60 [ # # ]: 0 : for (xub_StrLen i = 0; i < nSize; ++i)
61 : : {
62 : 0 : sal_Unicode ch(sPass.GetChar(i));
63 : 0 : pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
64 : 0 : pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
65 : : }
66 : :
67 : 0 : GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
68 : :
69 [ # # ]: 0 : delete[] pCharBuffer;
70 : 0 : }
71 : :
72 : 0 : void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const String& sPass)
73 : : {
74 : 0 : GetHashPasswordLittleEndian(rPassHash, sPass);
75 : 0 : }
76 : :
77 : 0 : bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const String& sNewPass)
78 : : {
79 : 0 : bool bResult = false;
80 : :
81 [ # # ]: 0 : uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
82 [ # # ]: 0 : GetHashPasswordLittleEndian(aNewPass, sNewPass);
83 [ # # ][ # # ]: 0 : if (aNewPass == rOldPassHash)
84 : 0 : bResult = true;
85 : : else
86 : : {
87 [ # # ]: 0 : GetHashPasswordBigEndian(aNewPass, sNewPass);
88 [ # # ]: 0 : bResult = (aNewPass == rOldPassHash);
89 : : }
90 : :
91 [ # # ]: 0 : return bResult;
92 : : }
93 : :
94 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|