File: MHashPP.cc

package info (click to toggle)
steghide 0.5.1-14
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 3,432 kB
  • sloc: cpp: 13,302; ansic: 6,184; sh: 3,960; makefile: 384; yacc: 316; perl: 228; sed: 16
file content (145 lines) | stat: -rwxr-xr-x 3,062 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/*
 * steghide 0.5.1 - a steganography program
 * Copyright (C) 1999-2003 Stefan Hetzl <shetzl@chello.at>
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 */

#include <cstdlib>
#include <string>

#include <mhash.h>

#include "BitString.h"
#include "common.h"
#include "error.h"
#include "MHashPP.h"

MHashPP::MHashPP ()
{
	hashing = false ;
	HashBytesValid = false ;
}

MHashPP::MHashPP (hashid id)
{
	init (id) ;
}

void MHashPP::init (hashid id)
{
	if ((HashD = mhash_init (id)) == MHASH_FAILED) {
		throw SteghideError (_("could not initialize libmhash %s algorithm."), getAlgorithmName(id).c_str()) ;
	}
	hashing = true ;
	HashBytesValid = false ;
}

const std::vector<BYTE>& MHashPP::end ()
{
	myassert (hashing) ;

	unsigned int n = getHashSize() ;
	HashBytes = std::vector<BYTE> (n) ;
	BYTE hash[n] ;
	mhash_deinit (HashD, hash) ;
	hashing = false ;
	for (unsigned int i = 0 ; i < n ; i++) {
		HashBytes[i] = hash[i] ;
	}

	HashBytesValid = true ;
	return HashBytes ;
}

unsigned int MHashPP::getHashSize (void)
{
	myassert (hashing) ;
	return ((unsigned int) mhash_get_block_size (mhash_get_mhash_algo (HashD))) ;
}

MHashPP& MHashPP::operator<< (std::string v)
{
	myassert (hashing) ;
	mhash (HashD, v.data(), v.size()) ;
	return *this ;
}

MHashPP& MHashPP::operator<< (BitString v)
{
	myassert (hashing) ;
	myassert (v.getLength() % 8 == 0) ;

	unsigned long n = v.getLength() / 8 ;
	for (unsigned int i = 0 ; i < n ; i++) {
		(*this) << (BYTE) v.getValue (8 * i, 8) ;
	}

	return *this ;
}

MHashPP& MHashPP::operator<< (BYTE v)
{
	myassert (hashing) ;
	mhash (HashD, &v, 1) ;
	return *this ;
}

MHashPP& MHashPP::operator<< (MHashPP::Command c)
{
	switch (c) {
		case endhash:
		HashBytes = end() ;
		break ;

		default:
		myassert (0) ;
		break ;
	}
	return *this ;
}

std::string MHashPP::getAlgorithmName ()
{
	myassert (hashing) ;
	return getAlgorithmName (mhash_get_mhash_algo (HashD)) ;
}

std::string MHashPP::getAlgorithmName (hashid id)
{
	char *name = (char *) mhash_get_hash_name (id) ;
	std::string retval ;
	if (name == NULL) {
		retval = std::string ("<algorithm not found>") ;
	}
	else {
		retval = std::string (name) ;
	}
	free (name) ;
	return retval ;
}

BitString MHashPP::getHashBits ()
{
	myassert (HashBytesValid) ;
	return BitString (HashBytes) ;
}

const std::vector<BYTE>& MHashPP::getHashBytes()
{
	myassert (HashBytesValid) ;
	return HashBytes ;
}