Cipher
in package
Cipher utility.
Table of Contents
Methods
- decrypt() : string
- Decryption.
- encode_sha256() : string
- Encode with SHA-256.
- encodeOpenSshPublicKey() : string
- Encode OpenSSH public key.
- encrypt() : string
- Encryption.
- generateInitialVector() : string
- Generate IV.
- generateKey() : string
- Generate a random key.
- generateKeyPair() : void
- Generate OpenSSL Key Pair ```php use \X\Util\Cipher;
- randStr() : string
- Generate a random string.
- randToken68() : string
- Generate a random token68 string.
- encodeOpenSshBuffer() : string
- OpenSSH encode the buffer.
Methods
decrypt()
Decryption.
public
static decrypt(string $encrypted, string $key, string $iv[, string $algorithm = 'AES-256-CTR' ]) : string
use \X\Util\Cipher;
$iv = Cipher::generateInitialVector();
$plaintext = 'Hello, World.';
$key = 'key';
$encrypted = Cipher::encrypt($plaintext, $key, $iv);// UHLY5PckT7Da02e42g==
$decrypted = Cipher::decrypt($encrypted, $key, $iv);// Hello, World.
Parameters
- $encrypted : string
-
Encrypted string.
- $key : string
-
Decryption key.
- $iv : string
-
IV.
- $algorithm : string = 'AES-256-CTR'
-
(optional) Cryptographic Algorithm. Default is "AES-256-CTR".
Return values
string —Decrypted string.
encode_sha256()
Encode with SHA-256.
public
static encode_sha256(string $plaintext[, string|null $key = null ]) : string
Parameters
- $plaintext : string
-
String to be hashed.
- $key : string|null = null
-
(optional) Encoding key. If not specified, get it from "encryption_key" in "application/config/config.php".
Return values
stringencodeOpenSshPublicKey()
Encode OpenSSH public key.
public
static encodeOpenSshPublicKey(string $privateKey) : string
Parameters
- $privateKey : string
-
Private Key.
Return values
string —SSH-encoded Public key.
encrypt()
Encryption.
public
static encrypt(string $plaintext, string $key, string $iv[, string $algorithm = 'AES-256-CTR' ]) : string
use \X\Util\Cipher;
$iv = Cipher::generateInitialVector();
$plaintext = 'Hello, World.';
$key = 'key';
$encrypted = Cipher::encrypt($plaintext, $key, $iv);// UHLY5PckT7Da02e42g==
$decrypted = Cipher::decrypt($encrypted, $key, $iv);// Hello, World.
Parameters
- $plaintext : string
-
String to be encrypted.
- $key : string
-
Encryption key.
- $iv : string
-
IV.
- $algorithm : string = 'AES-256-CTR'
-
(optional) Cryptographic Algorithm. Default is "AES-256-CTR".
Return values
string —Encrypted string.
generateInitialVector()
Generate IV.
public
static generateInitialVector([string $algorithm = 'AES-256-CTR' ]) : string
Parameters
- $algorithm : string = 'AES-256-CTR'
-
(optional) Cryptographic Algorithm. Default is "AES-256-CTR".
Return values
string —IV.
generateKey()
Generate a random key.
public
static generateKey([int $len = 32 ]) : string
Parameters
- $len : int = 32
-
(optional) Key length. Default is 32.
Return values
string —Key.
generateKeyPair()
Generate OpenSSL Key Pair ```php use \X\Util\Cipher;
public
static generateKeyPair(string &$privateKey, string &$publicKey[, array<string|int, mixed> $options = [] ]) : void
// Generate 4096bit long RSA key pair. Cipher::generateKeyPair($privateKey, $publicKey, [ 'digest_alg' => 'sha512', 'private_key_bits' => 4096, 'private_key_type' => OPENSSL_KEYTYPE_RSA ]);
// Debug private key. // Output: -----BEGIN PRIVATE KEY----- // MIIJQgIBADANBgkqhkiG9w0BAQEFAASCCSwwggkoAgEAAoICAQCpvdXUNEfrA4T+ // ... // -----END PRIVATE KEY----- echo 'Private key:'. PHP_EOL . $privateKey;
// Debug public key. // Output: -----BEGIN PUBLIC KEY----- // MIICIjANBgkqhkiG9w0BAQEFAAOCAg8AMIICCgKCAgEAqb3V1DRH6wOE/oVhJWEo // ... // -----END PUBLIC KEY----- echo 'Public key:' . PHP_EOL. $publicKey;
// OpenSSH encode the public key. // Output: ssh-rsa AAAAB3NzaC... $publicKey = Cipher::encodeOpenSshPublicKey($privateKey);
// Debug OpenSSH-encoded public key. echo 'OpenSSH-encoded public key:' . PHP_EOL . $publicKey;
Parameters
- $privateKey : string
-
The generated private key is set.
- $publicKey : string
-
The generated public key is set.
- $options : array<string|int, mixed> = []
randStr()
Generate a random string.
public
static randStr([int $len = 64 ][, string $chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ]) : string
Parameters
- $len : int = 64
-
(optional) Characters. Default is 64.
- $chars : string = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
-
(optional) Characters to be used. Default is one-byte alphanumeric characters.
Return values
string —Random string.
randToken68()
Generate a random token68 string.
public
static randToken68([int $len = 64 ]) : string
Parameters
- $len : int = 64
-
(optional) Characters. Default is 64.
Return values
string —token68 string.
encodeOpenSshBuffer()
OpenSSH encode the buffer.
private
static encodeOpenSshBuffer(string $buffer) : string
Parameters
- $buffer : string
-
buffer.
Return values
string —SSH encoded buffer.