ialopezg/encryption

PHP 加密库

v0.0.1 2020-09-24 01:10 UTC

This package is auto-updated.

Last update: 2024-09-24 09:41:41 UTC


README

Version License Total downloads

PHP 密码学库

目录

安装

  1. 包管理器

    • Composer
      composer require ialopezg/encryption
  2. 手动

    • Github
      git clone https://github.com/ialopezg/encryption

特性

需求

  • PHP v5.6+
  • ext-mbstring PHP 扩展
  • ext-openssl PHP 扩展
  • ialopezg/core v0.0.1+
  • ialopezg/logger v0.0.4+

使用说明

迭代器对象

方法

方法详情

方法: createdKey()
/**
 * Creates a random key with the length specified. Optional the result could be returned with capital letters.
 *
 * @param int $length       Output length.
 * @param bool $capitalize  Whether if the result key will be returned with capital letters.
 *
 * @throws Exception If it was not possible to gather sufficient entropy.
 * @return string A random key.
 */
public static function createKey($length, $capitalize = false): string

示例

$key = \ialopezg\Libraries\Encryption\Password::createKey(8, true);

// Display the encryption key string
echo "Encryption key: <strong>{$key}</strong><br>";
方法: getOption()
/**
 * Gets an option value. If value does not exists will return the default value.
 *
 * @param string $key Option key name.
 * @param null $default Option default value.
 *
 * @return mixed
 */
public function getOption($key, $default = null): string

示例

$encrypter->setOption('key', Password::createKey(8, true);

// Display the encryption key string
echo "Encryption key: <strong>{$encrypter->getOption('key')}</strong><br>";
方法: hash()
/**
 * Encrypts a simple text password into a ciphered password.
 * 
 * @param string $password Plain text password.
 * 
 * @return string Ciphered password.
 * @throws Exception If data provided is not a string.
 */
public function hash($password): string

示例

$encrypter = new \ialopezg\Libraries\Encryption\Password([
    'cipher' => 'AES-128-CTR',
    'digest' => 'SHA512',
    'options' => OPENSSL_CIPHER_RC2_40,
    'key' => 'YOUR_SECRET_KEY'
]);
$encrypted_password = $encrypter->encrypt($password);

// Display the encrypted string
echo "Encrypted password: <strong>{$encrypted_password}</strong><br>";
方法: setOption()
/**
 * Sets an option value.
 *
 * @param string $key Option key name.
 * @param mixed $value Option value.
 *
 * @return void
 */
public function setOption($key, $value): string

示例

$encrypter = new \ialopezg\Libraries\Encryption\Password();
$encrypter->setOption('key', $key);
方法: verify()
/**
 * Verifies if a hashed password is equal to the plain tex provided.
 *
 * @param string $password Plain text password.
 * @param string $hash Ciphered text
 *
 * @return bool true If password equals to hash, false otherwise.
 * @throws Exception If data provided is not a string.
 */
public function verify($password, $hash): bool

示例

$encrypter = new \ialopezg\Libraries\Encryption\Password();

$password = 'YOUR_PASSWORD';
$encrypted_password = 'YOUR_ENCRYPTED_PASSWORD';

// Display the encrypted string
echo 'Password are equals: <strong>' . ($encrypter->verify($password, $encrypted_password) ? 'true' : 'false') . '</strong>';

更多示例或选项,请参阅 examples 目录。要查看实时示例,请运行

### linux bash
./server.sh

### windows prompt
server.bat

许可证

此项目采用 MIT 许可证。更多信息请参阅 LICENSE

版权所有 (c) Isidro A. López G.