本文整理汇总了PHP中openssl_get_cipher_methods函数的典型用法代码示例。如果您正苦于以下问题:PHP openssl_get_cipher_methods函数的具体用法?PHP openssl_get_cipher_methods怎么用?PHP openssl_get_cipher_methods使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了openssl_get_cipher_methods函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: setCipher
/**
* @inheritdoc
*/
public function setCipher($cipher)
{
if (!in_array($cipher, openssl_get_cipher_methods())) {
throw new EncryptionException("Invalid cipher \"{$cipher}\"");
}
$this->cipher = $cipher;
}
示例2: setEncryptionMode
public function setEncryptionMode($mode = 'cbc', $strength = 128)
{
static $availableAlgorithms = null;
static $defaultAlgo = 'aes-128-cbc';
if (!is_array($availableAlgorithms)) {
$availableAlgorithms = openssl_get_cipher_methods();
foreach (array('aes-256-cbc', 'aes-256-ecb', 'aes-192-cbc', 'aes-192-ecb', 'aes-128-cbc', 'aes-128-ecb') as $algo) {
if (in_array($algo, $availableAlgorithms)) {
$defaultAlgo = $algo;
break;
}
}
}
$strength = (int) $strength;
$mode = strtolower($mode);
if (!in_array($strength, array(128, 192, 256))) {
$strength = 256;
}
if (!in_array($mode, array('cbc', 'ebc'))) {
$mode = 'cbc';
}
$algo = 'aes-' . $strength . '-' . $mode;
if (!in_array($algo, $availableAlgorithms)) {
$algo = $defaultAlgo;
}
$this->method = $algo;
}
示例3: testSetAlgorithm
public function testSetAlgorithm()
{
$algos = openssl_get_cipher_methods(true);
$algo = $algos[array_rand($algos)];
$this->crypt->setAlgorithm($algo);
$this->assertEquals($algo, $this->crypt->getAlgorithm());
}
示例4: testSupportedCiphersList
public function testSupportedCiphersList()
{
$supported = $this->defaultCipher->getSupportedCiphers();
$this->assertInternalType('array', $supported);
$cipherMethods = openssl_get_cipher_methods();
foreach ($supported as $method) {
$this->assertTrue(in_array($method, $cipherMethods));
}
}
示例5: __construct
public function __construct(array $options = [])
{
if (!empty($options)) {
$this->config = $options;
}
if (!\in_array($this->config['cipher'] . '-ctr', \openssl_get_cipher_methods())) {
throw new Error('Cipher ' . $this->config['cipher'] . '-ctr' . ' not found!');
}
}
示例6: __construct
/**
* EncryptedPrivateKey constructor.
* @param PrivateKeyInterface $key
* @param string $method
* @param string $iv
*/
public function __construct(PrivateKeyInterface $key, $method, $iv)
{
$methods = openssl_get_cipher_methods();
if (!in_array($method, array_values($methods))) {
throw new \RuntimeException('Unknown cipher method');
}
$this->key = $key;
$this->method = $method;
$this->iv = $iv;
}
示例7: encrypt
/**
* Encrypt the data
*
* @param string $data Data string to encrypt
* @param string $key Encryption key
* @param string $mode Default mode (openssl or mcrypt)
*
* @return string Encrypted data string.
*/
public function encrypt($data, $key, $mode = 'openssl')
{
if ($mode == 'openssl' && extension_loaded('openssl') && in_array('aes-256-cbc', openssl_get_cipher_methods())) {
return $this->encryptOpenSsl($data, $key);
}
if ($mode != 'raw' && function_exists('mcrypt_create_iv') && mcrypt_get_cipher_name(MCRYPT_RIJNDAEL_128) !== false) {
return $this->encryptMcrypt($data, $key);
}
throw new EncException('Either "openssl" PHP extension with "aes-256-cbc" cypher' . ' or "mcrypt" PHP extension with "MCRYPT_RIJNDAEL_128" cypher is required for AES encryption');
}
示例8: __construct
public function __construct(string $cipher = null, string $hash = null, string $mode = null, bool $twoStep = false)
{
parent::__construct($cipher, $hash, $mode, $twoStep);
$this->mCipher = $cipher ?? "AES-256";
$this->mMode = $mode ?? "CBC";
if (!function_exists("openssl_get_cipher_methods")) {
throw new Exception("Could not find the OpenSSL module");
} elseif (!in_array(strtoupper($this->mCipher . "-" . $this->mMode), openssl_get_cipher_methods())) {
throw new Exception("The cipher '" . strtoupper($this->mCipher . "-" . $this->mMode) . "' is not supported by this platform installation");
}
}
示例9: getMethod
/**
* Select which method we will use for our crypto stuff
*
* @return string The selected method that is available
*/
private function getMethod()
{
$availableMethods = openssl_get_cipher_methods();
if (in_array('AES-256-CBC-HMAC-SHA256', $availableMethods)) {
return 'AES-256-CBC-HMAC-SHA256';
} elseif (in_array('AES-256-CBC', $availableMethods)) {
return 'AES-256-CBC';
}
// just take the first one coming, it's better than nothing
return $availableMethods[0];
}
示例10: __construct
public function __construct($password, $method = null)
{
$this->_password = $password;
if ($method) {
$methods = openssl_get_cipher_methods(true);
if (!in_array($method, $methods)) {
throw new \LogicException('Unknown cipher algorithm');
}
$this->_method = $method;
}
}
示例11: __construct
/**
* EncryptedSubjectIdentifier constructor.
*
* @param string $pairwise_encryption_key
* @param string $algorithm
* @param null|string $iv
* @param string $salt
*/
public function __construct($pairwise_encryption_key, $algorithm, $iv, $salt)
{
Assertion::nullOrString($iv);
Assertion::string($salt);
Assertion::string($pairwise_encryption_key);
Assertion::string($algorithm);
Assertion::inArray($algorithm, openssl_get_cipher_methods(), sprintf('The algorithm "%s" is not supported.', $algorithm));
$this->pairwise_encryption_key = $pairwise_encryption_key;
$this->algorithm = $algorithm;
$this->salt = $salt;
$this->iv = $iv;
}
示例12: getSupportedAlgs
public function getSupportedAlgs()
{
$ciphers = openssl_get_cipher_methods();
$hashes = hash_algos();
$results = array();
foreach (self::$alg_params as $alg => $param) {
if (in_array($param['cipher'], $ciphers) && in_array($param['hash'], $hashes)) {
$results[] = $alg;
}
}
return $results;
}
示例13: _getCipherMethod
/**
* Get cipher method and verify that it's supported.
*
* @throws \RuntimeException
* @return string
*/
protected final function _getCipherMethod()
{
static $supported_ciphers;
if (!isset($supported_ciphers)) {
$supported_ciphers = array_flip(openssl_get_cipher_methods());
}
$method = $this->_cipherMethod();
if (!isset($supported_ciphers[$method])) {
throw new \RuntimeException("Cipher method {$method} is not" . " supported by this version of OpenSSL.");
}
return $method;
}
示例14: setMethod
private function setMethod()
{
// select the right method
$this->availableMethods = openssl_get_cipher_methods();
if (in_array('AES-256-CBC-HMAC-SHA256', $this->availableMethods)) {
$this->method = 'AES-256-CBC-HMAC-SHA256';
} elseif (in_array('AES-256-CBC', $this->availableMethods)) {
$this->method = 'AES-256-CBC';
} else {
// just take the first one coming, I guess it's better than nothing.
$this->method = $this->availableMethods[0];
}
}
示例15: check_methods
/**
* checks if the encryption and hash methods are supported
* @param $enc the encryption method.
* @param $hash the hash method.
* @throw Exception in case a method is not supported.
*/
private static function check_methods($enc, $hash)
{
if (!function_exists('openssl_encrypt')) {
throw new Exception('openssl_encrypt() not supported.');
} else {
if (!in_array($enc, openssl_get_cipher_methods())) {
throw new Exception('Encryption method ' . $enc . ' not supported.');
} else {
if (!in_array(strtolower($hash), hash_algos())) {
throw new Exception('Hashing method ' . $hash . ' not supported.');
}
}
}
}