LibreOffice Module package (master) 1
sha1context.cxx
Go to the documentation of this file.
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
28using namespace ::com::sun::star;
29
30// static
31uno::Reference<xml::crypto::XDigestContext> StarOfficeSHA1DigestContext::Create()
32{
34 xResult->m_pDigest = rtl_digest_createSHA1();
35 if ( !xResult->m_pDigest )
36 throw uno::RuntimeException("Can not create cipher!" );
37
38 return xResult;
39}
40
42{
43 if ( m_pDigest )
44 {
45 rtl_digest_destroySHA1( m_pDigest );
46 m_pDigest = nullptr;
47 }
48}
49
50void SAL_CALL StarOfficeSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& aData)
51{
52 std::scoped_lock aGuard( m_aMutex );
53 if ( !m_pDigest )
54 throw lang::DisposedException();
55
56 if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) )
57 {
58 rtl_digest_destroySHA1( m_pDigest );
59 m_pDigest = nullptr;
60
61 throw uno::RuntimeException();
62 }
63}
64
66{
67 std::scoped_lock aGuard( m_aMutex );
68 if ( !m_pDigest )
69 throw lang::DisposedException();
70
71 uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 );
72 if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) )
73 {
74 rtl_digest_destroySHA1( m_pDigest );
75 m_pDigest = nullptr;
76
77 throw uno::RuntimeException();
78 }
79
80 rtl_digest_destroySHA1( m_pDigest );
81 m_pDigest = nullptr;
82
83 return aResult;
84}
85
86uno::Reference<xml::crypto::XDigestContext> CorrectSHA1DigestContext::Create()
87{
88 return new CorrectSHA1DigestContext();
89}
90
92{
93}
94
96{
97}
98
99void SAL_CALL CorrectSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& rData)
100{
101 std::scoped_lock aGuard(m_Mutex);
102 if (m_bDisposed)
103 throw lang::DisposedException();
104
105 m_Hash.update(reinterpret_cast<unsigned char const*>(rData.getConstArray()), rData.getLength());
106}
107
108uno::Sequence<::sal_Int8> SAL_CALL CorrectSHA1DigestContext::finalizeDigestAndDispose()
109{
110 std::scoped_lock aGuard(m_Mutex);
111 if (m_bDisposed)
112 throw lang::DisposedException();
113
114 m_bDisposed = true;
115 std::vector<unsigned char> const sha1(m_Hash.finalize());
116 return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sha1.data()), sha1.size());
117}
118
119/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual ~CorrectSHA1DigestContext() override
Definition: sha1context.cxx:95
static css::uno::Reference< css::xml::crypto::XDigestContext > Create()
Definition: sha1context.cxx:86
virtual void SAL_CALL updateDigest(const css::uno::Sequence<::sal_Int8 > &rData) override
Definition: sha1context.cxx:99
::comphelper::Hash m_Hash
Definition: sha1context.hxx:53
virtual css::uno::Sequence<::sal_Int8 > SAL_CALL finalizeDigestAndDispose() override
virtual css::uno::Sequence< ::sal_Int8 > SAL_CALL finalizeDigestAndDispose() override
Definition: sha1context.cxx:65
static css::uno::Reference< css::xml::crypto::XDigestContext > Create()
Definition: sha1context.cxx:31
virtual ~StarOfficeSHA1DigestContext() override
Definition: sha1context.cxx:41
virtual void SAL_CALL updateDigest(const css::uno::Sequence< ::sal_Int8 > &aData) override
Definition: sha1context.cxx:50
std::vector< unsigned char > finalize()
void update(const unsigned char *pInput, size_t length)
constexpr OUStringLiteral aData
unsigned char sal_uInt8
signed char sal_Int8