/* * Copyright (c) 2010 Remko Tronçon * Licensed under the GNU General Public License v3. * See Documentation/Licenses/GPLv3.txt for more information. */ #pragma once #include #include #include #include namespace Swift { namespace HMAC_Detail { template struct KeyWrapper; template<> struct KeyWrapper { ByteArray wrap(const ByteArray& hash) const { return hash; } }; template<> struct KeyWrapper { SafeByteArray wrap(const ByteArray& hash) const { return createSafeByteArray(hash); } }; template static ByteArray getHMAC(const KeyType& key, const ByteArray& data) { Hash hash; // Create the padded key KeyType paddedKey(key.size() <= BlockSize ? key : KeyWrapper().wrap(hash(key))); paddedKey.resize(BlockSize, 0x0); // Create the first value KeyType x(paddedKey); for (unsigned int i = 0; i < x.size(); ++i) { x[i] ^= 0x36; } append(x, data); // Create the second value KeyType y(paddedKey); for (unsigned int i = 0; i < y.size(); ++i) { y[i] ^= 0x5c; } append(y, hash(x)); return hash(y); } }; template class HMAC { private: public: ByteArray operator()(const ByteArray& key, const ByteArray& data) const { return HMAC_Detail::getHMAC(key, data); } ByteArray operator()(const SafeByteArray& key, const ByteArray& data) const { return HMAC_Detail::getHMAC(key, data); } }; }