/* */ #include "LibgcryptARC4Encryptor.h" #include #include "DlAbortEx.h" #include "fmt.h" namespace aria2 { namespace { void handleError(gcry_error_t err) { throw DL_ABORT_EX (fmt("Exception in libgcrypt routine(ARC4Encryptor class): %s", gcry_strerror(err))); } } // namespace ARC4Encryptor::ARC4Encryptor() : hdl_(0) {} ARC4Encryptor::~ARC4Encryptor() { gcry_cipher_close(hdl_); } void ARC4Encryptor::init(const unsigned char* key, size_t keyLength) { int algo = GCRY_CIPHER_ARCFOUR; int mode = GCRY_CIPHER_MODE_STREAM; unsigned int flags = 0; gcry_error_t r; if((r = gcry_cipher_open(&hdl_, algo, mode, flags))) { handleError(r); } if((r = gcry_cipher_setkey(hdl_, key, keyLength))) { handleError(r); } if((r = gcry_cipher_setiv(hdl_, 0, 0))) { handleError(r); } } void ARC4Encryptor::encrypt (size_t len, unsigned char* out, const unsigned char* in) { size_t inlen; if(in == out) { out = const_cast(in); in = 0; inlen = 0; } else { inlen = len; } gcry_error_t r; if((r = gcry_cipher_encrypt(hdl_, out, len, in, inlen))) { handleError(r); } } } // namespace aria2