~ubuntu-branches/ubuntu/precise/botan1.8/precise

« back to all changes in this revision

Viewing changes to src/utils/buf_comp/buf_comp.h

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2009-08-04 00:47:32 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20090804004732-bmf7f33nfoctocbz
Tags: 1.8.5-1
* Merging upstream version 1.8.5.
* Adding old changelog entries for separately uploaded botan packages
  in the past.
* Using correct rfc-2822 date formats in changelog.
* Wrapping build depends.
* Adding misc depends.
* Renaming local manpages directory to common name.
* Minimizing rules file.
* Doing some minor cosmetical updates in the manpage.
* Updating copyright file to reflect changes of upstream version
  1.8.0.
* Using new configure.py instread of configure.pl, updating necessary
  things to cope with that.
* Updating.
* Tidy debhelper install files.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/**
 
2
* BufferedComputation
 
3
* (C) 1999-2007 Jack Lloyd
 
4
*
 
5
* Distributed under the terms of the Botan license
 
6
*/
 
7
 
 
8
#ifndef BOTAN_BUFFERED_COMPUTATION_H__
 
9
#define BOTAN_BUFFERED_COMPUTATION_H__
 
10
 
 
11
#include <botan/secmem.h>
 
12
 
 
13
namespace Botan {
 
14
 
 
15
/**
 
16
* This class represents any kind of computation which
 
17
* uses an internal state,
 
18
* such as hash functions.
 
19
*/
 
20
class BOTAN_DLL BufferedComputation
 
21
   {
 
22
   public:
 
23
 
 
24
      /**
 
25
      * The length of the output of this function in bytes.
 
26
      */
 
27
      const u32bit OUTPUT_LENGTH;
 
28
 
 
29
      /**
 
30
      * Add new input to process.
 
31
      * @param in the input to process as a byte array
 
32
      * @param the length of the byte array
 
33
      */
 
34
      void update(const byte in[], u32bit length) { add_data(in, length); }
 
35
 
 
36
      /**
 
37
      * Add new input to process.
 
38
      * @param in the input to process as a MemoryRegion
 
39
      */
 
40
      void update(const MemoryRegion<byte>& in) { add_data(in, in.size()); }
 
41
 
 
42
      /**
 
43
      * Add new input to process.
 
44
      * @param str the input to process as a std::string. Will be interpreted
 
45
      * as a byte array based on
 
46
      * the strings encoding.
 
47
      */
 
48
      void update(const std::string& str)
 
49
         {
 
50
         add_data(reinterpret_cast<const byte*>(str.data()), str.size());
 
51
         }
 
52
 
 
53
      /**
 
54
      * Process a single byte.
 
55
      * @param in the byte to process
 
56
      */
 
57
      void update(byte in) { add_data(&in, 1); }
 
58
 
 
59
      /**
 
60
      * Complete the computation and retrieve the
 
61
      * final result.
 
62
      * @param out The byte array to be filled with the result.
 
63
      * Must be of length OUTPUT_LENGTH.
 
64
      */
 
65
      void final(byte out[]) { final_result(out); }
 
66
 
 
67
      /**
 
68
      * Complete the computation and retrieve the
 
69
      * final result.
 
70
      * @return a SecureVector holding the result
 
71
      */
 
72
      SecureVector<byte> final()
 
73
         {
 
74
         SecureVector<byte> output(OUTPUT_LENGTH);
 
75
         final_result(output);
 
76
         return output;
 
77
         }
 
78
 
 
79
      /**
 
80
      * Update and finalize computation. Does the same as calling update()
 
81
      * and final() consecutively.
 
82
      * @param in the input to process as a byte array
 
83
      * @param length the length of the byte array
 
84
      * @result the result of the call to final()
 
85
      */
 
86
      SecureVector<byte> process(const byte in[], u32bit length)
 
87
         {
 
88
         add_data(in, length);
 
89
         return final();
 
90
         }
 
91
 
 
92
      /**
 
93
      * Update and finalize computation. Does the same as calling update()
 
94
      * and final() consecutively.
 
95
      * @param in the input to process
 
96
      * @result the result of the call to final()
 
97
      */
 
98
      SecureVector<byte> process(const MemoryRegion<byte>& in)
 
99
         {
 
100
         add_data(in, in.size());
 
101
         return final();
 
102
         }
 
103
 
 
104
      /**
 
105
      * Update and finalize computation. Does the same as calling update()
 
106
      * and final() consecutively.
 
107
      * @param in the input to process as a string
 
108
      * @result the result of the call to final()
 
109
      */
 
110
      SecureVector<byte> process(const std::string& in)
 
111
         {
 
112
         update(in);
 
113
         return final();
 
114
         }
 
115
 
 
116
      BufferedComputation(u32bit out_len) : OUTPUT_LENGTH(out_len) {}
 
117
      virtual ~BufferedComputation() {}
 
118
   private:
 
119
      BufferedComputation& operator=(const BufferedComputation&);
 
120
      virtual void add_data(const byte[], u32bit) = 0;
 
121
      virtual void final_result(byte[]) = 0;
 
122
   };
 
123
 
 
124
}
 
125
 
 
126
#endif