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 <boost/scoped_array.hpp>
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 OUString& sPass)
39 : {
40 0 : sal_Int32 nSize(sPass.getLength());
41 0 : boost::scoped_array<sal_Char> pCharBuffer(new sal_Char[nSize * sizeof(sal_Unicode)]);
42 :
43 0 : for (sal_Int32 i = 0; i < nSize; ++i)
44 : {
45 0 : sal_Unicode ch(sPass[ 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.get(), nSize * sizeof(sal_Unicode));
51 0 : }
52 :
53 0 : void SvPasswordHelper::GetHashPasswordBigEndian(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
54 : {
55 0 : sal_Int32 nSize(sPass.getLength());
56 0 : boost::scoped_array<sal_Char> pCharBuffer(new sal_Char[nSize * sizeof(sal_Unicode)]);
57 :
58 0 : for (sal_Int32 i = 0; i < nSize; ++i)
59 : {
60 0 : sal_Unicode ch(sPass[ i ]);
61 0 : pCharBuffer[2 * i] = static_cast< sal_Char >(ch >> 8);
62 0 : pCharBuffer[2 * i + 1] = static_cast< sal_Char >(ch & 0xFF);
63 : }
64 :
65 0 : GetHashPassword(rPassHash, pCharBuffer.get(), nSize * sizeof(sal_Unicode));
66 0 : }
67 :
68 0 : void SvPasswordHelper::GetHashPassword(uno::Sequence<sal_Int8>& rPassHash, const OUString& sPass)
69 : {
70 0 : GetHashPasswordLittleEndian(rPassHash, sPass);
71 0 : }
72 :
73 0 : bool SvPasswordHelper::CompareHashPassword(const uno::Sequence<sal_Int8>& rOldPassHash, const OUString& sNewPass)
74 : {
75 0 : bool bResult = false;
76 :
77 0 : uno::Sequence<sal_Int8> aNewPass(RTL_DIGEST_LENGTH_SHA1);
78 0 : GetHashPasswordLittleEndian(aNewPass, sNewPass);
79 0 : if (aNewPass == rOldPassHash)
80 0 : bResult = true;
81 : else
82 : {
83 0 : GetHashPasswordBigEndian(aNewPass, sNewPass);
84 0 : bResult = (aNewPass == rOldPassHash);
85 : }
86 :
87 0 : return bResult;
88 : }
89 :
90 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|