/src/libreoffice/package/source/zipapi/sha1context.cxx
Line | Count | Source |
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 | | #include <sal/config.h> |
21 | | |
22 | | #include <com/sun/star/lang/DisposedException.hpp> |
23 | | #include <rtl/digest.h> |
24 | | #include <rtl/ref.hxx> |
25 | | |
26 | | #include "sha1context.hxx" |
27 | | |
28 | | using namespace ::com::sun::star; |
29 | | |
30 | | // static |
31 | | uno::Reference<xml::crypto::XDigestContext> StarOfficeSHA1DigestContext::Create() |
32 | 0 | { |
33 | 0 | ::rtl::Reference<StarOfficeSHA1DigestContext> xResult = new StarOfficeSHA1DigestContext(); |
34 | 0 | xResult->m_pDigest = rtl_digest_createSHA1(); |
35 | 0 | if ( !xResult->m_pDigest ) |
36 | 0 | throw uno::RuntimeException(u"Can not create cipher!"_ustr ); |
37 | | |
38 | 0 | return xResult; |
39 | 0 | } |
40 | | |
41 | | StarOfficeSHA1DigestContext::~StarOfficeSHA1DigestContext() |
42 | 0 | { |
43 | 0 | if ( m_pDigest ) |
44 | 0 | { |
45 | 0 | rtl_digest_destroySHA1( m_pDigest ); |
46 | 0 | m_pDigest = nullptr; |
47 | 0 | } |
48 | 0 | } |
49 | | |
50 | | void SAL_CALL StarOfficeSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& aData) |
51 | 0 | { |
52 | 0 | std::scoped_lock aGuard( m_aMutex ); |
53 | 0 | if ( !m_pDigest ) |
54 | 0 | throw lang::DisposedException(); |
55 | | |
56 | 0 | if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) ) |
57 | 0 | { |
58 | 0 | rtl_digest_destroySHA1( m_pDigest ); |
59 | 0 | m_pDigest = nullptr; |
60 | |
|
61 | 0 | throw uno::RuntimeException(); |
62 | 0 | } |
63 | 0 | } |
64 | | |
65 | | uno::Sequence<::sal_Int8> SAL_CALL StarOfficeSHA1DigestContext::finalizeDigestAndDispose() |
66 | 0 | { |
67 | 0 | std::scoped_lock aGuard( m_aMutex ); |
68 | 0 | if ( !m_pDigest ) |
69 | 0 | throw lang::DisposedException(); |
70 | | |
71 | 0 | uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 ); |
72 | 0 | if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) ) |
73 | 0 | { |
74 | 0 | rtl_digest_destroySHA1( m_pDigest ); |
75 | 0 | m_pDigest = nullptr; |
76 | |
|
77 | 0 | throw uno::RuntimeException(); |
78 | 0 | } |
79 | | |
80 | 0 | rtl_digest_destroySHA1( m_pDigest ); |
81 | 0 | m_pDigest = nullptr; |
82 | |
|
83 | 0 | return aResult; |
84 | 0 | } |
85 | | |
86 | | uno::Reference<xml::crypto::XDigestContext> CorrectSHA1DigestContext::Create() |
87 | 0 | { |
88 | 0 | return new CorrectSHA1DigestContext(); |
89 | 0 | } |
90 | | |
91 | | CorrectSHA1DigestContext::CorrectSHA1DigestContext() |
92 | 0 | { |
93 | 0 | } |
94 | | |
95 | | CorrectSHA1DigestContext::~CorrectSHA1DigestContext() |
96 | 0 | { |
97 | 0 | } |
98 | | |
99 | | void SAL_CALL CorrectSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& rData) |
100 | 0 | { |
101 | 0 | std::scoped_lock aGuard(m_Mutex); |
102 | 0 | if (m_bDisposed) |
103 | 0 | throw lang::DisposedException(); |
104 | | |
105 | 0 | m_Hash.update(rData.getConstArray(), rData.getLength()); |
106 | 0 | } |
107 | | |
108 | | uno::Sequence<::sal_Int8> SAL_CALL CorrectSHA1DigestContext::finalizeDigestAndDispose() |
109 | 0 | { |
110 | 0 | std::scoped_lock aGuard(m_Mutex); |
111 | 0 | if (m_bDisposed) |
112 | 0 | throw lang::DisposedException(); |
113 | | |
114 | 0 | m_bDisposed = true; |
115 | 0 | std::vector<unsigned char> const sha1(m_Hash.finalize()); |
116 | 0 | return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sha1.data()), sha1.size()); |
117 | 0 | } |
118 | | |
119 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |