xref: /AOO41X/main/package/source/zipapi/sha1context.cxx (revision d0626817eadca72252d341a27a0d42c4a6bf2287)
1*d0626817SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*d0626817SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*d0626817SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*d0626817SAndrew Rist  * distributed with this work for additional information
6*d0626817SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*d0626817SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*d0626817SAndrew Rist  * "License"); you may not use this file except in compliance
9*d0626817SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*d0626817SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*d0626817SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*d0626817SAndrew Rist  * software distributed under the License is distributed on an
15*d0626817SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*d0626817SAndrew Rist  * KIND, either express or implied.  See the License for the
17*d0626817SAndrew Rist  * specific language governing permissions and limitations
18*d0626817SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*d0626817SAndrew Rist  *************************************************************/
21*d0626817SAndrew Rist 
22*d0626817SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_package.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <rtl/digest.h>
28cdf0e10cSrcweir #include <rtl/ref.hxx>
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include "sha1context.hxx"
31cdf0e10cSrcweir 
32cdf0e10cSrcweir using namespace ::com::sun::star;
33cdf0e10cSrcweir 
34cdf0e10cSrcweir // static
Create()35cdf0e10cSrcweir uno::Reference< xml::crypto::XDigestContext > SHA1DigestContext::Create()
36cdf0e10cSrcweir {
37cdf0e10cSrcweir     ::rtl::Reference< SHA1DigestContext > xResult = new SHA1DigestContext();
38cdf0e10cSrcweir     xResult->m_pDigest = rtl_digest_createSHA1();
39cdf0e10cSrcweir     if ( !xResult->m_pDigest )
40cdf0e10cSrcweir         throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not create cipher!\n" ),
41cdf0e10cSrcweir                                      uno::Reference< XInterface >() );
42cdf0e10cSrcweir 
43cdf0e10cSrcweir     return uno::Reference< xml::crypto::XDigestContext >( xResult.get() );
44cdf0e10cSrcweir }
45cdf0e10cSrcweir 
~SHA1DigestContext()46cdf0e10cSrcweir SHA1DigestContext::~SHA1DigestContext()
47cdf0e10cSrcweir {
48cdf0e10cSrcweir     if ( m_pDigest )
49cdf0e10cSrcweir     {
50cdf0e10cSrcweir 		rtl_digest_destroySHA1( m_pDigest );
51cdf0e10cSrcweir         m_pDigest = NULL;
52cdf0e10cSrcweir     }
53cdf0e10cSrcweir }
54cdf0e10cSrcweir 
updateDigest(const uno::Sequence<::sal_Int8> & aData)55cdf0e10cSrcweir void SAL_CALL SHA1DigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
56cdf0e10cSrcweir     throw( lang::DisposedException, uno::RuntimeException )
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
59cdf0e10cSrcweir     if ( !m_pDigest )
60cdf0e10cSrcweir         throw lang::DisposedException();
61cdf0e10cSrcweir 
62cdf0e10cSrcweir     if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) )
63cdf0e10cSrcweir     {
64cdf0e10cSrcweir 		rtl_digest_destroySHA1( m_pDigest );
65cdf0e10cSrcweir         m_pDigest = NULL;
66cdf0e10cSrcweir 
67cdf0e10cSrcweir         throw uno::RuntimeException();
68cdf0e10cSrcweir     }
69cdf0e10cSrcweir }
70cdf0e10cSrcweir 
finalizeDigestAndDispose()71cdf0e10cSrcweir uno::Sequence< ::sal_Int8 > SAL_CALL SHA1DigestContext::finalizeDigestAndDispose()
72cdf0e10cSrcweir     throw( lang::DisposedException, uno::RuntimeException )
73cdf0e10cSrcweir {
74cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
75cdf0e10cSrcweir     if ( !m_pDigest )
76cdf0e10cSrcweir         throw lang::DisposedException();
77cdf0e10cSrcweir 
78cdf0e10cSrcweir     uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 );
79cdf0e10cSrcweir     if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) )
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir 		rtl_digest_destroySHA1( m_pDigest );
82cdf0e10cSrcweir         m_pDigest = NULL;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         throw uno::RuntimeException();
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
87cdf0e10cSrcweir     rtl_digest_destroySHA1( m_pDigest );
88cdf0e10cSrcweir     m_pDigest = NULL;
89cdf0e10cSrcweir 
90cdf0e10cSrcweir     return aResult;
91cdf0e10cSrcweir }
92cdf0e10cSrcweir 
93cdf0e10cSrcweir 
94