Botan
1.11.15
|
00001 /* 00002 * OctetString 00003 * (C) 1999-2007 Jack Lloyd 00004 * 00005 * Botan is released under the Simplified BSD License (see license.txt) 00006 */ 00007 00008 #ifndef BOTAN_SYMKEY_H__ 00009 #define BOTAN_SYMKEY_H__ 00010 00011 #include <botan/secmem.h> 00012 #include <string> 00013 00014 namespace Botan { 00015 00016 /** 00017 * Octet String 00018 */ 00019 class BOTAN_DLL OctetString 00020 { 00021 public: 00022 /** 00023 * @return size of this octet string in bytes 00024 */ 00025 size_t length() const { return bits.size(); } 00026 00027 /** 00028 * @return this object as a secure_vector<byte> 00029 */ 00030 secure_vector<byte> bits_of() const { return bits; } 00031 00032 /** 00033 * @return start of this string 00034 */ 00035 const byte* begin() const { return &bits[0]; } 00036 00037 /** 00038 * @return end of this string 00039 */ 00040 const byte* end() const { return begin() + bits.size(); } 00041 00042 /** 00043 * @return this encoded as hex 00044 */ 00045 std::string as_string() const; 00046 00047 /** 00048 * XOR the contents of another octet string into this one 00049 * @param other octet string 00050 * @return reference to this 00051 */ 00052 OctetString& operator^=(const OctetString& other); 00053 00054 /** 00055 * Force to have odd parity 00056 */ 00057 void set_odd_parity(); 00058 00059 /** 00060 * Create a new OctetString 00061 * @param str is a hex encoded string 00062 */ 00063 OctetString(const std::string& str = ""); 00064 00065 /** 00066 * Create a new random OctetString 00067 * @param rng is a random number generator 00068 * @param len is the desired length in bytes 00069 */ 00070 OctetString(class RandomNumberGenerator& rng, size_t len); 00071 00072 /** 00073 * Create a new OctetString 00074 * @param in is an array 00075 * @param len is the length of in in bytes 00076 */ 00077 OctetString(const byte in[], size_t len); 00078 00079 /** 00080 * Create a new OctetString 00081 * @param in a bytestring 00082 */ 00083 OctetString(const secure_vector<byte>& in) : bits(in) {} 00084 00085 /** 00086 * Create a new OctetString 00087 * @param in a bytestring 00088 */ 00089 OctetString(const std::vector<byte>& in) : bits(in.begin(), in.end()) {} 00090 private: 00091 secure_vector<byte> bits; 00092 }; 00093 00094 /** 00095 * Compare two strings 00096 * @param x an octet string 00097 * @param y an octet string 00098 * @return if x is equal to y 00099 */ 00100 BOTAN_DLL bool operator==(const OctetString& x, 00101 const OctetString& y); 00102 00103 /** 00104 * Compare two strings 00105 * @param x an octet string 00106 * @param y an octet string 00107 * @return if x is not equal to y 00108 */ 00109 BOTAN_DLL bool operator!=(const OctetString& x, 00110 const OctetString& y); 00111 00112 /** 00113 * Concatenate two strings 00114 * @param x an octet string 00115 * @param y an octet string 00116 * @return x concatenated with y 00117 */ 00118 BOTAN_DLL OctetString operator+(const OctetString& x, 00119 const OctetString& y); 00120 00121 /** 00122 * XOR two strings 00123 * @param x an octet string 00124 * @param y an octet string 00125 * @return x XORed with y 00126 */ 00127 BOTAN_DLL OctetString operator^(const OctetString& x, 00128 const OctetString& y); 00129 00130 00131 /** 00132 * Alternate name for octet string showing intent to use as a key 00133 */ 00134 typedef OctetString SymmetricKey; 00135 00136 /** 00137 * Alternate name for octet string showing intent to use as an IV 00138 */ 00139 typedef OctetString InitializationVector; 00140 00141 } 00142 00143 #endif