Botan  1.10.9
symkey.h
Go to the documentation of this file.
1 /*
2 * OctetString
3 * (C) 1999-2007 Jack Lloyd
4 *
5 * Distributed under the terms of the Botan license
6 */
7 
8 #ifndef BOTAN_SYMKEY_H__
9 #define BOTAN_SYMKEY_H__
10 
11 #include <botan/secmem.h>
12 #include <string>
13 
14 namespace Botan {
15 
16 /**
17 * Octet String
18 */
19 class BOTAN_DLL OctetString
20  {
21  public:
22  /**
23  * @return size of this octet string in bytes
24  */
25  size_t length() const { return bits.size(); }
26 
27  /**
28  * @return this object as a SecureVector<byte>
29  */
30  SecureVector<byte> bits_of() const { return bits; }
31 
32  /**
33  * @return start of this string
34  */
35  const byte* begin() const { return &bits[0]; }
36 
37  /**
38  * @return end of this string
39  */
40  const byte* end() const { return &bits[bits.size()]; }
41 
42  /**
43  * @return this encoded as hex
44  */
45  std::string as_string() const;
46 
47  /**
48  * XOR the contents of another octet string into this one
49  * @param other octet string
50  * @return reference to this
51  */
52  OctetString& operator^=(const OctetString& other);
53 
54  /**
55  * Force to have odd parity
56  */
57  void set_odd_parity();
58 
59  /**
60  * Change the contents of this octet string
61  * @param hex_string a hex encoded bytestring
62  */
63  void change(const std::string& hex_string);
64 
65  /**
66  * Change the contents of this octet string
67  * @param in the input
68  * @param length of in in bytes
69  */
70  void change(const byte in[], size_t length);
71 
72  /**
73  * Change the contents of this octet string
74  * @param in the input
75  */
76  void change(const MemoryRegion<byte>& in) { bits = in; }
77 
78  /**
79  * Create a new random OctetString
80  * @param rng is a random number generator
81  * @param len is the desired length in bytes
82  */
83  OctetString(class RandomNumberGenerator& rng, size_t len);
84 
85  /**
86  * Create a new OctetString
87  * @param str is a hex encoded string
88  */
89  OctetString(const std::string& str = "") { change(str); }
90 
91  /**
92  * Create a new OctetString
93  * @param in is an array
94  * @param len is the length of in in bytes
95  */
96  OctetString(const byte in[], size_t len) { change(in, len); }
97 
98  /**
99  * Create a new OctetString
100  * @param in a bytestring
101  */
102  OctetString(const MemoryRegion<byte>& in) { change(in); }
103  private:
104  SecureVector<byte> bits;
105  };
106 
107 /**
108 * Compare two strings
109 * @param x an octet string
110 * @param y an octet string
111 * @return if x is equal to y
112 */
113 BOTAN_DLL bool operator==(const OctetString& x,
114  const OctetString& y);
115 
116 /**
117 * Compare two strings
118 * @param x an octet string
119 * @param y an octet string
120 * @return if x is not equal to y
121 */
122 BOTAN_DLL bool operator!=(const OctetString& x,
123  const OctetString& y);
124 
125 /**
126 * Concatenate two strings
127 * @param x an octet string
128 * @param y an octet string
129 * @return x concatenated with y
130 */
131 BOTAN_DLL OctetString operator+(const OctetString& x,
132  const OctetString& y);
133 
134 /**
135 * XOR two strings
136 * @param x an octet string
137 * @param y an octet string
138 * @return x XORed with y
139 */
140 BOTAN_DLL OctetString operator^(const OctetString& x,
141  const OctetString& y);
142 
143 
144 /**
145 * Alternate name for octet string showing intent to use as a key
146 */
148 
149 /**
150 * Alternate name for octet string showing intent to use as an IV
151 */
153 
154 }
155 
156 #endif
OctetString(const byte in[], size_t len)
Definition: symkey.h:96
bool operator!=(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:106
const byte * end() const
Definition: symkey.h:40
OctetString(const MemoryRegion< byte > &in)
Definition: symkey.h:102
bool operator==(const OctetString &s1, const OctetString &s2)
Definition: symkey.cpp:98
size_t length() const
Definition: symkey.h:25
OctetString operator^(const OctetString &k1, const OctetString &k2)
Definition: symkey.cpp:125
unsigned char byte
Definition: types.h:22
OctetString SymmetricKey
Definition: symkey.h:147
SecureVector< byte > bits_of() const
Definition: symkey.h:30
RandomNumberGenerator * rng
Definition: global_rng.cpp:165
OctetString operator+(const OctetString &k1, const OctetString &k2)
Definition: symkey.cpp:114
const byte * begin() const
Definition: symkey.h:35
OctetString(const std::string &str="")
Definition: symkey.h:89
void change(const MemoryRegion< byte > &in)
Definition: symkey.h:76
OctetString InitializationVector
Definition: symkey.h:152