PHPackages                             vzool/api-key - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. vzool/api-key

ActiveLibrary[API Development](/categories/api)

vzool/api-key
=============

021PHPCI passing

Since May 14Pushed 1y ago1 watchersCompare

[ Source](https://github.com/vzool/ApiKey)[ Packagist](https://packagist.org/packages/vzool/api-key)[ RSS](/packages/vzool-api-key/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependenciesVersions (1)Used By (0)

[![logo](images/logo_600.jpg)](images/logo_600.jpg)

API Key Management Library (ApiKey)
===================================

[](#api-key-management-library-apikey)

[ ![](https://github.com/vzool/ApiKey/actions/workflows/php.yml/badge.svg)](https://github.com/vzool/ApiKey/actions/workflows/php.yml)[ ![Total Downloads](https://camo.githubusercontent.com/b8f1ca791c53a4c64667e08b36b5302989011f41cda05d8c3b2de25ccea044db/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f767a6f6f6c2f6170692d6b6579)](https://packagist.org/packages/vzool/api-key)[ ![Latest Stable Version](https://camo.githubusercontent.com/8c09765c3ded986c2d76f986eb1aed3e243effa577cb37cf2fe53d17e8c1b4bd/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f767a6f6f6c2f6170692d6b6579)](https://packagist.org/packages/vzool/api-key)[ ![License](https://camo.githubusercontent.com/97c04acee9ce958b454b812cc436b84cf646ddc8793cc284ded1cf3f73ac6b12/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f767a6f6f6c2f6170692d6b6579)](https://packagist.org/packages/vzool/api-key)This library offers a simple and secure solution for API key management. It generates, stores, and validates keys in a way that prevents leakage and impersonation by avoiding a direct link between tokens and stored data. It supports storing keys in memory, in the file system or in `PDO` Databases.

Core Class: `Key`
-----------------

[](#core-class-key)

The `Key` class is the core of the library. It handles the generation, hashing, and validation of API keys.

### Features

[](#features)

- **Key Generation:** Generates a random, secure API key.
- **Hashing:** Hashes the public key using a provided `APP_KEY` and a strong hashing algorithm (default: `SHA3-384`).
- **Token Creation:** Creates a token by combining the public key and a hash of a private key.
- **Token Parsing:** Parses a token to extract the public and shared keys.
- **Validation:** Validates a token by comparing the provided shared key with a newly generated hash.
- **Customization:**
    - `label`: A label for the API key.
    - `ip`: The IP address associated with the API key.
    - `ttl`: The Time-to-live (in seconds) associated with the API key. (default: 0) means no expiration.
    - `APP_KEY`: A secret key used for hashing. **This is required and must be kept secret.**
    - `KEY_LENGTH`: The length of the generated API key (default: 33 bytes).
    - `HASH_ALGO`: The hashing algorithm to use (default: `sha3-384`). See `hash_hmac_algos()` for supported algorithms.
- **Static Helper Functions:**
    - `hmac()`: Computes the HMAC hash of a given `text` using the provided `APP_KEY` and hashing algorithm.
    - `parse()`: Parses a token into its public and shared key components.
    - `create()`: Reconstructs a `Key` object from stored data (hashed public key and data).
- **Testing:** Includes a `test()` method with comprehensive unit tests.

### Installation

[](#installation)

```
composer require vzool/api-key
```

```

```

**Manual Installation**

1. Ensure you have PHP 8.0+ or later.
2. Copy the `ApiKey.php` file to your project.
3. Include the file in your PHP script:

```

```

### Usage

[](#usage)

```
