{
    "mode": "perldoc",
    "parameter": "Crypt::Mac::HMAC",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3AMac%3A%3AHMAC/json",
    "generated": "2026-06-10T16:27:00Z",
    "synopsis": "### Functional interface:\nuse Crypt::Mac::HMAC qw( hmac hmachex );\n# calculate MAC from string/buffer\n$hmacraw  = hmac('SHA256', $key, 'data buffer');\n$hmachex  = hmachex('SHA256', $key, 'data buffer');\n$hmacb64  = hmacb64('SHA256', $key, 'data buffer');\n$hmacb64u = hmacb64u('SHA256', $key, 'data buffer');\n### OO interface:\nuse Crypt::Mac::HMAC;\n$d = Crypt::Mac::HMAC->new('SHA256', $key);\n$d->add('any data');\n$d->addfile('filename.dat');\n$d->addfile(*FILEHANDLE);\n$resultraw  = $d->mac;     # raw bytes\n$resulthex  = $d->hexmac;  # hexadecimal form\n$resultb64  = $d->b64mac;  # Base64 form\n$resultb64u = $d->b64umac; # Base64 URL Safe form",
    "sections": {
        "NAME": {
            "content": "Crypt::Mac::HMAC - Message authentication code HMAC\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "### Functional interface:\nuse Crypt::Mac::HMAC qw( hmac hmachex );\n\n# calculate MAC from string/buffer\n$hmacraw  = hmac('SHA256', $key, 'data buffer');\n$hmachex  = hmachex('SHA256', $key, 'data buffer');\n$hmacb64  = hmacb64('SHA256', $key, 'data buffer');\n$hmacb64u = hmacb64u('SHA256', $key, 'data buffer');\n\n### OO interface:\nuse Crypt::Mac::HMAC;\n\n$d = Crypt::Mac::HMAC->new('SHA256', $key);\n$d->add('any data');\n$d->addfile('filename.dat');\n$d->addfile(*FILEHANDLE);\n$resultraw  = $d->mac;     # raw bytes\n$resulthex  = $d->hexmac;  # hexadecimal form\n$resultb64  = $d->b64mac;  # Base64 form\n$resultb64u = $d->b64umac; # Base64 URL Safe form\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Provides an interface to the HMAC message authentication code (MAC) algorithm.\n",
            "subsections": []
        },
        "EXPORT": {
            "content": "Nothing is exported by default.\n\nYou can export selected functions:\n\nuse Crypt::Mac::HMAC qw(hmac hmachex );\n\nOr all of them at once:\n\nuse Crypt::Mac::HMAC ':all';\n",
            "subsections": []
        },
        "FUNCTIONS": {
            "content": "hmac\nLogically joins all arguments into a single string, and returns its HMAC message authentication\ncode encoded as a binary string.\n\n$hmacraw = hmac($hashname, $key, 'data buffer');\n#or\n$hmacraw = hmac($hashname, $key, 'any data', 'more data', 'even more data');\n\n# $hashname ... any <NAME> for which there exists Crypt::Digest::<NAME>\n# $key ......... the key (octets/bytes)\n\nhmachex\nLogically joins all arguments into a single string, and returns its HMAC message authentication\ncode encoded as a hexadecimal string.\n\n$hmachex = hmachex($hashname, $key, 'data buffer');\n#or\n$hmachex = hmachex($hashname, $key, 'any data', 'more data', 'even more data');\n\n# $hashname ... any <NAME> for which there exists Crypt::Digest::<NAME>\n# $key ......... the key (octets/bytes, not hex!)\n\nhmacb64\nLogically joins all arguments into a single string, and returns its HMAC message authentication\ncode encoded as a Base64 string.\n\n$hmacb64 = hmacb64($hashname, $key, 'data buffer');\n#or\n$hmacb64 = hmacb64($hashname, $key, 'any data', 'more data', 'even more data');\n\n# $hashname ... any <NAME> for which there exists Crypt::Digest::<NAME>\n# $key ......... the key (octets/bytes, not Base64!)\n\nhmacb64u\nLogically joins all arguments into a single string, and returns its HMAC message authentication\ncode encoded as a Base64 URL Safe string (see RFC 4648 section 5).\n\n$hmacb64url = hmacb64u($hashname, $key, 'data buffer');\n#or\n$hmacb64url = hmacb64u($hashname, $key, 'any data', 'more data', 'even more data');\n\n# $hashname ... any <NAME> for which there exists Crypt::Digest::<NAME>\n# $key ......... the key (octets/bytes, not Base64url!)\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new\n$d = Crypt::Mac::HMAC->new($hashname, $key);\n\n# $hashname ... any <NAME> for which there exists Crypt::Digest::<NAME>\n# $key ......... the key (octets/bytes)\n\nclone\n$d->clone();\n\nreset\n$d->reset();\n\nadd\n$d->add('any data');\n#or\n$d->add('any data', 'more data', 'even more data');\n\naddfile\n$d->addfile('filename.dat');\n#or\n$d->addfile(*FILEHANDLE);\n\nmac\n$resultraw = $d->mac();\n\nhexmac\n$resulthex = $d->hexmac();\n\nb64mac\n$resultb64 = $d->b64mac();\n\nb64umac\n$resultb64url = $d->b64umac();\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "*   CryptX\n\n*   <https://en.wikipedia.org/wiki/Hmac>\n\n*   <https://tools.ietf.org/html/rfc2104>\n",
            "subsections": []
        }
    },
    "summary": "Crypt::Mac::HMAC - Message authentication code HMAC",
    "flags": [],
    "examples": [],
    "see_also": []
}