initial import of 'tito' library
git-svn-id: svn://ttmath.org/publicrep/tito/src@361 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
26
Makefile
Executable file
26
Makefile
Executable file
@@ -0,0 +1,26 @@
|
|||||||
|
include Makefile.o.dep
|
||||||
|
|
||||||
|
|
||||||
|
all: tito.a
|
||||||
|
|
||||||
|
tito.a: $(o)
|
||||||
|
ar rcs tito.a $(o)
|
||||||
|
|
||||||
|
|
||||||
|
%.o: %.cpp
|
||||||
|
$(CXX) -c $(CXXFLAGS) $<
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
depend:
|
||||||
|
makedepend $(CXXFLAGS) -Y. -f- *.cpp > Makefile.dep
|
||||||
|
echo -n "o = " > Makefile.o.dep
|
||||||
|
ls -1 *.cpp | xargs -I foo echo -n foo " " | sed -E "s/([^\.]*)\.cpp[ ]/\1\.o/g" >> Makefile.o.dep
|
||||||
|
|
||||||
|
|
||||||
|
clean:
|
||||||
|
rm -f *.o
|
||||||
|
rm -f *.a
|
||||||
|
|
||||||
|
|
||||||
|
include Makefile.dep
|
7
Makefile.dep
Executable file
7
Makefile.dep
Executable file
@@ -0,0 +1,7 @@
|
|||||||
|
# DO NOT DELETE
|
||||||
|
|
||||||
|
edanticaes.o: aes.h
|
||||||
|
edanticbase64.o: base64.h
|
||||||
|
edanticcrypto.o: crypto.h aes.h base64.h /home/tomek/roboczy/ezc/src/utf8.h
|
||||||
|
edanticcrypto.o: misc.h
|
||||||
|
edanticmisc.o: misc.h
|
1
Makefile.o.dep
Executable file
1
Makefile.o.dep
Executable file
@@ -0,0 +1 @@
|
|||||||
|
o = aes.o base64.o crypto.o misc.o
|
239
aes.h
Executable file
239
aes.h
Executable file
@@ -0,0 +1,239 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef headerfile_tito_aes
|
||||||
|
#define headerfile_tito_aes
|
||||||
|
|
||||||
|
#include <vector>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
class AES
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
typedef unsigned char uint8;
|
||||||
|
typedef unsigned short int uint16;
|
||||||
|
typedef unsigned int uint32;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// 'state'
|
||||||
|
// 128 bits data block (16 bytes)
|
||||||
|
// this is input/output buffer for encoding/decoding
|
||||||
|
uint8 data[4][4];
|
||||||
|
|
||||||
|
// copying 16 bytes (128bits) buffer to/from 'data' table
|
||||||
|
void CopyToData(const uint8 * state);
|
||||||
|
void CopyFromData(uint8 * state);
|
||||||
|
void CopyFromData(std::vector<uint8> & out, bool clear = true);
|
||||||
|
|
||||||
|
// setting a new key
|
||||||
|
// key_length should be either: 16, 24 or 32
|
||||||
|
// if the key has incorrect length the method does nothing and returns false
|
||||||
|
bool Key(AES::uint8 * key_src, int key_length);
|
||||||
|
|
||||||
|
// encoding/decoding one 128 bits block
|
||||||
|
// you can set the value for encoding/decoding either by directly
|
||||||
|
// setting 'data' table or using CopyToData method
|
||||||
|
void EncodeData();
|
||||||
|
void DecodeData();
|
||||||
|
|
||||||
|
// encoding/decoding block of data
|
||||||
|
// the length should be multiplication of 16 (128bits)
|
||||||
|
// the method returns false if length is incorrect
|
||||||
|
bool Encode(uint8 * block, size_t len);
|
||||||
|
bool Decode(uint8 * block, size_t len);
|
||||||
|
|
||||||
|
// encoding/decoding block of data
|
||||||
|
// there is not any restrictions to the length
|
||||||
|
// we are using our own header in which we remember the length
|
||||||
|
// some control sums, how many padding bytes were used
|
||||||
|
// Encode returns always true
|
||||||
|
// Decode returns false is the control sums are different from original in the header
|
||||||
|
bool Encode(const uint8 * block, size_t len, const std::string & name, std::vector<uint8> & out);
|
||||||
|
bool Encode(const std::vector<uint8> & block, const std::string & name, std::vector<uint8> & out);
|
||||||
|
bool Decode(const uint8 * block, size_t len, std::string & name, std::vector<uint8> & out);
|
||||||
|
bool Decode(const std::vector<uint8> & block, std::string & name, std::vector<uint8> & out);
|
||||||
|
|
||||||
|
// printing 'data' tab
|
||||||
|
// for debug purposes
|
||||||
|
void PrintData();
|
||||||
|
void PrintKey(int xs);
|
||||||
|
void PrintKey2(int xs);
|
||||||
|
void PrintKey3(int xs);
|
||||||
|
bool CheckGFTables();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
|
||||||
|
// key (either 128 or 192 or 256 bits)
|
||||||
|
// and a space for key expansion
|
||||||
|
uint8 key[4][60];
|
||||||
|
|
||||||
|
// key length (in bytes)
|
||||||
|
// 16 for 128 bits
|
||||||
|
// 24 for 192 bits
|
||||||
|
// 32 for 256 bits
|
||||||
|
int key_len;
|
||||||
|
|
||||||
|
// key columns
|
||||||
|
// 4 for 128 bits key
|
||||||
|
// 6 for 192 bits key
|
||||||
|
// 8 for 256 bits key
|
||||||
|
int key_col;
|
||||||
|
|
||||||
|
// how many rounds should be performed
|
||||||
|
// 10 for 128 bits key
|
||||||
|
// 12 for 192 bits key
|
||||||
|
// 14 for 256 bits key
|
||||||
|
int round;
|
||||||
|
|
||||||
|
// sbox and inverse sbox tables (256 bytes each)
|
||||||
|
static uint8 sbox[16][16];
|
||||||
|
static uint8 invsbox[16][16];
|
||||||
|
|
||||||
|
// a table for mixcolumn state
|
||||||
|
static int mixcolumntab[4][4];
|
||||||
|
|
||||||
|
static uint8 Rcon[10];
|
||||||
|
|
||||||
|
|
||||||
|
static int invmixcolumntab[4][4];
|
||||||
|
|
||||||
|
static uint8 gf_mul9[16][16];
|
||||||
|
static uint8 gf_mul11[16][16];
|
||||||
|
static uint8 gf_mul13[16][16];
|
||||||
|
static uint8 gf_mul14[16][16];
|
||||||
|
|
||||||
|
// returning a coresponding value from sbox table
|
||||||
|
uint8 SubBytes(AES::uint8 v);
|
||||||
|
|
||||||
|
// making the SubBytes algorithm on all items in 'data' table
|
||||||
|
void SubBytes();
|
||||||
|
|
||||||
|
// performing shift rows by 1 item
|
||||||
|
void ShiftRows1(AES::uint8 tab[4]);
|
||||||
|
|
||||||
|
// performing shift rows by 2 items
|
||||||
|
void ShiftRows2(AES::uint8 tab[4]);
|
||||||
|
|
||||||
|
// performing shift rows by 3 item
|
||||||
|
void ShiftRows3(AES::uint8 tab[4]);
|
||||||
|
|
||||||
|
// performing shift rows on 'data' table
|
||||||
|
void ShiftRows();
|
||||||
|
|
||||||
|
|
||||||
|
uint8 MixColumns2(uint8 v);
|
||||||
|
uint8 MixColumns3(uint8 v);
|
||||||
|
uint8 MixColumns(int type, uint8 value);
|
||||||
|
void MixColumns();
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void RotKey(int i);
|
||||||
|
void SubWordKey(int i);
|
||||||
|
void KeyExpansion();
|
||||||
|
|
||||||
|
void AddRoundKey(int xs);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* inverse */
|
||||||
|
|
||||||
|
void InvShiftRows1(uint8 tab[4]);
|
||||||
|
void InvShiftRows2(uint8 tab[4]);
|
||||||
|
void InvShiftRows3(uint8 tab[4]);
|
||||||
|
void InvShiftRows();
|
||||||
|
|
||||||
|
uint8 InvSubBytes(uint8 v);
|
||||||
|
void InvSubBytes();
|
||||||
|
void InvMixColumns();
|
||||||
|
|
||||||
|
uint8 InvMixColumns(uint8 gf_mul[16][16], uint8 v);
|
||||||
|
uint8 InvMixColumns(int type, uint8 value);
|
||||||
|
|
||||||
|
|
||||||
|
/* for debug */
|
||||||
|
|
||||||
|
uint8 GaloisMultiply(uint8 a, uint8 b);
|
||||||
|
bool CheckGFTable(int value, uint8 gf_mul[16][16]);
|
||||||
|
|
||||||
|
|
||||||
|
struct TiHeader
|
||||||
|
{
|
||||||
|
AES::uint16 signature;
|
||||||
|
AES::uint16 pad1;
|
||||||
|
AES::uint16 pad2;
|
||||||
|
AES::uint32 checksum_before;
|
||||||
|
AES::uint32 checksum_after;
|
||||||
|
AES::uint32 name_len;
|
||||||
|
size_t all_len;
|
||||||
|
};
|
||||||
|
|
||||||
|
void IntToBuf(uint8 * buf, uint16 value);
|
||||||
|
void IntToBuf(uint8 * buf, uint32 value);
|
||||||
|
void CreateHeader(TiHeader & header, const uint8 * block, size_t len, const std::string & name);
|
||||||
|
void PutHeader(const TiHeader & header, std::vector<uint8> & out);
|
||||||
|
void PutChecksumAfter(std::vector<AES::uint8> & out);
|
||||||
|
uint32 Checksum(const uint8 * block, size_t len);
|
||||||
|
void EncodePad(int & y, int & x, uint16 pad, std::vector<uint8> & out);
|
||||||
|
void EncodeName(int & y, int & x, const std::string & name, std::vector<uint8> & out);
|
||||||
|
void EncodeData(int & y, int & x, const uint8 * block, size_t len, std::vector<uint8> & out);
|
||||||
|
|
||||||
|
void ReadHeader(TiHeader & header, const uint8 * block);
|
||||||
|
void DecodePad(int & y, int & x, const uint8 * & block, size_t & len, uint16 pad);
|
||||||
|
void DecodeName(int & y, int & x, const uint8 * & block, size_t & len, uint32 name_len, std::string & name);
|
||||||
|
void DecodeData(int & y, int & x, const uint8 * & block, size_t & len, size_t data_len, std::vector<uint8> & out);
|
||||||
|
|
||||||
|
bool CheckSignatureAndChecksumAfter(const TiHeader & header, const uint8 * block, size_t len);
|
||||||
|
bool CheckChecksumBefore(const TiHeader & header, std::vector<uint8> & out);
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
281
base64.cpp
Executable file
281
base64.cpp
Executable file
@@ -0,0 +1,281 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <string.h>
|
||||||
|
#include "base64.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
// 64 characters
|
||||||
|
const char * Base64::tabbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Base64::Base64()
|
||||||
|
{
|
||||||
|
padding = '=';
|
||||||
|
tabbase64_len = 64;
|
||||||
|
not_existing = 256; // first 8 bits are zero
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::SetPadding(char c)
|
||||||
|
{
|
||||||
|
for(size_t i=0 ; i<tabbase64_len ; ++i)
|
||||||
|
if( tabbase64[i] == c )
|
||||||
|
return;
|
||||||
|
|
||||||
|
padding = c;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::Convert3to4Pieces(const char * s, size_t len, unsigned int * tab4)
|
||||||
|
{
|
||||||
|
const unsigned char * tab3 = reinterpret_cast<const unsigned char*>(s);
|
||||||
|
|
||||||
|
tab4[0] = tab4[1] = tab4[2] = tab4[3] = 64;
|
||||||
|
|
||||||
|
if( len == 0 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tab4[0] = tab3[0] >> 2;
|
||||||
|
tab4[1] = (tab3[0] & 0x03) << 4;
|
||||||
|
|
||||||
|
if( len == 1 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tab4[1] = tab4[1] | ((tab3[1] & 0xf0) >> 4);
|
||||||
|
tab4[2] = (tab3[1] & 0x0f) << 2;
|
||||||
|
|
||||||
|
if( len == 2 )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tab4[2] = (tab4[2]) | ((tab3[2] & 0xc0) >> 6);
|
||||||
|
tab4[3] = tab3[2] & 0x3f;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::ConvertFrom4to3Pieces(const unsigned int * tab4, unsigned int * tab3)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
tab4[0] and tab4[1] are always different from 'not_existing'
|
||||||
|
*/
|
||||||
|
|
||||||
|
tab3[0] = ((tab4[0] & 0x3f) << 2) | (( tab4[1] & 0x30 ) >> 4);
|
||||||
|
tab3[1] = tab3[2] = not_existing;
|
||||||
|
|
||||||
|
if( tab4[2] == not_existing )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tab3[1] = ((tab4[1] & 0x0f) << 4) | (( tab4[2] & 0x3c ) >> 2);
|
||||||
|
|
||||||
|
if( tab4[3] == not_existing )
|
||||||
|
return;
|
||||||
|
|
||||||
|
tab3[2] = ((tab4[2] & 0x03) << 6) | (tab4[3] & 0x3f);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool Base64::CharFromBase64(unsigned int from, unsigned int & to)
|
||||||
|
{
|
||||||
|
if( from>='A' && from<='Z' )
|
||||||
|
{
|
||||||
|
to = from-'A';
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( from>='a' && from<='z' )
|
||||||
|
{
|
||||||
|
to = from - 'a' + 'Z'-'A' + 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( from>='0' && from<='9' )
|
||||||
|
{
|
||||||
|
to = from - '0' + 'Z'-'A' + 1 + 'z'-'a' + 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( from == '+' )
|
||||||
|
{
|
||||||
|
to = tabbase64_len - 2;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
if( from == '/' )
|
||||||
|
{
|
||||||
|
to = tabbase64_len - 1;
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
such character does not exist in tabbase64 table
|
||||||
|
*/
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool Base64::Make4pieces(const char * s, unsigned int * tab4)
|
||||||
|
{
|
||||||
|
for(size_t i=0 ; i<4 ; ++i)
|
||||||
|
{
|
||||||
|
if( s[i] == padding )
|
||||||
|
tab4[i] = not_existing;
|
||||||
|
else
|
||||||
|
if( !CharFromBase64(s[i], tab4[i]) )
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::Save4PiecesToString(std::string & s, const unsigned int * tab4)
|
||||||
|
{
|
||||||
|
for(int i=0 ; i<4 ; ++i)
|
||||||
|
{
|
||||||
|
if( tab4[i] < tabbase64_len )
|
||||||
|
s += tabbase64[tab4[i]];
|
||||||
|
else
|
||||||
|
s += padding;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::Save3PiecesToString(std::string & s, const unsigned int * tab3)
|
||||||
|
{
|
||||||
|
for(int i=0 ; i<3 ; ++i)
|
||||||
|
{
|
||||||
|
if( tab3[i] != not_existing )
|
||||||
|
s += tab3[i];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
*/
|
||||||
|
void Base64::Encode(const char * in, size_t len, std::string & out)
|
||||||
|
{
|
||||||
|
unsigned int tab4[4];
|
||||||
|
|
||||||
|
out.clear();
|
||||||
|
size_t new_size = len + len/3 + 3;
|
||||||
|
out.reserve(new_size);
|
||||||
|
|
||||||
|
for(size_t i=0 ; i < len ; i+=3)
|
||||||
|
{
|
||||||
|
Convert3to4Pieces(in+i, len-i, tab4);
|
||||||
|
Save4PiecesToString(out, tab4);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::Encode(const char * in, std::string & out)
|
||||||
|
{
|
||||||
|
size_t len = strlen(in);
|
||||||
|
Encode(in, len, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Base64::Encode(const std::string & in, std::string & out)
|
||||||
|
{
|
||||||
|
Encode(in.c_str(), in.size(), out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool Base64::Decode(const char * in, size_t len, std::string & out)
|
||||||
|
{
|
||||||
|
unsigned int tab3[3];
|
||||||
|
unsigned int tab4[4];
|
||||||
|
|
||||||
|
out.clear();
|
||||||
|
size_t new_size = len - len/3 + 3;
|
||||||
|
out.reserve(new_size);
|
||||||
|
|
||||||
|
if( len % 4 != 0 )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
for(size_t i=0 ; i<len ; i+=4)
|
||||||
|
{
|
||||||
|
if( !Make4pieces(in+i, tab4) )
|
||||||
|
return false;
|
||||||
|
|
||||||
|
ConvertFrom4to3Pieces(tab4, tab3);
|
||||||
|
Save3PiecesToString(out, tab3);
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Base64::Decode(const char * in, std::string & out)
|
||||||
|
{
|
||||||
|
size_t len = strlen(in);
|
||||||
|
return Decode(in, len, out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Base64::Decode(const std::string & in, std::string & out)
|
||||||
|
{
|
||||||
|
return Decode(in.c_str(), in.size(), out);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
|
90
base64.h
Executable file
90
base64.h
Executable file
@@ -0,0 +1,90 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef headerfile_tito_base64
|
||||||
|
#define headerfile_tito_base64
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
class Base64
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
Base64();
|
||||||
|
|
||||||
|
// default '=';
|
||||||
|
void SetPadding(char c);
|
||||||
|
|
||||||
|
void Encode(const char * in, size_t len, std::string & out);
|
||||||
|
void Encode(const char * in, std::string & out);
|
||||||
|
void Encode(const std::string & in, std::string & out);
|
||||||
|
|
||||||
|
bool Decode(const char * in, size_t len, std::string & out);
|
||||||
|
bool Decode(const char * in, std::string & out);
|
||||||
|
bool Decode(const std::string & in, std::string & out);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
static const char * tabbase64;
|
||||||
|
size_t tabbase64_len;
|
||||||
|
char padding;
|
||||||
|
unsigned int not_existing;
|
||||||
|
|
||||||
|
void Convert3to4Pieces(const char * s, size_t len, unsigned int * tab4);
|
||||||
|
void ConvertFrom4to3Pieces(const unsigned int * tab4, unsigned int * tab3);
|
||||||
|
bool CharFromBase64(unsigned int from, unsigned int & to);
|
||||||
|
bool Make4pieces(const char * s, unsigned int * tab4);
|
||||||
|
void Save4PiecesToString(std::string & s, const unsigned int * tab4);
|
||||||
|
void Save3PiecesToString(std::string & s, const unsigned int * tab3);
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
163
crypto.cpp
Executable file
163
crypto.cpp
Executable file
@@ -0,0 +1,163 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "crypto.h"
|
||||||
|
|
||||||
|
// !! FIX ME
|
||||||
|
//#include "log.h"
|
||||||
|
|
||||||
|
#include "../ezc/src/utf8.h"
|
||||||
|
#include "misc.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool Crypto::SetAESKey(unsigned char aes_key[32])
|
||||||
|
{
|
||||||
|
if( !aes.Key(aes_key, 32) )
|
||||||
|
{
|
||||||
|
//log << log1 << "problem with AES key" << logend;
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Crypto::Crypt(const std::wstring & in, std::wstring & out)
|
||||||
|
{
|
||||||
|
Ezc::WideToUTF8(in, utf8_str);
|
||||||
|
aes.Encode((const unsigned char*)utf8_str.c_str(), utf8_str.size(), aes_name, uint8_tab);
|
||||||
|
|
||||||
|
base64_str.clear();
|
||||||
|
|
||||||
|
if( !uint8_tab.empty() )
|
||||||
|
base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), base64_str);
|
||||||
|
|
||||||
|
AssignString(base64_str, out);
|
||||||
|
Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void Crypto::Crypt(const std::vector<char> & in, std::string & out)
|
||||||
|
{
|
||||||
|
aes.Encode((const unsigned char*)&in[0], in.size(), aes_name, uint8_tab);
|
||||||
|
base64.Encode((const char*)&uint8_tab[0], uint8_tab.size(), out);
|
||||||
|
Clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
bool Crypto::Decrypt(const std::wstring & in, std::wstring & out)
|
||||||
|
{
|
||||||
|
out.clear();
|
||||||
|
|
||||||
|
if( in.empty() )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
AssignString(in, base64_str);
|
||||||
|
base64.Decode(base64_str, aes_str);
|
||||||
|
|
||||||
|
if( aes.Decode((const unsigned char *)aes_str.c_str(), aes_str.size(), aes_tmp_name, uint8_tab) )
|
||||||
|
{
|
||||||
|
if( !uint8_tab.empty() )
|
||||||
|
Ezc::UTF8ToWide((const char*)&uint8_tab[0], uint8_tab.size(), out);
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//log << "problem with AES decoding (skipping)" << logend;
|
||||||
|
}
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
bool Crypto::Decrypt(const std::string & in, std::vector<char> & out)
|
||||||
|
{
|
||||||
|
out.clear();
|
||||||
|
|
||||||
|
if( in.empty() )
|
||||||
|
return true;
|
||||||
|
|
||||||
|
base64.Decode(in, aes_str);
|
||||||
|
|
||||||
|
if( aes.Decode((const unsigned char *)aes_str.c_str(), aes_str.size(), aes_tmp_name, uint8_tab) )
|
||||||
|
{
|
||||||
|
out.resize(uint8_tab.size());
|
||||||
|
|
||||||
|
for(size_t i=0 ; i<uint8_tab.size() ; ++i)
|
||||||
|
out[i] = uint8_tab[i];
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//log << "problem with AES decoding (skipping)" << logend;
|
||||||
|
}
|
||||||
|
|
||||||
|
Clear();
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void Crypto::Clear()
|
||||||
|
{
|
||||||
|
Clear(uint8_tab);
|
||||||
|
Clear(aes_name);
|
||||||
|
Clear(aes_tmp_name);
|
||||||
|
Clear(utf8_str);
|
||||||
|
Clear(base64_str);
|
||||||
|
Clear(aes_str);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
130
crypto.h
Executable file
130
crypto.h
Executable file
@@ -0,0 +1,130 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef headerfile_tito_crypto
|
||||||
|
#define headerfile_tito_crypto
|
||||||
|
|
||||||
|
#include "aes.h"
|
||||||
|
#include "base64.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class Crypto
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
setting AES256 key
|
||||||
|
the table should consists of 32 bytes
|
||||||
|
*/
|
||||||
|
bool SetAESKey(unsigned char aes_key[32]);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
crypting AES256 and base64 then
|
||||||
|
so the 'out' string is ready to save as a normal text
|
||||||
|
*/
|
||||||
|
void Crypt(const std::wstring & in, std::wstring & out);
|
||||||
|
void Crypt(const std::vector<char> & in, std::string & out);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
decrypting base64 and AES256 then
|
||||||
|
decrypt can return false if the control sums are incorrect
|
||||||
|
in such a case out is empty
|
||||||
|
if 'in' is empty the 'out' will be empty too
|
||||||
|
*/
|
||||||
|
bool Decrypt(const std::wstring & in, std::wstring & out);
|
||||||
|
bool Decrypt(const std::string & in, std::vector<char> & out);
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
clearing a string or a vector
|
||||||
|
first it puts some characters and then calls clear() method
|
||||||
|
*/
|
||||||
|
template<class BufferType>
|
||||||
|
void Clear(BufferType & str);
|
||||||
|
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
// AES cryptography
|
||||||
|
AES aes;
|
||||||
|
|
||||||
|
// base64 encoding/decoding
|
||||||
|
Base64 base64;
|
||||||
|
|
||||||
|
// a table used with AES
|
||||||
|
std::vector<AES::uint8> uint8_tab;
|
||||||
|
|
||||||
|
// AES name (always empty)
|
||||||
|
std::string aes_name;
|
||||||
|
std::string aes_tmp_name;
|
||||||
|
|
||||||
|
std::string utf8_str;
|
||||||
|
std::string base64_str;
|
||||||
|
std::string aes_str;
|
||||||
|
|
||||||
|
void Clear();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
template<class BufferType>
|
||||||
|
void Crypto::Clear(BufferType & str)
|
||||||
|
{
|
||||||
|
for(size_t i=0 ; i<str.size() ; ++i)
|
||||||
|
str[i] = 0x5c;
|
||||||
|
|
||||||
|
str.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
|
181
misc.cpp
Executable file
181
misc.cpp
Executable file
@@ -0,0 +1,181 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <fstream>
|
||||||
|
#include "misc.h"
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const char * src, size_t len, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
if( dst.capacity() < dst.size() + len )
|
||||||
|
dst.reserve(dst.size() + len + 128);
|
||||||
|
|
||||||
|
for(size_t i=0 ; i<len ; ++i )
|
||||||
|
dst += static_cast<unsigned char>(src[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const char * src, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
for(len=0 ; src[len] ; ++len){}
|
||||||
|
|
||||||
|
AssignString(src, len, dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const std::string & src, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
AssignString(src.c_str(), src.size(), dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, size_t len, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
if( dst.capacity() < dst.size() + len )
|
||||||
|
dst.reserve(dst.size() + len + 128);
|
||||||
|
|
||||||
|
for(size_t i=0 ; i<len ; ++i)
|
||||||
|
dst += static_cast<char>(src[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
for(len=0 ; src[len] ; ++len){}
|
||||||
|
|
||||||
|
AssignString(src, len, dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const std::wstring & src, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
AssignString(src.c_str(), src.size(), dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const char * src, size_t len, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
// we suppose that append is smart enough and we don't have to use reserve()
|
||||||
|
dst.append(src, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const char * src, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
for(len=0 ; src[len] ; ++len){}
|
||||||
|
|
||||||
|
AssignString(src, len, dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const std::string & src, std::string & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
dst.append(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, size_t len, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
// we suppose that append is smart enough and we don't have to use reserve()
|
||||||
|
dst.append(src, len);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
size_t len;
|
||||||
|
|
||||||
|
for(len=0 ; src[len] ; ++len){}
|
||||||
|
|
||||||
|
AssignString(src, len, dst, clear);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
void AssignString(const std::wstring & src, std::wstring & dst, bool clear)
|
||||||
|
{
|
||||||
|
if( clear )
|
||||||
|
dst.clear();
|
||||||
|
|
||||||
|
dst.append(src);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
83
misc.h
Executable file
83
misc.h
Executable file
@@ -0,0 +1,83 @@
|
|||||||
|
/*
|
||||||
|
* This file is a part of Tito - a cryptography library
|
||||||
|
* and is distributed under the (new) BSD licence.
|
||||||
|
* Author: Tomasz Sowa <t.sowa@ttmath.org>
|
||||||
|
*/
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Copyright (c) 2012, Tomasz Sowa
|
||||||
|
* All rights reserved.
|
||||||
|
*
|
||||||
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
* modification, are permitted provided that the following conditions are met:
|
||||||
|
*
|
||||||
|
* * Redistributions of source code must retain the above copyright notice,
|
||||||
|
* this list of conditions and the following disclaimer.
|
||||||
|
*
|
||||||
|
* * Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* * Neither the name Tomasz Sowa nor the names of contributors to this
|
||||||
|
* project may be used to endorse or promote products derived
|
||||||
|
* from this software without specific prior written permission.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
||||||
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef headerfile_tito_misc
|
||||||
|
#define headerfile_tito_misc
|
||||||
|
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
#include <sstream>
|
||||||
|
#include <ctime>
|
||||||
|
#include <cstdio>
|
||||||
|
|
||||||
|
|
||||||
|
namespace Tito
|
||||||
|
{
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/*
|
||||||
|
conversions between ascii text and wide characters
|
||||||
|
(destination is always std::string or std::wstring)
|
||||||
|
|
||||||
|
characters are copied as they are without any locales checking
|
||||||
|
*/
|
||||||
|
|
||||||
|
void AssignString(const char * src, size_t len, std::wstring & dst, bool clear = true);
|
||||||
|
void AssignString(const char * src, std::wstring & dst, bool clear = true);
|
||||||
|
void AssignString(const std::string & src, std::wstring & dst, bool clear = true);
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, size_t len, std::string & dst, bool clear = true);
|
||||||
|
void AssignString(const wchar_t * src, std::string & dst, bool clear = true);
|
||||||
|
void AssignString(const std::wstring & src, std::string & dst, bool clear = true);
|
||||||
|
|
||||||
|
void AssignString(const char * src, size_t len, std::string & dst, bool clear = true);
|
||||||
|
void AssignString(const char * src, std::string & dst, bool clear = true);
|
||||||
|
void AssignString(const std::string & src, std::string & dst, bool clear = true);
|
||||||
|
|
||||||
|
void AssignString(const wchar_t * src, size_t len, std::wstring & dst, bool clear = true);
|
||||||
|
void AssignString(const wchar_t * src, std::wstring & dst, bool clear = true);
|
||||||
|
void AssignString(const std::wstring & src, std::wstring & dst, bool clear = true);
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
} // namespace Tito
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
Reference in New Issue
Block a user