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 <comphelper/hash.hxx> 23 #include <com/sun/star/lang/DisposedException.hpp> 24 #include <rtl/digest.h> 25 #include <rtl/ref.hxx> 26 27 #include "sha1context.hxx" 28 29 using namespace ::com::sun::star; 30 31 // static Create()32uno::Reference<xml::crypto::XDigestContext> StarOfficeSHA1DigestContext::Create() 33 { 34 ::rtl::Reference<StarOfficeSHA1DigestContext> xResult = new StarOfficeSHA1DigestContext(); 35 xResult->m_pDigest = rtl_digest_createSHA1(); 36 if ( !xResult->m_pDigest ) 37 throw uno::RuntimeException("Can not create cipher!" ); 38 39 return uno::Reference< xml::crypto::XDigestContext >( xResult.get() ); 40 } 41 ~StarOfficeSHA1DigestContext()42StarOfficeSHA1DigestContext::~StarOfficeSHA1DigestContext() 43 { 44 if ( m_pDigest ) 45 { 46 rtl_digest_destroySHA1( m_pDigest ); 47 m_pDigest = nullptr; 48 } 49 } 50 updateDigest(const uno::Sequence<::sal_Int8> & aData)51void SAL_CALL StarOfficeSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& aData) 52 { 53 ::osl::MutexGuard aGuard( m_aMutex ); 54 if ( !m_pDigest ) 55 throw lang::DisposedException(); 56 57 if ( rtl_Digest_E_None != rtl_digest_updateSHA1( m_pDigest, aData.getConstArray(), aData.getLength() ) ) 58 { 59 rtl_digest_destroySHA1( m_pDigest ); 60 m_pDigest = nullptr; 61 62 throw uno::RuntimeException(); 63 } 64 } 65 finalizeDigestAndDispose()66uno::Sequence<::sal_Int8> SAL_CALL StarOfficeSHA1DigestContext::finalizeDigestAndDispose() 67 { 68 ::osl::MutexGuard aGuard( m_aMutex ); 69 if ( !m_pDigest ) 70 throw lang::DisposedException(); 71 72 uno::Sequence< sal_Int8 > aResult( RTL_DIGEST_LENGTH_SHA1 ); 73 if ( rtl_Digest_E_None != rtl_digest_getSHA1( m_pDigest, reinterpret_cast< sal_uInt8* >( aResult.getArray() ), aResult.getLength() ) ) 74 { 75 rtl_digest_destroySHA1( m_pDigest ); 76 m_pDigest = nullptr; 77 78 throw uno::RuntimeException(); 79 } 80 81 rtl_digest_destroySHA1( m_pDigest ); 82 m_pDigest = nullptr; 83 84 return aResult; 85 } 86 Create()87uno::Reference<xml::crypto::XDigestContext> CorrectSHA1DigestContext::Create() 88 { 89 return new CorrectSHA1DigestContext(); 90 } 91 92 struct CorrectSHA1DigestContext::Impl 93 { 94 ::osl::Mutex m_Mutex; 95 ::comphelper::Hash m_Hash{::comphelper::HashType::SHA1}; 96 bool m_bDisposed{false}; 97 }; 98 CorrectSHA1DigestContext()99CorrectSHA1DigestContext::CorrectSHA1DigestContext() 100 : m_pImpl(new Impl) 101 { 102 } 103 ~CorrectSHA1DigestContext()104CorrectSHA1DigestContext::~CorrectSHA1DigestContext() 105 { 106 } 107 updateDigest(const uno::Sequence<::sal_Int8> & rData)108void SAL_CALL CorrectSHA1DigestContext::updateDigest(const uno::Sequence<::sal_Int8>& rData) 109 { 110 ::osl::MutexGuard aGuard(m_pImpl->m_Mutex); 111 if (m_pImpl->m_bDisposed) 112 throw lang::DisposedException(); 113 114 m_pImpl->m_Hash.update(reinterpret_cast<unsigned char const*>(rData.getConstArray()), rData.getLength()); 115 } 116 finalizeDigestAndDispose()117uno::Sequence<::sal_Int8> SAL_CALL CorrectSHA1DigestContext::finalizeDigestAndDispose() 118 { 119 ::osl::MutexGuard aGuard(m_pImpl->m_Mutex); 120 if (m_pImpl->m_bDisposed) 121 throw lang::DisposedException(); 122 123 m_pImpl->m_bDisposed = true; 124 std::vector<unsigned char> const sha1(m_pImpl->m_Hash.finalize()); 125 return uno::Sequence<sal_Int8>(reinterpret_cast<sal_Int8 const*>(sha1.data()), sha1.size()); 126 } 127 128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ 129