📄 shmac.cpp
字号:
// shmac.cc// code for shmac.h// copyright SafeTP Development Group, Inc., 2000 Terms of use are as specified in license.txt// Scott McPeak's transmogrify of Wei Dai's public domain code#include "shmac.h" // this module#include "cryptlib.h" // HashModuleHMAC::HMAC(HashModule &h, const byte *userKey, unsigned int keylength) : k_ipad(h.DataSize()), k_opad(h.DataSize()), hash(h){ int ds = DataSize(); byte *tempKey = NULL; if (keylength > (unsigned)ds) { // RFC 2202's source code and test vectors indicate we are // supposed to hash the key if it is longer than the // hash's block size; Wei Dai's code didn't do this tempKey = new byte[h.DigestSize()]; // hash userKey into tempKey hash.CalculateDigest(tempKey, userKey, keylength); // point userKey at tempKey, so we use the key's hash instead userKey = tempKey; keylength = h.DigestSize(); // NOTE: I had a tricky bug in here where I was using 'ds' instead // of h.DigestSize(). It should be digest size. See RFC2202, // in particular its accompanying source code. (When you get // down to it, source code specs rock! So do automated test // suites, which detected the error in the first place....) } memset(k_ipad, IPAD, ds); xorbuf(k_ipad, userKey, keylength); memset(k_opad, OPAD, ds); xorbuf(k_opad, userKey, keylength); Init(); if (tempKey != NULL) { delete[] tempKey; }}void HMAC::Init(){ hash.Update(k_ipad, DataSize());}void HMAC::Update(const byte *input, unsigned int length){ hash.Update(input, length);}void HMAC::Final(byte *mac){ hash.Final(mac); hash.Update(k_opad, DataSize()); hash.Update(mac, DigestSize()); hash.Final(mac); Init();}unsigned int HMAC::DigestSize() const{ return hash.DigestSize();}unsigned int HMAC::DataSize() const{ return hash.DataSize();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -