diff options
Diffstat (limited to 'Swiften/Crypto/OpenSSLCryptoProvider.cpp')
-rw-r--r-- | Swiften/Crypto/OpenSSLCryptoProvider.cpp | 190 |
1 files changed, 95 insertions, 95 deletions
diff --git a/Swiften/Crypto/OpenSSLCryptoProvider.cpp b/Swiften/Crypto/OpenSSLCryptoProvider.cpp index 91387d2..c785041 100644 --- a/Swiften/Crypto/OpenSSLCryptoProvider.cpp +++ b/Swiften/Crypto/OpenSSLCryptoProvider.cpp @@ -20,96 +20,96 @@ using namespace Swift; namespace { - class SHA1Hash : public Hash { - public: - SHA1Hash() : finalized(false) { - if (!SHA1_Init(&context)) { - assert(false); - } - } - - ~SHA1Hash() { - } - - virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE { - return updateInternal(data); - } - - virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE { - return updateInternal(data); - } - - virtual std::vector<unsigned char> getHash() SWIFTEN_OVERRIDE { - assert(!finalized); - std::vector<unsigned char> result(SHA_DIGEST_LENGTH); - SHA1_Final(vecptr(result), &context); - return result; - } - - private: - template<typename ContainerType> - Hash& updateInternal(const ContainerType& data) { - assert(!finalized); - if (!SHA1_Update(&context, vecptr(data), data.size())) { - assert(false); - } - return *this; - } - - private: - SHA_CTX context; - bool finalized; - }; - - class MD5Hash : public Hash { - public: - MD5Hash() : finalized(false) { - if (!MD5_Init(&context)) { - assert(false); - } - } - - ~MD5Hash() { - } - - virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE { - return updateInternal(data); - } - - virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE { - return updateInternal(data); - } - - virtual std::vector<unsigned char> getHash() SWIFTEN_OVERRIDE { - assert(!finalized); - std::vector<unsigned char> result(MD5_DIGEST_LENGTH); - MD5_Final(vecptr(result), &context); - return result; - } - - private: - template<typename ContainerType> - Hash& updateInternal(const ContainerType& data) { - assert(!finalized); - if (!MD5_Update(&context, vecptr(data), data.size())) { - assert(false); - } - return *this; - } - - private: - MD5_CTX context; - bool finalized; - }; - - - template<typename T> - ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) { - unsigned int len = SHA_DIGEST_LENGTH; - std::vector<unsigned char> result(len); - HMAC(EVP_sha1(), vecptr(key), boost::numeric_cast<int>(key.size()), vecptr(data), data.size(), vecptr(result), &len); - return result; - } + class SHA1Hash : public Hash { + public: + SHA1Hash() : finalized(false) { + if (!SHA1_Init(&context)) { + assert(false); + } + } + + ~SHA1Hash() { + } + + virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE { + return updateInternal(data); + } + + virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE { + return updateInternal(data); + } + + virtual std::vector<unsigned char> getHash() SWIFTEN_OVERRIDE { + assert(!finalized); + std::vector<unsigned char> result(SHA_DIGEST_LENGTH); + SHA1_Final(vecptr(result), &context); + return result; + } + + private: + template<typename ContainerType> + Hash& updateInternal(const ContainerType& data) { + assert(!finalized); + if (!SHA1_Update(&context, vecptr(data), data.size())) { + assert(false); + } + return *this; + } + + private: + SHA_CTX context; + bool finalized; + }; + + class MD5Hash : public Hash { + public: + MD5Hash() : finalized(false) { + if (!MD5_Init(&context)) { + assert(false); + } + } + + ~MD5Hash() { + } + + virtual Hash& update(const ByteArray& data) SWIFTEN_OVERRIDE { + return updateInternal(data); + } + + virtual Hash& update(const SafeByteArray& data) SWIFTEN_OVERRIDE { + return updateInternal(data); + } + + virtual std::vector<unsigned char> getHash() SWIFTEN_OVERRIDE { + assert(!finalized); + std::vector<unsigned char> result(MD5_DIGEST_LENGTH); + MD5_Final(vecptr(result), &context); + return result; + } + + private: + template<typename ContainerType> + Hash& updateInternal(const ContainerType& data) { + assert(!finalized); + if (!MD5_Update(&context, vecptr(data), data.size())) { + assert(false); + } + return *this; + } + + private: + MD5_CTX context; + bool finalized; + }; + + + template<typename T> + ByteArray getHMACSHA1Internal(const T& key, const ByteArray& data) { + unsigned int len = SHA_DIGEST_LENGTH; + std::vector<unsigned char> result(len); + HMAC(EVP_sha1(), vecptr(key), boost::numeric_cast<int>(key.size()), vecptr(data), data.size(), vecptr(result), &len); + return result; + } } OpenSSLCryptoProvider::OpenSSLCryptoProvider() { @@ -119,22 +119,22 @@ OpenSSLCryptoProvider::~OpenSSLCryptoProvider() { } Hash* OpenSSLCryptoProvider::createSHA1() { - return new SHA1Hash(); + return new SHA1Hash(); } Hash* OpenSSLCryptoProvider::createMD5() { - return new MD5Hash(); + return new MD5Hash(); } ByteArray OpenSSLCryptoProvider::getHMACSHA1(const SafeByteArray& key, const ByteArray& data) { - return getHMACSHA1Internal(key, data); + return getHMACSHA1Internal(key, data); } ByteArray OpenSSLCryptoProvider::getHMACSHA1(const ByteArray& key, const ByteArray& data) { - return getHMACSHA1Internal(key, data); + return getHMACSHA1Internal(key, data); } bool OpenSSLCryptoProvider::isMD5AllowedForCrypto() const { - return true; + return true; } |