library php-hmac
A generic HMAC function that can be used with any hash
lyoshenka/php-hmac
A generic HMAC function that can be used with any hash
- Friday, November 25, 2016
- by lyoshenka
- Repository
- 1 Watchers
- 1 Stars
- 11 Installations
- PHP
- 0 Dependents
- 0 Suggesters
- 1 Forks
- 0 Open issues
- 2 Versions
- 0 % Grown
php-hmac
A generic HMAC function that can be used with any hash, (*1)
Install
composer install lyoshenka/php-hmac
Use
<?php
require __DIR__.'/vendor/autoload.php';
# Ensure that your hash functions returns raw output (not hex)
$sha256Fn = function($message) { return hash('sha256', $message, true); };
# Look up the block size - https://en.wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions
$blockSize = 64; // make sure this in bytes, not bits
# Compute the HMAC digest
$digest = \lyoshenka\hmac("The message text goes here", "secret-key", $sha256Fn, $blockSize);
# Output the digest as a hex string
echo bin2hex($digest) . "\n";
Examples
See examples.php, (*2)
References
- https://tools.ietf.org/html/rfc2104
- https://en.wikipedia.org/wiki/Hash-based_message_authentication_code
- https://en.wikipedia.org/wiki/Comparison_of_cryptographic_hash_functions