PHPackages                             fpoirotte/tomcrypt - 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. [Utility &amp; Helpers](/categories/utility)
4. /
5. fpoirotte/tomcrypt

ActiveExtension[Utility &amp; Helpers](/categories/utility)

fpoirotte/tomcrypt
==================

PHP bindings for libtomcrypt

0.2.2(9y ago)417PHPC

Since Nov 13Pushed 7y ago1 watchersCompare

[ Source](https://github.com/fpoirotte/tomcrypt)[ Packagist](https://packagist.org/packages/fpoirotte/tomcrypt)[ RSS](/packages/fpoirotte-tomcrypt/feed)WikiDiscussions develop Synced 4w ago

READMEChangelog (6)DependenciesVersions (2)Used By (0)

php\_tomcrypt
=============

[](#php_tomcrypt)

PHP bindings for [libtomcrypt](http://www.libtom.net/).

Badges: [![Travis-CI (unknown)](https://camo.githubusercontent.com/711dafa73fc4375957d3e4e7792a08b10f578a10fe9fc4e1ff985c6f814816d1/68747470733a2f2f7472617669732d63692e6f72672f66706f69726f7474652f746f6d63727970742e737667)](http://travis-ci.org/fpoirotte/tomcrypt)

Why?
----

[](#why)

I made this extension for two reasons:

- First, I wanted to learn how to write a PHP extension.
- The `mcrypt` extension was deprecated in PHP 7.0-7.1 and it has been completely removed from PHP 7.2.

    While I agree with the rationale behind that decision (libmcrypt has not been maintained since 2007), I also needed a replacement for some of my own projects. Therefore, I decided to look for a crypto library with:

    - a permissive license (see below)
    - a simple API so that I could easily write bindings for it (I didn't want to have to learn OpenSSL's API for example)
    - relatively good support (eg. widely packaged, receiving updates, etc.)

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

[](#installation)

### POSIX systems (Linux, etc.)

[](#posix-systems-linux-etc)

You can install this extension using `pear`:

```
wget https://github.com/fpoirotte/tomcrypt/archive/master.tar.gz
tar zxvf master.tar.gz
pear install tomcrypt-master/package.xml
```

It will also try to add the extension to your `php.ini` automatically. If it fails to do so, you can enable the extension manually by adding the following line to your `php.ini`:

```
extension=tomcrypt.so
```

### Microsoft Windows

[](#microsoft-windows)

Download a pre-built `php_tomcrypt.dll` from the [release page](https://github.com/fpoirotte/tomcrypt/releases/latest)or [compile the DLL yourself](./README.WIN32.rst).

After that, just drop the DLL into you're PHP installation's `extension_dir`and add the following line of configuration to your `php.ini`:

```
extension=php_tomcrypt.dll
```

Usage
-----

[](#usage)

### Encryption

[](#encryption)

Use code such as the following to encrypt some plaintext data:

```

```

The list of supported ciphers for your platform can be obtained through `tomcrypt_list_ciphers()`. This extension also provides constants which can be used to refer to the various ciphers:

- `TOMCRYPT_CIPHER_3DES`
- `TOMCRYPT_CIPHER_AES`
- `TOMCRYPT_CIPHER_ANUBIS`
- `TOMCRYPT_CIPHER_BLOWFISH`
- `TOMCRYPT_CIPHER_CAMELLIA`
- `TOMCRYPT_CIPHER_CAST5`
- `TOMCRYPT_CIPHER_CHACHA`
- `TOMCRYPT_CIPHER_DES`
- `TOMCRYPT_CIPHER_KASUMI`
- `TOMCRYPT_CIPHER_KHAZAD`
- `TOMCRYPT_CIPHER_MULTI2`
- `TOMCRYPT_CIPHER_NOEKEON`
- `TOMCRYPT_CIPHER_RC2`
- `TOMCRYPT_CIPHER_RC4`
- `TOMCRYPT_CIPHER_RC5`
- `TOMCRYPT_CIPHER_RC6`
- `TOMCRYPT_CIPHER_RIJNDAEL` (alias for `TOMCRYPT_CIPHER_AES`)
- `TOMCRYPT_CIPHER_SAFER128` (alias for `TOMCRYPT_CIPHER_SAFERSK128`)
- `TOMCRYPT_CIPHER_SAFER64` (alias for `TOMCRYPT_CIPHER_SAFERSK64`)
- `TOMCRYPT_CIPHER_SAFERK128`
- `TOMCRYPT_CIPHER_SAFERK64`
- `TOMCRYPT_CIPHER_SAFERPLUS`
- `TOMCRYPT_CIPHER_SAFERSK128`
- `TOMCRYPT_CIPHER_SAFERSK64`
- `TOMCRYPT_CIPHER_SOBER128`
- `TOMCRYPT_CIPHER_SEED`
- `TOMCRYPT_CIPHER_SKIPJACK`
- `TOMCRYPT_CIPHER_TRIPLEDES` (alias for `TOMCRYPT_CIPHER_3DES`)
- `TOMCRYPT_CIPHER_TWOFISH`
- `TOMCRYPT_CIPHER_XTEA`

The list of supported encryption/decryption modes can be retrieved through `tomcrypt_list_modes()`. The following constants are also provided:

- `TOMCRYPT_MODE_CBC`
- `TOMCRYPT_MODE_CCM`
- `TOMCRYPT_MODE_CFB`
- `TOMCRYPT_MODE_CHACHA20POLY1305`
- `TOMCRYPT_MODE_CTR`
- `TOMCRYPT_MODE_EAX`
- `TOMCRYPT_MODE_ECB`
- `TOMCRYPT_MODE_F8`
- `TOMCRYPT_MODE_GCM`
- `TOMCRYPT_MODE_LRW`
- `TOMCRYPT_MODE_OCB`
- `TOMCRYPT_MODE_OCB3`
- `TOMCRYPT_MODE_OFB`
- `TOMCRYPT_MODE_STREAM`
- `TOMCRYPT_MODE_XTS`

Note

`TOMCRYPT_MODE_STREAM` only works for stream ciphers (ie. `TOMCRYPT_CIPHER_RC4`, `TOMCRYPT_CIPHER_CHACHA` and `TOMCRYPT_CIPHER_SOBER128`). Likewise, these stream ciphers will not work with other modes.

### Decryption

[](#decryption)

Decryption works pretty much the same way encryption does:

```

```

Of course, for decryption to work properly, the same algorithm (cipher), mode and secret key should be used during encryption and decryption.

### Hashing

[](#hashing)

Hashing data can easily be done using the following code:

```

```

Use `tomcrypt_list_hashes()` to get a list of supported hashing algorithms. Like with ciphers, several constants are provided to refer to the various known hashing algorithms:

- `TOMCRYPT_HASH_BLAKE2B_160`
- `TOMCRYPT_HASH_BLAKE2B_256`
- `TOMCRYPT_HASH_BLAKE2B_384`
- `TOMCRYPT_HASH_BLAKE2B_512`
- `TOMCRYPT_HASH_BLAKE2S_128`
- `TOMCRYPT_HASH_BLAKE2S_160`
- `TOMCRYPT_HASH_BLAKE2S_224`
- `TOMCRYPT_HASH_BLAKE2S_256`
- `TOMCRYPT_HASH_MD2`
- `TOMCRYPT_HASH_MD4`
- `TOMCRYPT_HASH_MD5`
- `TOMCRYPT_HASH_RIPEMD128`
- `TOMCRYPT_HASH_RIPEMD160`
- `TOMCRYPT_HASH_RIPEMD256`
- `TOMCRYPT_HASH_RIPEMD320`
- `TOMCRYPT_HASH_SHA1`
- `TOMCRYPT_HASH_SHA224` (alias for `TOMCRYPT_HASH_SHA2_224`)
- `TOMCRYPT_HASH_SHA256` (alias for `TOMCRYPT_HASH_SHA2_256`)
- `TOMCRYPT_HASH_SHA384` (alias for `TOMCRYPT_HASH_SHA2_384`)
- `TOMCRYPT_HASH_SHA512` (alias for `TOMCRYPT_HASH_SHA2_512`)
- `TOMCRYPT_HASH_SHA512_224` (alias for `TOMCRYPT_HASH_SHA2_512_224`)
- `TOMCRYPT_HASH_SHA512_256` (alias for `TOMCRYPT_HASH_SHA2_512_256`)
- `TOMCRYPT_HASH_SHA2_224`
- `TOMCRYPT_HASH_SHA2_256`
- `TOMCRYPT_HASH_SHA2_384`
- `TOMCRYPT_HASH_SHA2_512`
- `TOMCRYPT_HASH_SHA2_512_224`
- `TOMCRYPT_HASH_SHA2_512_256`
- `TOMCRYPT_HASH_SHA3_224`
- `TOMCRYPT_HASH_SHA3_256`
- `TOMCRYPT_HASH_SHA3_384`
- `TOMCRYPT_HASH_SHA3_512`
- `TOMCRYPT_HASH_TIGER`
- `TOMCRYPT_HASH_WHIRLPOOL`

### Message Authentication Codes

[](#message-authentication-codes)

Generating a Message Authentication Code (MAC) can be done using the following code:

```

```

Use `tomcrypt_list_macs()` for a list of MAC algorithms supported by your platform. The following constants are also provided:

- `TOMCRYPT_MAC_BLAKE2B`
- `TOMCRYPT_MAC_BLAKE2S`
- `TOMCRYPT_MAC_CMAC`
- `TOMCRYPT_MAC_F9`
- `TOMCRYPT_MAC_HMAC`
- `TOMCRYPT_MAC_PELICAN`
- `TOMCRYPT_MAC_PMAC`
- `TOMCRYPT_MAC_POLY1305`
- `TOMCRYPT_MAC_XCBC`

Most of these MAC algorithms require an additional algorithm to be given:

- `TOMCRYPT_MAC_BLAKE2B`, `TOMCRYPT_MAC_BLAKE2S` and `TOMCRYPT_MAC_POLY1305`: no additional algorithm is necessary (i.e. you may pass `null` instead of an algorithm)
- `TOMCRYPT_MAC_HMAC`: some hashing algorithm must be passed
- other MAC algorithms: a cipher must be passed

Please refer to the documentation on [Encryption](#encryption) and [Hashing](#hashing) for more information about supported algorithms.

### (Pseudo-)Random Number Generators

[](#pseudo-random-number-generators)

This extension can provide you with data generated at random, as an alternative to [openssl\_random\_pseudo\_bytes()](http://php.net/openssl_random_pseudo_bytes).

The following code can be used to generate (pseudo-)random number generators:

```

```

Various algorithms of (pseudo-)random number generators are available:

- `TOMCRYPT_RNG_CHACHA20`
- `TOMCRYPT_RNG_FORTUNA`
- `TOMCRYPT_RNG_RC4`
- `TOMCRYPT_RNG_SECURE`
- `TOMCRYPT_RNG_SOBER128`
- `TOMCRYPT_RNG_YARROW`

Warning

Apart from `TOMCRYPT_RNG_SECURE` --- which is the default RNG used by `tomcrypt_rng_get_bytes()`, all the other generators are only PRNGs and should not be used when truly random data is required.

It is also possible to export/import the state of a random number generator (eg. to reseed a PRNG between restarts of the PHP interpreter or to get predictable outputs from the PRNG):

```

```

Note

Because `TOMCRYPT_RNG_SECURE` refers to the system's actual random number generator, it is not possible to export/import the state for that generator. Trying to do so will only result in an empty state being exported/imported.

Caveats
-------

[](#caveats)

The following is a list of currently known problems:

- For the time being, the API for Authenticated Encryption (using the `options` array to pass the expected tag during decryption) is a bit awkward to work with.
- The context used during encryption/decryption with stream ciphers is reinitialized between each operation, therefore making it unusable in any real streaming scenario.

License
-------

[](#license)

libtomcrypt is released under a dual public domain / [WTFPL](http://sam.zoy.org/wtfpl/) license.

php\_tomcrypt is released under version 3.01 of the [PHP](http://www.php.net/license/3_01.txt) license.

###  Health Score

25

—

LowBetter than 36% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity53

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 ~0 days

Total

2

Last Release

3515d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/3349e240558c2c63c2488ec7ddc04f623fad0adec6ff13ff4d58047b3facded4?d=identicon)[fpoirotte](/maintainers/fpoirotte)

---

Top Contributors

[![fpoirotte](https://avatars.githubusercontent.com/u/499919?v=4)](https://github.com/fpoirotte "fpoirotte (194 commits)")

### Embed Badge

![Health badge](/badges/fpoirotte-tomcrypt/health.svg)

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

PHPackages © 2026

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