inanepain/crypt

Encryption helpers and password hashers.

Maintainers

Package info

github.com/inanepain/crypt

Issues

pkg:composer/inanepain/crypt

Statistics

Installs: 0

Dependents: 0

Suggesters: 0

Stars: 1

0.1.0 2022-07-26 17:06 UTC

This package is auto-updated.

Last update: 2026-04-15 10:39:13 UTC


README

Table of Contents

icon inanepain/crypt

Encryption helpers and password hashers.

1. Install

composer
composer require inanepain/crypt

2. Crypt

This is meant as more of an introduction to the classes found in the Inane\Crypt namespace and not an in-depth guide.

Classes

3. Secret

The Secret class is a helper for encrypting and decrypting strings using OpenSSL. It also provides methods to encode the encrypted data into a URL-safe format.

3.1. Properties

properties
  • protected string $passphrase

  • protected string $cipher

  • protected ?string $iv

3.2. Methods

methods
  • public function __construct(string $passphrase, string $cipher = 'aes-256-ctr', ?string $iv = null)

  • public function encrypt(string $plainText): string

  • public function decrypt(string $encryptedText): string

  • public function encode(string $encryptedText): string

  • public function decode(string $encodedText): string

  • public function encryptEncode(string $plainText): string

  • public function decryptDecode(string $encryptedEncodedText): string

3.2.1. Usage

To use the Secret class, you need to instantiate it with a passphrase (minimum 16 characters).

Encryption and Decryption
use Inane\Crypt\Secret;

$passphrase = 'a-very-secret-phrase-of-at-least-16-chars';
$secret = new Secret($passphrase);

$originalText = 'Hello World!';

// Encrypt
$encrypted = $secret->encrypt($originalText);

// Decrypt
$decrypted = $secret->decrypt($encrypted);

echo $decrypted; // Hello World!

3.2.2. URL Safe Encoding

Encrypted data often contains characters that are not safe for URLs (like +, /, =). The encode and decode methods handle this by replacing them with URL-safe alternatives.

URL Safe Encryption
$secret = new Secret($passphrase);

$originalText = 'Sensitive Data';

// Encrypt and Encode in one go
$urlSafeEncrypted = $secret->encryptEncode($originalText);

// ... later ...

// Decode and Decrypt
$decrypted = $secret->decryptDecode($urlSafeEncrypted);

echo $decrypted; // Sensitive Data

3.2.3. Custom Cipher and IV

By default, it uses aes-256-ctr. You can specify a different cipher or provide your own IV.

Custom Cipher
$secret = new Secret($passphrase, 'aes-128-cbc', 'my-custom-iv-123');