{"id":22092079,"url":"https://github.com/krakphp/hmac","last_synced_at":"2025-03-23T23:44:50.082Z","repository":{"id":57009017,"uuid":"57081063","full_name":"krakphp/hmac","owner":"krakphp","description":"Hmac Library","archived":false,"fork":false,"pushed_at":"2016-04-25T22:42:43.000Z","size":21,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-01-29T06:52:18.624Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/krakphp.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-04-25T22:40:58.000Z","updated_at":"2016-04-25T22:41:42.000Z","dependencies_parsed_at":"2022-08-21T14:50:49.250Z","dependency_job_id":null,"html_url":"https://github.com/krakphp/hmac","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhmac","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhmac/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhmac/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/krakphp%2Fhmac/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/krakphp","download_url":"https://codeload.github.com/krakphp/hmac/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245186922,"owners_count":20574554,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-12-01T03:08:15.103Z","updated_at":"2025-03-23T23:44:50.058Z","avatar_url":"https://github.com/krakphp.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Hmac\n\nHmac is a library for handling hmac authentication. Look at the source code for documentation.\n\n## Usage\n\n```php\n\u003c?php\n\nuse Krak\\Hmac\\Psr7HmacRequest,\n    Krak\\Hmac\\HmacKeyPair,\n    Krak\\Hmac\\ArrayHmacKeyPairProvider;\n\nuse function Krak\\Hmac\\hmac_sign_request,\n    Krak\\Hmac\\hmac_auth_request;\n\n// create the key pair of public and private keys\n$keypair = new HmacKeyPair('public-key', 'private-key');\n\n// wrap your request with the appropriate HmacRequest wrapper\n$request = new Psr7HmacRequest($psr7_request);\n\n// create the signer with default configuration\n$sign = hmac_sign_request();\n\n// sign the request\n$request = $sign($request, $keypair);\n\n// create a key pair provider for looking up the key pair used for the request\n$provider = new ArrayHmacKeyPairProvider([$keypair]);\n\n// we can authenticate a request now, by creating the authenticator with the\n// same config as the signer (they need to match exactly)\n$auth = hmac_auth_request($provider);\n\n// authenticate the request\nvar_dump($auth($request));\n// output: bool(true)\n```\n\n### HmacConfig\n\nThe `HmacConfig` is passed at creation to the signer and authenticator to allow total configuration of how the request is authed or signed. The same config needs to be provided to the sign and the auth for them to inverse each other.\n\nHere are the options:\n\n```php\n\u003c?php\n\nuse Krak\\Hmac;\n\n$config = Hmac\\HmacConfig::create([\n    'scheme' =\u003e 'CustomHmac', // authentication scheme. Authorization: \u003cscheme\u003e \u003cpublic_key\u003e:\u003chash\u003e\n    'hasher' =\u003e new Hmac\\StdHmacHasher(), // any HmacHasher instance, defaults to Base64HmacHasher(StdHmacHasher)\n    'hs_gen' =\u003e hmac\\hmac_hashed_hs_gen(), // any hash string generator function\n    'time_gen' =\u003e hmac\\hmac_date_time_gen() // any time_gen function for generating a unit of time\n    'time_header' =\u003e 'Date',\n    'auth_header' =\u003e 'Authorization',\n]);\n\n$sign = hmac\\hmac_sign_request($config);\n$auth = hmac\\hmac_auth_request($provider, $config);\n```\n\n### Signers\n\n```\nhmac_sign_request(HmacConfing = null);\nhmac_psr7_sign_request(HmacConfing = null);\nhmac_psr7_sign($sign);\n```\n\n`hmac_sign_request` is the default signer which creates sign function that accepts an `HmacRequest` and `HmacKeyPair`.\n\n`hmac_psr7_sign_request` is a decorated signer around the hmac_sign_request for accepting and returning Psr Http Requests. It simply just wraps `hmac_sign_request` around the `hmac_psr7_sign` decorator.\n\n`hmac_psr7_sign` is a decorator sign function that accepts a sign function and wraps it and returns a sign function that will accept and return Psr Http Requests.\n\n### Hash String Generator\n\nThe hash string generators are functions that take the request and time value and generate the string that will end up being hashed.\n\nprovided `hs_gen` funcs:\n\n    // returns a hs_gen that will md5 hash the content and join everything by the given separator\n    hmac_hashed_hs_gen($sep = \"\\n\");\n\nexample:\n\n```php\n\u003c?php\n\nfunction concat_hs_gen() {\n    return function(Krak\\Hmac\\HmacRequest $req, $time) {\n        return $req-\u003egetUri() . $time;\n    };\n}\n```\n\n### Time Generator\n\nA time generator is just used to generate a time unit like a timestamp or date stamp.\n\nprovided `time_gen` funcs:\n\n    // returns the `time` function which creates a unix timestamp\n    hmac_ts_time_gen();\n\n    // returns a time_gen which creates an RFC 2822  formatted date\n    hmac_date_time_gen();\n\n## Integration\n\n### GuzzleHttp\n\nSimple integration with Guzzle can be done via the `Provider/guzzle.php` functions.\n\n```php\n\u003c?php\n\nuse GuzzleHttp\\HandlerStack,\n    GuzzleHttp\\Client,\n    Krak\\Hmac\\HmacKeyPair;\nuse function Krak\\Hmac\\Provider\\guzzle_hmac_middleware,\n    Krak\\Hmac\\hmac_psr7_sign_request,\n\n$handler = HandlerStack::create();\n$handler-\u003epush(guzzle_hmac_middleware(hmac_psr7_sign_request(), $keypair));\n$client = new Client(['handler' =\u003e $handler]);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fhmac","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fkrakphp%2Fhmac","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fkrakphp%2Fhmac/lists"}