| 1 |
/* |
| 2 |
* Copyright (c) 2010 .SE (The Internet Infrastructure Foundation) |
| 3 |
* All rights reserved. |
| 4 |
* |
| 5 |
* Redistribution and use in source and binary forms, with or without |
| 6 |
* modification, are permitted provided that the following conditions |
| 7 |
* are met: |
| 8 |
* 1. Redistributions of source code must retain the above copyright |
| 9 |
* notice, this list of conditions and the following disclaimer. |
| 10 |
* 2. Redistributions in binary form must reproduce the above copyright |
| 11 |
* notice, this list of conditions and the following disclaimer in the |
| 12 |
* documentation and/or other materials provided with the distribution. |
| 13 |
* |
| 14 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 15 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 16 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 17 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY |
| 18 |
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 19 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 20 |
* GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 21 |
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER |
| 22 |
* IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
| 23 |
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN |
| 24 |
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 25 |
*/ |
| 26 |
|
| 27 |
/***************************************************************************** |
| 28 |
BotanHashAlgorithm.cpp |
| 29 |
|
| 30 |
Base class for Botan hash algorithm classes |
| 31 |
*****************************************************************************/ |
| 32 |
|
| 33 |
#include "config.h" |
| 34 |
#include "BotanHashAlgorithm.h" |
| 35 |
#include <botan/hash.h> |
| 36 |
|
| 37 |
// Base constructor |
| 38 |
BotanHashAlgorithm::BotanHashAlgorithm() |
| 39 |
{ |
| 40 |
hash = NULL; |
| 41 |
} |
| 42 |
|
| 43 |
// Destructor |
| 44 |
BotanHashAlgorithm::~BotanHashAlgorithm() |
| 45 |
{ |
| 46 |
delete hash; |
| 47 |
} |
| 48 |
|
| 49 |
// Hashing functions |
| 50 |
bool BotanHashAlgorithm::hashInit() |
| 51 |
{ |
| 52 |
if (!HashAlgorithm::hashInit()) |
| 53 |
{ |
| 54 |
return false; |
| 55 |
} |
| 56 |
|
| 57 |
// Initialize digesting |
| 58 |
try |
| 59 |
{ |
| 60 |
if (hash == NULL) |
| 61 |
{ |
| 62 |
hash = Botan::HashFunction::create_or_throw(getHashName()).release(); |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
hash->clear(); |
| 67 |
} |
| 68 |
} |
| 69 |
catch (...) |
| 70 |
{ |
| 71 |
ERROR_MSG("Failed to initialize the digesting token"); |
| 72 |
|
| 73 |
ByteString dummy; |
| 74 |
HashAlgorithm::hashFinal(dummy); |
| 75 |
|
| 76 |
return false; |
| 77 |
} |
| 78 |
|
| 79 |
return true; |
| 80 |
} |
| 81 |
|
| 82 |
bool BotanHashAlgorithm::hashUpdate(const ByteString& data) |
| 83 |
{ |
| 84 |
if (!HashAlgorithm::hashUpdate(data)) |
| 85 |
{ |
| 86 |
return false; |
| 87 |
} |
| 88 |
|
| 89 |
// Continue digesting |
| 90 |
try |
| 91 |
{ |
| 92 |
if (data.size() != 0) |
| 93 |
{ |
| 94 |
hash->update(data.const_byte_str(), data.size()); |
| 95 |
} |
| 96 |
} |
| 97 |
catch (...) |
| 98 |
{ |
| 99 |
ERROR_MSG("Failed to buffer data"); |
| 100 |
|
| 101 |
ByteString dummy; |
| 102 |
HashAlgorithm::hashFinal(dummy); |
| 103 |
|
| 104 |
return false; |
| 105 |
} |
| 106 |
|
| 107 |
return true; |
| 108 |
} |
| 109 |
|
| 110 |
bool BotanHashAlgorithm::hashFinal(ByteString& hashedData) |
| 111 |
{ |
| 112 |
if (!HashAlgorithm::hashFinal(hashedData)) |
| 113 |
{ |
| 114 |
return false; |
| 115 |
} |
| 116 |
|
| 117 |
// Resize |
| 118 |
hashedData.resize(hash->output_length()); |
| 119 |
|
| 120 |
// Read the digest |
| 121 |
try |
| 122 |
{ |
| 123 |
hash->final(&hashedData[0]); |
| 124 |
} |
| 125 |
catch (...) |
| 126 |
{ |
| 127 |
ERROR_MSG("Failed to digest the data"); |
| 128 |
|
| 129 |
return false; |
| 130 |
} |
| 131 |
|
| 132 |
return true; |
| 133 |
} |