PHPackages                             drewlabs/crypt - 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. [Security](/categories/security)
4. /
5. drewlabs/crypt

ActiveLibrary[Security](/categories/security)

drewlabs/crypt
==============

Provides files encryption &amp; Decryption, HMAC Hash object and Password Hashing using PHP password\_hash, password\_verify &amp; hash\_equals functions

v0.2.5(3y ago)0129MITPHP

Since Jun 1Pushed 3y ago1 watchersCompare

[ Source](https://github.com/azandrew-sidoine/drewlabs-php-crypt)[ Packagist](https://packagist.org/packages/drewlabs/crypt)[ RSS](/packages/drewlabs-crypt/feed)WikiDiscussions master Synced 5d ago

READMEChangelogDependencies (2)Versions (12)Used By (0)

Crypt
=====

[](#crypt)

Crypt is PHP composer based package providing hashing and encryption classes, and methods, based on PHP default hashing &amp; encryption methods.

Installation
------------

[](#installation)

Simply use `composer` PHP dependencies manager to install the package. If there is no composer installation on your operating system, you should be able to install the utility using this guide \[\].

Once composer is intalled on your operating system, run the comand below in your terminal at the root of your project:

> composer require drewlabs/crypt

The above command will add `drewlabs/crypt` package and it dependencies to your project.

Usage
-----

[](#usage)

### Encrypting a raw string

[](#encrypting-a-raw-string)

```
use Drewlabs\Crypt\Encrypter\Crypt;

// Creates a crypt instance
$instance = Crypt::new();
$encrypted = $instance->encryptString('Raw string value');
```

**Note**By default `Crypt::new()` generate a random key and use `AES-128-CBC` as cipher type. To override the defaults:

```
use Drewlabs\Crypt\Encrypter\Crypt;

// Creates a crypt instance
$instance = Crypt::new('MySecret');
```

**Note**Supported cipher type are:

- `aes-128-cbc` and `aes-128-gcm` -&gt; Key length equals to 8 characters
- `aes-256-cbc` and `aes-256-gcm` -&gt; Key length equals 16 characters

**Note**If the key is a base64 string, crypt library will try to decode the base64 string before creating the encryption key internally:

```
use Drewlabs\Crypt\Encrypter\Crypt;

// Creates a crypt instance
$instance = Crypt::new(base64_encode('MySecret'));
```

- Decrypting a string

To get the plain text from an encrypted string, simply call the `decryptString()` method on the encrypted text:

```
use Drewlabs\Crypt\Encrypter\Crypt;

// Creates a crypt instance
$instance = Crypt::new();
$encrypted = $instance->encryptString('Raw string value');

// Decrypting text
$plainText = $instance->decryptString($encrypted); // Raw string value
```

- File encryption

The `Crypt` also class provides methods/functions for entrypting an entire file and decrypt the file from back to it original state. Below are the API for encrypting and decrypting files on a disk:

- encryptBlob(string $from, string $to): void // Encrypt document located at path `$from` and output the encrypted content to path `$to`
- decryptBlob(string $from. string $to): void // Decrypt document located at path `$from` and output the encrypted content to path `$to`

### HMAC Hashing

[](#hmac-hashing)

HMAC hashing provides methods for creating hash and checking a raw value against a hash using user defined algorightm. To create a hash value:

```
use Drewlabs\Crypt\HMAC\Hash as HMACHash;

$instance = HMACHash::new();
$hash = $instance->make('My Hashable Value');
```

**Note**By default, hashed value are created from `strings`. But the Hmac implementation supports PHP arrays, and serializable objects (classes having a `toArray()`). If the object does not have a `toArray()` method, the hashing implementation call `get_object_vars` on the object to convert the object into array, before hashing it.

**Note**To create a hash object from existing raw hashed string use the `Hash::raw()` method:

```
use Drewlabs\Crypt\HMAC\Hash as HMACHash;

$instance = HMACHash::raw("...."); // Creates a hash instance from a raw sstring composed
```

- Checking a hashed value

To check a hashed value against a new plain text value, first you create the hash object from raw hashed value and then you call the `check()` method against the plain text.

```
use Drewlabs\Crypt\HMAC\Hash as HMACHash;

$hash = HMACHash::new('md5');
// Returns a boolean indicating whether hashed value and plain text matches.
$boolean = $hash::raw("Hashed value")->check('Hello World!');
```

**Note**By default the library uses `sha256` algorightm when creating hash values. PHP base function `hash_algos` returns the list of supported hash algorithm.

**Note**Below is the api for hashing a value:

- make(?string $alg, ?string $key = null) // Creates a hashed value using user provided algorithm
- hashOptions() // Returns an encoded string composed of has key and algorithm used when hashing a value
- static raw(string $hash, ?string $options = null) // Static method for creating a hash object from a raw hashed value
- check(string $value) // Check the hash object internal hash value against a plain text value

### Password encryption

[](#password-encryption)

The package also comes with implementation for creating `md5`, `argon2`, `argon2i` and `bcrypt` hash from plain text. It internally uses PHP `password_hash` function for creating hash. They are recommended for hashing password for user applications. To create a password hash from plain text:

- Hash manager factory

```
use Drewlabs\Crypt\Passwords\Factory;

// Creates a hash manager from using argon2i password hasing
$hash = Factory::new()->make(\PASSWORD_ARGON2I)
                ->resolve()
                ->make('SuperSecret');
```

- Proxy function API

```
use function Drewlabs\Crypt\Proxy\usePasswordManager;

// Creates a hash manager from using bcrypt password hasing
$hash = usePasswordManager('bcrypt')->make('SuperSecret');
```

###  Health Score

22

—

LowBetter than 22% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity44

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~14 days

Recently: every ~28 days

Total

11

Last Release

1303d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/48c4973d500c7f4233d5ceacab51a57208d5fb60b0f95ae60264cf92380d0534?d=identicon)[azandrew-sidoine](/maintainers/azandrew-sidoine)

---

Top Contributors

[![azandrew-sidoine](https://avatars.githubusercontent.com/u/23530515?v=4)](https://github.com/azandrew-sidoine "azandrew-sidoine (16 commits)")

---

Tags

encryptionpasswordhash

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/drewlabs-crypt/health.svg)

```
[![Health](https://phpackages.com/badges/drewlabs-crypt/health.svg)](https://phpackages.com/packages/drewlabs-crypt)
```

###  Alternatives

[paragonie/password_lock

Wraps Bcrypt-SHA2 in Authenticated Encryption

19348.7k1](/packages/paragonie-password-lock)[passwordlib/passwordlib

A Password Hashing Library

377220.6k6](/packages/passwordlib-passwordlib)[miladrahimi/phpcrypt

Encryption, decryption, and hashing tools for PHP projects

3171.5k2](/packages/miladrahimi-phpcrypt)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
