xref: /AOO41X/main/package/source/zipapi/sha1context.cxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_package.hxx"
30 
31 #include <rtl/digest.h>
32 #include <rtl/ref.hxx>
33 
34 #include "sha1context.hxx"
35 
36 using namespace ::com::sun::star;
37 
38 // static
39 uno::Reference< xml::crypto::XDigestContext > SHA1DigestContext::Create()
40 {
41     ::rtl::Reference< SHA1DigestContext > xResult = new SHA1DigestContext();
42     xResult->m_pDigest = rtl_digest_createSHA1();
43     if ( !xResult->m_pDigest )
44         throw uno::RuntimeException( ::rtl::OUString::createFromAscii( "Can not create cipher!\n" ),
45                                      uno::Reference< XInterface >() );
46 
47     return uno::Reference< xml::crypto::XDigestContext >( xResult.get() );
48 }
49 
50 SHA1DigestContext::~SHA1DigestContext()
51 {
52     if ( m_pDigest )
53     {
54 		rtl_digest_destroySHA1( m_pDigest );
55         m_pDigest = NULL;
56     }
57 }
58 
59 void SAL_CALL SHA1DigestContext::updateDigest( const uno::Sequence< ::sal_Int8 >& aData )
60     throw( lang::DisposedException, uno::RuntimeException )
61 {
62     ::osl::MutexGuard aGuard( m_aMutex );
63     if ( !m_pDigest )
64         throw lang::DisposedException();
65 
66     if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) )
67     {
68 		rtl_digest_destroySHA1( m_pDigest );
69         m_pDigest = NULL;
70 
71         throw uno::RuntimeException();
72     }
73 }
74 
75 uno::Sequence< ::sal_Int8 > SAL_CALL SHA1DigestContext::finalizeDigestAndDispose()
76     throw( lang::DisposedException, uno::RuntimeException )
77 {
78     ::osl::MutexGuard aGuard( m_aMutex );
79     if ( !m_pDigest )
80         throw lang::DisposedException();
81 
82     uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 );
83     if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) )
84     {
85 		rtl_digest_destroySHA1( m_pDigest );
86         m_pDigest = NULL;
87 
88         throw uno::RuntimeException();
89     }
90 
91     rtl_digest_destroySHA1( m_pDigest );
92     m_pDigest = NULL;
93 
94     return aResult;
95 }
96 
97 
98