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