CLX C++ Libraries
Home >> hmac

Declaration

template <class Engine>
Engine hmac(const char* key, size_t keylen, const char* data, size_t datalen);

template <class Engine>
Engine hmac(const std::string& key, const std::string& data);

Overview

hmac クラスは,HMAC (Keyed-Hashing for Message Authentication Code) アルゴリズムを用いてダイジェストを計算するためのクラスです.Engine には, MD5 や SHA1 などのハッシュ値を計算するためのクラスを指定します.指定する Engine には以下のメソッドが定義されている必要があります.

class engine {
public:
    const char* code() const;
    int size() const;
};

code() メソッドは計算されたハッシュ値を返します.また,size() メソッドはハッシュ値のデータサイズを返します.Engin に関しては,現在のところ md5 および sha1 を実装しています.

Example

example_hmac_md5.cpp

#include <iostream>
#include "clx/hmac.h"
#include "clx/md5.h"

int main(int argc, char* argv[]) {
    // test_case = 1
    char key1[17];
    char dat1[9] = "Hi There";
    
    for (size_t i = 0; i < sizeof(key1) - 1; ++i) key1[i] = 0x0b;
    std::cout << "case 1: "
        << clx::hmac<clx::md5>(key1, sizeof(key1) - 1, dat1, sizeof(dat1) - 1).to_string()
        << std::endl;
    
    // test_case = 2
    char key2[5] = "Jefe";
    char dat2[29] = "what do ya want for nothing?";
    
    std::cout << "case 2: "
        << clx::hmac<clx::md5>(key2, sizeof(key2) - 1, dat2, sizeof(dat2) - 1).to_string()
        << std::endl;
    
    // test_cast = 3
    char key3[17];
    char dat3[51];
    
    for (size_t i = 0; i < sizeof(key3) - 1; ++i) key3[i] = 0xaa;
    for (size_t i = 0; i < sizeof(dat3) - 1; ++i) dat3[i] = 0xdd;
    std::cout << "case 3: "
        << clx::hmac<clx::md5>(key3, sizeof(key3) - 1, dat3, sizeof(dat3) - 1).to_string()
        << std::endl;
    
    // test_cast = 4
    char key4[26];
    char dat4[51];
    
    for (size_t i = 0; i < sizeof(key4) - 1; ++i) key4[i] = i + 1;
    for (size_t i = 0; i < sizeof(dat4) - 1; ++i) dat4[i] = 0xcd;
    std::cout << "case 4: "
        << clx::hmac<clx::md5>(key4, sizeof(key4) - 1, dat4, sizeof(dat4) - 1).to_string()
        << std::endl;
    
    // test_cast = 5
    char key5[17];
    char dat5[21] = "Test With Truncation";
    
    for (size_t i = 0; i < sizeof(key5) - 1; ++i) key5[i] = 0x0c;
    std::cout << "case 5: "
        << clx::hmac<clx::md5>(key5, sizeof(key5) - 1, dat5, sizeof(dat5) - 1).to_string()
        << std::endl;
    
    // test_case = 6
    char key6[81];
    char dat6[55] = "Test Using Larger Than Block-Size Key - Hash Key First";
    
    for (size_t i = 0; i < sizeof(key6) - 1; ++i) key6[i] = 0xaa;
    std::cout << "case 6: "
        << clx::hmac<clx::md5>(key6, sizeof(key6) - 1, dat6, sizeof(dat6) - 1).to_string()
        << std::endl;
    
    // test_case = 7
    char key7[81];
    char dat7[74] = "Test Using Larger Than Block-Size Key "
        "and Larger Than One Block-Size Data";
    
    for (size_t i = 0; i < sizeof(key7) - 1; ++i) key7[i] = 0xaa;
    std::cout << "case 7: "
        << clx::hmac<clx::md5>(key7, sizeof(key7) - 1, dat7, sizeof(dat7) - 1).to_string()
        << std::endl;
    
    return 0;
}
Result
case 1: 9294727a3638bb1c13f48ef8158bfc9d
case 2: 750c783e6ab0b503eaa86e310a5db738
case 3: 56be34521d144c88dbb8c733f0e8b3f6
case 4: 697eaf0aca3a3aea3a75164746ffaa79
case 5: 56461ef2342edc00f9bab995690efd4c
case 6: 6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
case 7: 6f630fad67cda0ee1fb1f562db3aa53e

HMAC-SHA1 のサンプルコードについては, example_hmac_sha1.cpp を参照して下さい.

Related Pages

  1. CLX C++ Libraries - md5
  2. CLX C++ Libraries - sha1

Reference

  1. Test Cases for HMAC-MD5 and HMAC-SHA-1