CppCMS
cppcms/crypto.h
00001 
00002 //                                                                             
00003 //  Copyright (C) 2008-2012  Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>     
00004 //                                                                             
00005 //  See accompanying file COPYING.TXT file for licensing details.
00006 //
00008 #ifndef CPPCMS_CRYPTO_H
00009 #define CPPCMS_CRYPTO_H
00010 
00011 #include <cppcms/defs.h>
00012 #include <booster/noncopyable.h>
00013 #include <booster/hold_ptr.h>
00014 #include <memory>
00015 #include <string>
00016 
00017 namespace cppcms {
00027         namespace crypto {
00034                 class CPPCMS_API key {
00035                 public:
00039                         key();
00043                         key(key const &other);
00047                         key const &operator=(key const &);
00051                         ~key();
00055                         key(void const *data,size_t length);
00059                         explicit key(char const *s);
00063                         explicit key(std::string const &);
00067                         char const *data() const;
00071                         size_t size() const;
00072 
00076                         void reset();
00077 
00081                         void set(void const *ptr,size_t len);
00085                         void set_hex(char const *ptr,size_t len);
00086                         
00091                         void read_from_file(std::string const &file_name);
00092 
00093                 private:
00094                         static unsigned from_hex(char c);
00095                         char *data_;
00096                         size_t size_;
00097                 };
00101                 class CPPCMS_API message_digest : public booster::noncopyable {
00102                 protected:
00104                         message_digest()
00105                         {
00106                         }
00107                 public:
00108                         virtual ~message_digest()
00109                         {
00110                         }
00111                         
00115                         virtual unsigned digest_size() const = 0;
00119                         virtual unsigned block_size() const = 0;
00120 
00124                         virtual void append(void const *ptr,size_t size) = 0;
00129                         virtual void readout(void *ptr) = 0;
00130 
00135                         virtual message_digest *clone() const = 0;
00136 
00140                         virtual char const *name() const = 0;
00141 
00145                         static std::auto_ptr<message_digest> md5();
00149                         static std::auto_ptr<message_digest> sha1();
00154                         static std::auto_ptr<message_digest> create_by_name(std::string const &name);
00155                 };
00156                 
00160                 class CPPCMS_API hmac : public booster::noncopyable  {
00161                 public:
00165                         hmac(std::auto_ptr<message_digest> digest,key const &k);
00169                         hmac(std::string const &name,key const &k);
00170                         ~hmac();
00171 
00175                         unsigned digest_size() const;
00176 
00180                         void append(void const *ptr,size_t size);
00181 
00188                         void readout(void *ptr);
00189                 private:
00190                         void init();    
00191                         struct data_;
00192                         booster::hold_ptr<data_> d;
00193                         std::auto_ptr<message_digest> md_,md_opad_;
00194                         key key_;
00195                 };
00196 
00202                 class CPPCMS_API cbc  : public booster::noncopyable {
00203                 public:
00207                         typedef enum {
00208                                 aes128  = 0,    
00209                                 aes192  = 1,    
00210                                 aes256  = 2     
00211                         } cbc_type;
00212                 
00218                         static std::auto_ptr<cbc> create(cbc_type type);
00227                         static std::auto_ptr<cbc> create(std::string const &name);
00228 
00232                         virtual unsigned block_size() const = 0;
00236                         virtual unsigned key_size() const = 0;
00237 
00241                         virtual void set_key(key const &) = 0;
00245                         virtual void set_iv(void const *ptr,size_t size) = 0;
00249                         virtual void set_nonce_iv() = 0;
00253                         virtual void encrypt(void const *in,void *out,unsigned len) = 0;
00257                         virtual void decrypt(void const *in,void *out,unsigned len) = 0;
00258 
00259                         virtual ~cbc() 
00260                         {
00261                         }
00262                         
00263                 };
00264 
00265         } // crypto
00266 
00267 } // cppcms
00268 
00269 
00270 
00271 #endif