📄 hmac.cpp
字号:
/************************************************** HMAC Source File ** (C) 1999-2002 The Botan Project **************************************************/#include <botan/hmac.h>#include <botan/lookup.h>namespace Botan {/************************************************** Update an HMAC Calculation **************************************************/void HMAC::add_data(const byte input[], u32bit length) { hash->update(input, length); }/************************************************** Finalize a HMAC Calculation **************************************************/void HMAC::final_result(byte mac[]) { hash->final(mac); hash->update(o_key, o_key.size()); hash->update(mac, OUTPUT_LENGTH); hash->final(mac); hash->update(i_key, i_key.size()); }/************************************************** HMAC Key Schedule **************************************************/void HMAC::key(const byte key[], u32bit length) { hash->clear(); i_key.set(0x36); o_key.set(0x5C); SecureVector<byte> hmac_key(key, length); if(length > hash->HASH_BLOCK_SIZE) hmac_key = hash->process(key, length); xor_buf(i_key, hmac_key, hmac_key.size()); xor_buf(o_key, hmac_key, hmac_key.size()); hash->update(i_key, i_key.size()); }/************************************************** Clear memory of sensitive data **************************************************/void HMAC::clear() throw() { hash->clear(); i_key.clear(); o_key.clear(); }/************************************************** Return the name of this type **************************************************/std::string HMAC::name() const { return "HMAC(" + hash->name() + ")"; }/************************************************** Return a clone of this object **************************************************/MessageAuthenticationCode* HMAC::clone() const { return new HMAC(hash->name()); }/************************************************** HMAC Constructor **************************************************/HMAC::HMAC(const std::string& hash_name) : MessageAuthenticationCode(output_length_of(hash_name), 1, 32), hash(get_hash(hash_name)) { if(hash->HASH_BLOCK_SIZE == 0) throw Invalid_Argument("HMAC cannot be used with " + hash->name()); i_key.create(hash->HASH_BLOCK_SIZE); o_key.create(hash->HASH_BLOCK_SIZE); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -