1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <com/sun/star/lang/DisposedException.hpp>
23 #include <rtl/digest.h>
24 #include <rtl/ref.hxx>
26 #include "sha1context.hxx"
28 using namespace ::com::sun::star
;
31 uno::Reference
<xml::crypto::XDigestContext
> StarOfficeSHA1DigestContext::Create()
33 ::rtl::Reference
<StarOfficeSHA1DigestContext
> xResult
= new StarOfficeSHA1DigestContext();
34 xResult
->m_pDigest
= rtl_digest_createSHA1();
35 if ( !xResult
->m_pDigest
)
36 throw uno::RuntimeException(u
"Can not create cipher!"_ustr
);
41 StarOfficeSHA1DigestContext::~StarOfficeSHA1DigestContext()
45 rtl_digest_destroySHA1( m_pDigest
);
50 void SAL_CALL
StarOfficeSHA1DigestContext::updateDigest(const uno::Sequence
<::sal_Int8
>& aData
)
52 std::scoped_lock
aGuard( m_aMutex
);
54 throw lang::DisposedException();
56 if ( rtl_Digest_E_None
!= rtl_digest_updateSHA1( m_pDigest
, aData
.getConstArray(), aData
.getLength() ) )
58 rtl_digest_destroySHA1( m_pDigest
);
61 throw uno::RuntimeException();
65 uno::Sequence
<::sal_Int8
> SAL_CALL
StarOfficeSHA1DigestContext::finalizeDigestAndDispose()
67 std::scoped_lock
aGuard( m_aMutex
);
69 throw lang::DisposedException();
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() ) )
74 rtl_digest_destroySHA1( m_pDigest
);
77 throw uno::RuntimeException();
80 rtl_digest_destroySHA1( m_pDigest
);
86 uno::Reference
<xml::crypto::XDigestContext
> CorrectSHA1DigestContext::Create()
88 return new CorrectSHA1DigestContext();
91 CorrectSHA1DigestContext::CorrectSHA1DigestContext()
95 CorrectSHA1DigestContext::~CorrectSHA1DigestContext()
99 void SAL_CALL
CorrectSHA1DigestContext::updateDigest(const uno::Sequence
<::sal_Int8
>& rData
)
101 std::scoped_lock
aGuard(m_Mutex
);
103 throw lang::DisposedException();
105 m_Hash
.update(reinterpret_cast<unsigned char const*>(rData
.getConstArray()), rData
.getLength());
108 uno::Sequence
<::sal_Int8
> SAL_CALL
CorrectSHA1DigestContext::finalizeDigestAndDispose()
110 std::scoped_lock
aGuard(m_Mutex
);
112 throw lang::DisposedException();
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());
119 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */