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 :
24 : using namespace com::sun::star;
25 :
26 0 : void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const sal_Char* pPass, sal_uInt32 nLen)
27 : {
28 0 : rPassHash.realloc(RTL_DIGEST_LENGTH_SHA1);
29 :
30 0 : rtlDigestError aError = rtl_digest_SHA1 (pPass, nLen, reinterpret_cast<sal_uInt8*>(rPassHash.getArray()), rPassHash.getLength());
31 0 : if (aError != rtl_Digest_E_None)
32 : {
33 0 : rPassHash.realloc(0);
34 : }
35 0 : }
36 :
37 0 : void SvPasswordHelper::GetHashPasswordLittleEndian(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
38 : {
39 0 : sal_Int32 nSize(sPass.getLength());
40 0 : sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
41 :
42 0 : for (sal_Int32 i = 0; i < nSize; ++i)
43 : {
44 0 : sal_Unicode ch(sPass[ i ]);
45 0 : pCharBuffer[2 * i] = static_cast< sal_Char >(ch & 0xFF);
46 0 : pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch >> 8);
47 : }
48 :
49 0 : GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
50 :
51 0 : delete[] pCharBuffer;
52 0 : }
53 :
54 0 : void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
55 : {
56 0 : sal_Int32 nSize(sPass.getLength());
57 0 : sal_Char* pCharBuffer = new sal_Char[nSize * sizeof(sal_Unicode)];
58 :
59 0 : for (sal_Int32 i = 0; i < nSize; ++i)
60 : {
61 0 : sal_Unicode ch(sPass[ i ]);
62 0 : pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
63 0 : pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
64 : }
65 :
66 0 : GetHashPassword(rPassHash, pCharBuffer, nSize * sizeof(sal_Unicode));
67 :
68 0 : delete[] pCharBuffer;
69 0 : }
70 :
71 0 : void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
72 : {
73 0 : GetHashPasswordLittleEndian(rPassHash, sPass);
74 0 : }
75 :
76 0 : bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const OUString& sNewPass)
77 : {
78 0 : bool bResult = false;
79 :
80 0 : uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
81 0 : GetHashPasswordLittleEndian(aNewPass, sNewPass);
82 0 : if (aNewPass == rOldPassHash)
83 0 : bResult = true;
84 : else
85 : {
86 0 : GetHashPasswordBigEndian(aNewPass, sNewPass);
87 0 : bResult = (aNewPass == rOldPassHash);
88 : }
89 :
90 0 : return bResult;
91 : }
92 :
93 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|