📄 hmacengine.h
字号:
//
// HMACEngine.h
//
// $Id: //poco/Main/Foundation/include/Foundation/HMACEngine.h#5 $
//
// Definition of the HMACEngine class.
//
// Copyright (c) 2004, Guenter Obiltschnig/Applied Informatics.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// 3. Redistributions in any form must be accompanied by information on
// how to obtain complete source code for this software and any
// accompanying software that uses this software. The source code
// must either be included in the distribution or be available for no
// more than the cost of distribution plus a nominal fee, and must be
// freely redistributable under reasonable conditions. For an
// executable file, complete source code means the source code for all
// modules it contains. It does not include source code for modules or
// files that typically accompany the major components of the operating
// system on which the executable file runs.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
// COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
#ifndef Foundation_HMACEngine_INCLUDED
#define Foundation_HMACEngine_INCLUDED
#ifndef Foundation_Foundation_INCLUDED
#include "Foundation/Foundation.h"
#endif
#ifndef Foundation_DigestEngine_INCLUDED
#include "Foundation/DigestEngine.h"
#endif
#ifndef STD_CSTRING_INCLUDED
#include <string.h>
#define STD_CSTRING_INCLUDED
#endif
Foundation_BEGIN
template <class Engine>
class HMACEngine: public DigestEngine
/// This class implementes the HMAC message
/// authentication code algorithm, as specified
/// in RFC 2104. The underlying DigestEngine
/// (MD5Engine, SHA1Engine, etc.) must be given as
/// template argument.
/// Since the HMACEngine is a DigestEngine, it can
/// be used with the DigestStream class to create
/// a HMAC for a stream.
{
public:
enum
{
BLOCK_SIZE = Engine::BLOCK_SIZE,
DIGEST_SIZE = Engine::DIGEST_SIZE
};
HMACEngine(const std::string& passphrase)
{
init(passphrase.data(), (unsigned) passphrase.length());
}
HMACEngine(const char* passphrase, unsigned length)
{
poco_check_ptr (passphrase);
init(passphrase, length);
}
~HMACEngine()
{
memset(_ipad, 0, BLOCK_SIZE);
memset(_opad, 0, BLOCK_SIZE);
delete [] _ipad;
delete [] _opad;
}
unsigned digestLength() const
{
return DIGEST_SIZE;
}
void reset()
{
_engine.reset();
_engine.update(_ipad, BLOCK_SIZE);
}
const DigestEngine::Digest& digest()
{
const DigestEngine::Digest& d = _engine.digest();
char db[DIGEST_SIZE];
char* pdb = db;
for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end(); ++it)
*pdb++ = *it;
_engine.reset();
_engine.update(_opad, BLOCK_SIZE);
_engine.update(db, DIGEST_SIZE);
const DigestEngine::Digest& result = _engine.digest();
reset();
return result;
}
protected:
void init(const char* passphrase, unsigned length)
{
_ipad = new char[BLOCK_SIZE];
_opad = new char[BLOCK_SIZE];
memset(_ipad, 0, BLOCK_SIZE);
memset(_opad, 0, BLOCK_SIZE);
if (length > BLOCK_SIZE)
{
_engine.reset();
_engine.update(passphrase, length);
const DigestEngine::Digest& d = _engine.digest();
char* ipad = _ipad;
char* opad = _opad;
int n = BLOCK_SIZE;
for (DigestEngine::Digest::const_iterator it = d.begin(); it != d.end() && n-- > 0; ++it)
{
*ipad++ = *it;
*opad++ = *it;
}
}
else
{
memcpy(_ipad, passphrase, length);
memcpy(_opad, passphrase, length);
}
for (int i = 0; i < BLOCK_SIZE; ++i)
{
_ipad[i] ^= 0x36;
_opad[i] ^= 0x5c;
}
reset();
}
void updateImp(const void* data, unsigned length)
{
_engine.update(data, length);
}
private:
HMACEngine();
Engine _engine;
char* _ipad;
char* _opad;
};
Foundation_END
#endif // Foundation_HMACEngine_INCLUDED
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -