WvStreams
wvxor.cc
1/*
2 * Worldvisions Tunnel Vision Software:
3 * Copyright (C) 1997-2002 Net Integration Technologies, Inc.
4 *
5 * XOR cryptography abstractions.
6 * Could use this to implement short one time pads.
7 */
8#include "wvxor.h"
9
10/***** WvXOREncoder *****/
11
12WvXOREncoder::WvXOREncoder(const void *_key, size_t _keylen) :
13 keylen(_keylen), keyoff(0)
14{
15 key = new unsigned char[keylen];
16 memcpy(key, _key, keylen);
17}
18
19
20WvXOREncoder::~WvXOREncoder()
21{
22 deletev key;
23}
24
25
26bool WvXOREncoder::_encode(WvBuf &inbuf, WvBuf &outbuf, bool flush)
27{
28 size_t len;
29 while ((len = inbuf.optgettable()) != 0)
30 {
31 const unsigned char *data = inbuf.get(len);
32 unsigned char *out = outbuf.alloc(len);
33
34 // FIXME: this loop is SLOW! (believe it or not)
35 while (len-- > 0)
36 {
37 *out++ = (*data++) ^ key[keyoff++];
38 keyoff %= keylen;
39 }
40 }
41 return true;
42}
43
44/***** WvXORStream *****/
45
46WvXORStream::WvXORStream(WvStream *_cloned,
47 const void *_key, size_t _keysize) :
48 WvEncoderStream(_cloned)
49{
50 readchain.append(new WvXOREncoder(_key, _keysize), true);
51 writechain.append(new WvXOREncoder(_key, _keysize), true);
52}
size_t optgettable() const
Returns the optimal maximum number of elements in the buffer currently available for reading without ...
const T * get(size_t count)
Reads exactly the specified number of elements and returns a pointer to a storage location owned by t...
T * alloc(size_t count)
Allocates exactly the specified number of elements and returns a pointer to an UNINITIALIZED storage ...
WvEncoderStream chains a series of encoders on the input and output ports of the underlying stream to...
bool flush(WvBuf &inbuf, WvBuf &outbuf, bool finish=false)
Flushes the encoder and optionally finishes it.
Unified support for streams, that is, sequences of bytes that may or may not be ready for read/write ...
bool _encode(WvBuf &in, WvBuf &out, bool flush)
Template method implementation of encode().
Definition wvxor.cc:26
WvXOREncoder(const void *_key, size_t _keylen)
Creates a new XOR encoder / decoder.
Definition wvxor.cc:12