2019-05-09 18:54:39 +12:00
|
|
|
<?php
|
|
|
|
|
2020-03-25 06:56:32 +13:00
|
|
|
namespace Appwrite\OpenSSL;
|
2019-05-09 18:54:39 +12:00
|
|
|
|
|
|
|
class OpenSSL
|
|
|
|
{
|
|
|
|
const CIPHER_AES_128_GCM = 'aes-128-gcm';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $method
|
|
|
|
* @param $key
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param int $options
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param string $iv
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param null $tag
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param string $aad
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param int $tag_length
|
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function encrypt($data, $method, $key, $options = 0, $iv = '', &$tag = null, $aad = '', $tag_length = 16)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \openssl_encrypt($data, $method, $key, $options, $iv, $tag, $aad, $tag_length);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $data
|
|
|
|
* @param $method
|
|
|
|
* @param $password
|
2019-09-07 05:04:26 +12:00
|
|
|
* @param int $options
|
2019-05-09 18:54:39 +12:00
|
|
|
* @param string $iv
|
|
|
|
* @param string $tag
|
|
|
|
* @param string $aad
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return string
|
|
|
|
*/
|
2019-09-30 19:13:40 +13:00
|
|
|
public static function decrypt($data, $method, $password, $options = 1, $iv = '', $tag = '', $aad = '')
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \openssl_decrypt($data, $method, $password, $options, $iv, $tag, $aad);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $method
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2019-05-09 18:54:39 +12:00
|
|
|
* @return int
|
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function cipherIVLength($method)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \openssl_cipher_iv_length($method);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $length
|
|
|
|
* @param null $crypto_strong
|
2019-09-07 05:04:26 +12:00
|
|
|
*
|
2020-10-27 13:08:29 +13:00
|
|
|
* @return false|string
|
2019-05-09 18:54:39 +12:00
|
|
|
*/
|
2019-09-07 05:04:26 +12:00
|
|
|
public static function randomPseudoBytes($length, &$crypto_strong = null)
|
2019-05-09 18:54:39 +12:00
|
|
|
{
|
2020-06-20 23:05:43 +12:00
|
|
|
return \openssl_random_pseudo_bytes($length, $crypto_strong);
|
2019-05-09 18:54:39 +12:00
|
|
|
}
|
2019-09-07 05:04:26 +12:00
|
|
|
}
|