CryptoKeypair.h

00001 // $Id: CryptoKeypair.h 21 2010-09-05 04:18:17Z cschwarz1 $
00002 
00003 #ifndef BASE_CRYPTO_CRYPTOKEYPAIR_H
00004 #define BASE_CRYPTO_CRYPTOKEYPAIR_H
00005 
00006 #include <cstdio>
00007 #include "CryptoChecksum.h"
00008 
00009 namespace base_crypto {
00016     class PPBASE_CRYPTO_EXPORT CryptoKeypair {
00017     public:
00019         enum keypairMethod {
00020             keypairUndef = NID_undef,   
00021             keypairDSA   = NID_dsa,     
00022             keypairRSA   = NID_rsa      
00023         };
00024 
00031         static CryptoKeypair *create(keypairMethod method);
00032 
00039         static CryptoKeypair *create(const char *name);
00040 
00047         static keypairMethod getMethodByName(const char *name);
00048 
00050         virtual ~CryptoKeypair();
00051 
00057         virtual void create(unsigned keylen) = 0;
00058 
00065         void *free(unsigned char *buf) const;
00066 
00072         virtual keypairMethod getMethodId() const = 0;
00073 
00079         virtual const char *getMethodName() const = 0;
00080 
00086         base::String getPrivateKey() const;
00087 
00097         virtual unsigned getPrivateKey(unsigned char **rbuf) const = 0;
00098 
00104         base::String getPublicKey() const;
00105 
00115         virtual unsigned getPublicKey(unsigned char **rbuf) const = 0;
00116 
00123         virtual unsigned getSize() const = 0;
00124 
00131         virtual void readPrivateKey(FILE *file, bool pem) = 0;
00132 
00139         virtual void readPrivateKey(const char *fname, bool pem) = 0;
00140 
00147         virtual void readPublicKey(FILE *file, bool pem) = 0;
00148 
00155         virtual void readPublicKey(const char *fname, bool pem) = 0;
00156 
00162         void setPrivateKey(const base::String &key);
00163 
00170         virtual void setPrivateKey(const unsigned char *buf, unsigned len) = 0;
00171 
00177         void setPublicKey(const base::String &key);
00178 
00185         virtual void setPublicKey(const unsigned char *buf, unsigned len) = 0;
00186 
00194         base::String sign(const base::String &data, CryptoChecksum::checksumMethod method);
00195 
00205         virtual unsigned sign(const unsigned char *data, unsigned len, CryptoChecksum::checksumMethod method, unsigned char *sig) = 0;
00206 
00215         bool verify(const base::String &data, CryptoChecksum::checksumMethod method, const base::String &sig);
00216 
00227         virtual bool verify(const unsigned char *data, unsigned datalen, CryptoChecksum::checksumMethod method,
00228                             const unsigned char *sig, unsigned siglen) = 0;
00229 
00236         virtual void writePrivateKey(FILE *file, bool pem) = 0;
00237 
00244         virtual void writePrivateKey(const char *fname, bool pem) = 0;
00245 
00252         virtual void writePublicKey(FILE *file, bool pem) = 0;
00253 
00260         virtual void writePublicKey(const char *fname, bool pem) = 0;
00261     };
00262 }
00263 
00264 #endif