/* */ #ifndef _D_LIBGCRYPT_ARC4_CONTEXT_H_ #define _D_LIBGCRYPT_ARC4_CONTEXT_H_ #include "common.h" #include "DlAbortEx.h" #include namespace aria2 { class LibgcryptARC4Context { private: gcry_cipher_hd_t _cipherCtx; void handleError(gcry_error_t err) const { throw new DlAbortEx("Exception in libgcrypt routine(ARC4Context class): %s", gcry_strerror(err)); } public: LibgcryptARC4Context():_cipherCtx(0) {} ~LibgcryptARC4Context() { gcry_cipher_close(_cipherCtx); } gcry_cipher_hd_t getCipherContext() const { return _cipherCtx; } void init(const unsigned char* key, size_t keyLength) { gcry_cipher_close(_cipherCtx); int algo = GCRY_CIPHER_ARCFOUR; int mode = GCRY_CIPHER_MODE_STREAM; unsigned int flags = 0; { gcry_error_t r = gcry_cipher_open(&_cipherCtx, algo, mode, flags); if(r) { handleError(r); } } { gcry_error_t r = gcry_cipher_setkey(_cipherCtx, key, keyLength); if(r) { handleError(r); } } { gcry_error_t r = gcry_cipher_setiv(_cipherCtx, 0, 0); if(r) { handleError(r); } } } }; } // namespace aria2 #endif // _D_LIBGCRYPT_ARC4_CONTEXT_H_