crypto.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_CRYPTO_H
#define CPPCMS_CRYPTO_H
#include <cppcms/defs.h>
#include <booster/noncopyable.h>
#include <booster/hold_ptr.h>
#include <memory>
#include <string>
namespace cppcms {
namespace crypto {
class CPPCMS_API key {
public:
key();
key(key const &other);
key const &operator=(key const &);
~key();
key(void const *data,size_t length);
explicit key(char const *s);
explicit key(std::string const &);
char const *data() const;
size_t size() const;
void reset();
void set(void const *ptr,size_t len);
void set_hex(char const *ptr,size_t len);
void read_from_file(std::string const &file_name);
private:
static unsigned from_hex(char c);
char *data_;
size_t size_;
};
class CPPCMS_API message_digest : public booster::noncopyable {
protected:
message_digest()
{
}
public:
virtual ~message_digest()
{
}
virtual unsigned digest_size() const = 0;
virtual unsigned block_size() const = 0;
virtual void append(void const *ptr,size_t size) = 0;
virtual void readout(void *ptr) = 0;
virtual message_digest *clone() const = 0;
virtual char const *name() const = 0;
static std::auto_ptr<message_digest> md5();
static std::auto_ptr<message_digest> sha1();
static std::auto_ptr<message_digest> create_by_name(std::string const &name);
};
class CPPCMS_API hmac : public booster::noncopyable {
public:
hmac(std::auto_ptr<message_digest> digest,key const &k);
hmac(std::string const &name,key const &k);
~hmac();
unsigned digest_size() const;
void append(void const *ptr,size_t size);
void readout(void *ptr);
private:
void init();
struct data_;
booster::hold_ptr<data_> d;
std::auto_ptr<message_digest> md_,md_opad_;
key key_;
};
class CPPCMS_API cbc : public booster::noncopyable {
public:
typedef enum {
aes128 = 0,
aes192 = 1,
aes256 = 2
} cbc_type;
static std::auto_ptr<cbc> create(cbc_type type);
static std::auto_ptr<cbc> create(std::string const &name);
virtual unsigned block_size() const = 0;
virtual unsigned key_size() const = 0;
virtual void set_key(key const &) = 0;
virtual void set_iv(void const *ptr,size_t size) = 0;
virtual void set_nonce_iv() = 0;
virtual void encrypt(void const *in,void *out,unsigned len) = 0;
virtual void decrypt(void const *in,void *out,unsigned len) = 0;
virtual ~cbc()
{
}
};
} // crypto
} // cppcms
#endif
booster::hold_ptr< data_ >
cppcms::crypto::message_digest
this class provides an API to calculate various cryptographic hash functions
Definition: crypto.h:101
cppcms::crypto::hmac
This object calculates the HMAC signature for the input data.
Definition: crypto.h:160
cppcms::crypto::cbc
Cipher-block chaining encryption and decryption cryptographic service.
Definition: crypto.h:202
cppcms::crypto::message_digest::message_digest
message_digest()
It should be implemented in derived classes.
Definition: crypto.h:104
cppcms::crypto::key
Key object, holds the string that represents the binary key.
Definition: crypto.h:34
booster::noncopyable
This class makes impossible to copy any class derived from this one.
Definition: noncopyable.h:15