mysqlAES
[ class tree: mysqlAES ] [ index: mysqlAES ] [ all elements ]

Source for file mysqlAES.php

Documentation is available at mysqlAES.php

  1. <?php
  2. declare (strict_types=1);
  3. declare (encoding='UTF-8');
  4.  
  5. /**
  6.  * Project: oops\Encrypt\mysqlAES :: MYSQL 호환 AES ENCRYPT/DECRYPT Class<br>
  7.  * File:    mysqlAES.php
  8.  *
  9.  * mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
  10.  * php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
  11.  *
  12.  * encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
  13.  * MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
  14.  *
  15.  * key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
  16.  * UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
  17.  *
  18.  * 의존성:
  19.  * <ul style="margin-left: 30px;">
  20.  *    <li>php >= 7.1<br>
  21.  *        PHP 7.1 이전 버전에서는 1.0 branch 를 사용하십시오.</li>
  22.  *    <li>openssl extension</li>
  23.  * </ul>
  24.  *
  25.  * 예제:
  26.  * {@example mysqlAES/test.php}
  27.  *
  28.  *
  29.  * @category    Encryption
  30.  * @package     mysqlAES
  31.  * @author      JoungKyun.Kim <http://oops.org>
  32.  * @copyright   (c) 2018, OOPS.org
  33.  * @license     BSD License
  34.  * @link        http://pear.oops.org/package/mysqlAES
  35.  * @since       File available since release 0.0.1
  36.  * @example     mysqlAES/test.php mysqlAES 예제
  37.  * @filesource
  38.  *
  39.  */
  40. namespace oops\Encrypt;
  41.  
  42.  
  43. /**
  44.  * mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
  45.  * php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
  46.  *
  47.  * encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
  48.  * MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
  49.  *
  50.  * key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
  51.  * UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
  52.  *
  53.  * 예제:
  54.  * {@example mysqlAES/test.php}
  55.  *
  56.  * @package mysqlAES
  57.  */
  58. Class mysqlAES {
  59.     // {{{ properties
  60.     /**
  61.      * AES block 사이즈
  62.      */
  63.     const AES_BLOCK_SIZE 16;
  64.     // }}}
  65.     
  66.     // {{{ +-- public __construct (void)
  67.     /**
  68.      * oops\Encrypt\mysqlAES 초기화
  69.      *
  70.      * @access public
  71.      */
  72.     function __construct (}
  73.     // }}}
  74.  
  75.     // {{{ +-- static public (string) hex (string $v = null)
  76.     /**
  77.      * Return a hexadecimal representation of a decimal or string value
  78.      *
  79.      * This method is compatible HEX function of mysql
  80.      *
  81.      * Example:
  82.      * {@example mysqlAES/test.php 22 1}
  83.      *
  84.      * @access public
  85.      * @return string hexadecimal data. If given parameter $v is empty, return null.
  86.      * @param  string original data
  87.      */
  88.     static public function hex (?string $v): ?string {
  89.         if $v )
  90.             return null;
  91.         $r strtoupper (bin2hex ($v));
  92.         return $r $r null;
  93.     }
  94.     // }}}
  95.  
  96.     // {{{ +-- static public (string) unhex (string $v = null)
  97.     /**
  98.      * Return a string containing hex representation of a number
  99.      *
  100.      * This method is compatible UNHEX function of mysql
  101.      *
  102.      * Example:
  103.      * {@example mysqlAES/test.php 26 1}
  104.      *
  105.      * @access public
  106.      * @return string Returns an ASCII string containing the hexadecimal representation.
  107.      *                 If given parameter $v is empty, return null.
  108.      * @param  string hexadecimal data
  109.      */
  110.     static public function unhex (?string $v): ?string {
  111.         if $v || is_string ($v) )
  112.             return null;
  113.  
  114.         $r hex2bin ($v);
  115.         return $r $r null;
  116.     }
  117.     // }}}
  118.  
  119.     // {{{ +-- static private (string) _encrypt (string $cipher, string $key)
  120.     /**
  121.      * skeleton encrypt function
  122.      * @access private
  123.      * @return string encrypted data by AES
  124.      * @param  string The plaintext message data to be encrypted.
  125.      * @param  string The key for encryption
  126.      */
  127.     static private function _encrypt (string $cipherstring $key): ?string {
  128.         $keylen strlen ($key);
  129.         if $keylen <= 16 )
  130.             $method 'AES-128-ECB';
  131.         else if $keylen <= 24 )
  132.             $method 'AES-192-ECB';
  133.         else
  134.             $method 'AES-256-ECB';
  135.         return openssl_encrypt ($cipher$method$keyOPENSSL_RAW_DATA OPENSSL_ZERO_PADDING);
  136.     }
  137.     // }}}
  138.  
  139.     // {{{ +-- static public (string) encrypt (string $cipher = null, string $key = null)
  140.     /**
  141.      * Encrypt using AES
  142.      *
  143.      * This method is compatible AES_ENCRYPT function of mysql, if key is 128 bit.
  144.      * And then, If key is 192 or 256 bit, this method is compatible follow APIS:
  145.      *  - {@link http://mirror.oops.org/pub/oops/mysql/lib_mysqludf_aes256/ MySQL UDF lib_mysqludf_aes256}
  146.      *  - {@link http://mirror.oops.org/pub/oops/javascript/mysqlAES/ Javascript mysqlAES class}
  147.      *
  148.      * Example:
  149.      * {@example mysqlAES/test.php}
  150.      *
  151.      * @access public
  152.      * @return string encrypted data by AES. If $cipyer or $key has empty value, return null
  153.      * @param  string The plaintext message data to be encrypted.
  154.      * @param  string encryption key
  155.      *    - 128bit : 16 byte string
  156.      *    - 192bit : 24 byte string
  157.      *    - 256bit : 32 byte string
  158.      */
  159.     static public function encrypt (?string $cipher?string $key): ?string {
  160.         if $cipher || $key )
  161.             return null;
  162.  
  163.         $blocks self::AES_BLOCK_SIZE (floor (strlen ($cipherself::AES_BLOCK_SIZE1);
  164.         $padlen = (int) $blocks strlen ($cipher);
  165.  
  166.         $cipher .= str_repeat (chr ($padlen)$padlen);
  167.  
  168.         $r self::_encrypt ($cipher$key);
  169.         return $r $r null;
  170.     }
  171.     // }}}
  172.  
  173.     // {{{ +-- static private (string) _decrypt (string $cipher, string $key)
  174.     /**
  175.      * skeleton encrypt function
  176.      * @access private
  177.      * @return string encrypted data by AES
  178.      */
  179.     static private function _decrypt (string $cipherstring $key): ?string {
  180.         $keylen strlen ($key);
  181.         if $keylen <= 16 )
  182.             $method 'AES-128-ECB';
  183.         else if $keylen <= 24 )
  184.             $method 'AES-192-ECB';
  185.         else
  186.             $method 'AES-256-ECB';
  187.         return openssl_decrypt ($cipher$method$keyOPENSSL_RAW_DATA OPENSSL_ZERO_PADDING);
  188.     }
  189.     // }}}
  190.  
  191.     // {{{ +-- static public (string) decrypt (string $cipher = null, string $key = null)
  192.     /**
  193.      * Decrypt using AES
  194.      *
  195.      * This method is compatible AES_DECRYPT function of mysql, if key is 128 bit
  196.      * And then, If key is 192 or 256 bit, this method is compatible follow APIS:
  197.      *  - {@link http://mirror.oops.org/pub/oops/mysql/lib_mysqludf_aes256/ MySQL UDF lib_mysqludf_aes256}
  198.      *  - {@link http://mirror.oops.org/pub/oops/javascript/mysqlAES/ Javascript mysqlAES class}
  199.      *
  200.      * Example:
  201.      * {@example mysqlAES/test.php}
  202.      *
  203.      * @access public
  204.      * @return string decrypted data by AES. If $cipyer or $key has empty value, return null.
  205.      * @param  string cipher data for being decryption
  206.      * @param  string decryption key
  207.      *    - 128bit : 16 byte string
  208.      *    - 192bit : 24 byte string
  209.      *    - 256bit : 32 byte string
  210.      */
  211.     static public function decrypt (?string $cipher?string $key): ?string {
  212.         if $cipher || $key )
  213.             return null;
  214.  
  215.         if ($r self::_decrypt ($cipher$key)) )
  216.             return null;
  217.         $last $r[strlen ($r1];
  218.         $r substr ($r0strlen($rord($last));
  219.         return $r;
  220.     }
  221.     // }}}
  222. }
  223.  
  224. // {{{ +-- public mysqlAES_REQUIRES (void)
  225. /**
  226.  * mysqlAES 패키지에서 필요한 의존성을 검사한다.
  227.  *
  228.  * @access public
  229.  * @return void 
  230.  */
  231. function mysqlAES_REQUIRES ({
  232.     if extension_loaded ('openssl') ) {
  233.         throw new \Exception (
  234.             __NAMESPACE__ . '\mysqlAES class must need openssl extension',
  235.             E_USER_ERROR
  236.         );
  237.     }
  238. }
  239. // }}}
  240.  
  241. ?>

Documentation generated on Tue, 14 May 2019 02:00:38 +0900 by phpDocumentor 1.4.4