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

Source for file mysqlAES.php

Documentation is available at mysqlAES.php

  1. <?php
  2. /**
  3.  * Project: mysqlAES :: MYSQL 호환 AES ENCRYPT/DECRYPT Class<br>
  4.  * File:    mysqlAES.php
  5.  *
  6.  * mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
  7.  * php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
  8.  *
  9.  * encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
  10.  * MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
  11.  *
  12.  * key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
  13.  * UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
  14.  *
  15.  * 예제:
  16.  * {@example mysqlAES/test.php}
  17.  *
  18.  *
  19.  * @category    Encryption
  20.  * @package     mysqlAES
  21.  * @author      JoungKyun.Kim <http://oops.org>
  22.  * @copyright   (c) 2018, OOPS.org
  23.  * @license     BSD License
  24.  * @link        http://pear.oops.org/package/mysqlAES
  25.  * @since       File available since release 0.0.1
  26.  * @example     mysqlAES/test.php mysqlAES 예제
  27.  * @filesource
  28.  *
  29.  */
  30.  
  31. /**
  32.  * mysqlAES 패키지는 MySQL의 AES_EMCRYPT, AES_DECRYPT, HEX, UNHEX 함수를
  33.  * php에서 호환되게 사용할 수 있도록 하는 기능을 제공한다.
  34.  *
  35.  * encrypt method와 decrypt method의 경우, key 길이가 128bit(16byte)이면
  36.  * MySQL과 MariaDB의 AES_ENCRYPT/AES_DECRYPT 함수와 완벽하게 호환이 된다.
  37.  *
  38.  * key 길이가 192또는 256bit일 경우에는 oops에서 제공하는 lib_mysqludf_aes256
  39.  * UDF에서 제공하는 AES256_ENCRYPT, AES256_DECRYPT와 완변하게 호환이 된다.
  40.  *
  41.  * 예제:
  42.  * {@example mysqlAES/test.php}
  43.  *
  44.  * @package mysqlAES
  45.  */
  46. Class mysqlAES {
  47.     // {{{ properties
  48.     /**
  49.      * AES block 사이즈
  50.      */
  51.     const AES_BLOCK_SIZE 16;
  52.     // }}}
  53.     
  54.     /**
  55.      * Variables for separating mcrypt or openssl extensions.
  56.      * mcrypt takes precedence over openssl.
  57.      * @access public
  58.      * @var string 
  59.      */
  60.     static public $extname = null;
  61.  
  62.     // {{{ +-- public __construct (void)
  63.     /**
  64.      * mysqlAES 초기화
  65.      *
  66.      * @access public
  67.      */
  68.     function __construct ({
  69.         if extension_loaded ('mcrypt') )
  70.             $this->extname = 'mcrypt';
  71.         else if extension_loaded ('openssl') )
  72.             $this->extname = 'openssl';
  73.     }
  74.     // }}}
  75.  
  76.     // {{{ +-- static public (string) hex ($v)
  77.     /**
  78.      * Return a hexadecimal representation of a decimal or string value
  79.      *
  80.      * This method is compatible HEX function of mysql
  81.      *
  82.      * Example:
  83.      * {@example mysqlAES/test.php 16 1}
  84.      *
  85.      * @access public
  86.      * @return string hexadecimal data. If given parameter $v is empty, return null.
  87.      * @param  string original data
  88.      */
  89.     static public function hex ($v{
  90.         if $v )
  91.             return null;
  92.         return strtoupper (bin2hex ($v));
  93.     }
  94.     // }}}
  95.  
  96.     // {{{ +-- static public (string) unhex ($v)
  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 19 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 ($v{
  111.         return self::hex2bin ($v);
  112.     }
  113.     // }}}
  114.  
  115.     // {{{ +-- static private (string) _encrypt ($cipher, $key)
  116.     /**
  117.      * skeleton encrypt function
  118.      * @access private
  119.      * @return string encrypted data by AES
  120.      * @param  string The plaintext message data to be encrypted.
  121.      * @param  string The key for encryption
  122.      */
  123.     static private function _encrypt ($cipher$key{
  124.         if self::$extname == 'mcrypt' )
  125.             return mcrypt_encrypt (MCRYPT_RIJNDAEL_128$key$cipherMCRYPT_MODE_ECB);
  126.         else {
  127.             $keylen strlen ($key);
  128.             if $keylen <= 16 )
  129.                 $method 'AES-128-ECB';
  130.             else if $keylen <= 24 )
  131.                 $method 'AES-192-ECB';
  132.             else
  133.                 $method 'AES-256-ECB';
  134.             return openssl_encrypt ($cipher$method$keyOPENSSL_RAW_DATA OPENSSL_ZERO_PADDING);
  135.         }
  136.     }
  137.     // }}}
  138.  
  139.     // {{{ +-- static public (string) encrypt ($cipher, $key)
  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 ($cipher$key{
  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 null $r;
  170.     }
  171.     // }}}
  172.  
  173.     // {{{ +-- static private (string) _decrypt ($cipher, $key)
  174.     /**
  175.      * skeleton encrypt function
  176.      * @access private
  177.      * @return string encrypted data by AES
  178.      */
  179.     static private function _decrypt ($cipher$key{
  180.         if self::$extname == 'mcrypt' {
  181.             return mcrypt_decrypt (MCRYPT_RIJNDAEL_128$key$cipherMCRYPT_MODE_ECB);
  182.         else {
  183.             $keylen strlen ($key);
  184.             if $keylen <= 16 )
  185.                 $method 'AES-128-ECB';
  186.             else if $keylen <= 24 )
  187.                 $method 'AES-192-ECB';
  188.             else
  189.                 $method 'AES-256-ECB';
  190.             return openssl_decrypt ($cipher$method$keyOPENSSL_RAW_DATA OPENSSL_ZERO_PADDING);
  191.         }
  192.     }
  193.     // }}}
  194.  
  195.     // {{{ +-- static public (string) decrypt ($cipher, $key)
  196.     /**
  197.      * Decrypt using AES
  198.      *
  199.      * This method is compatible AES_DECRYPT function of mysql, if key is 128 bit
  200.      * And then, If key is 192 or 256 bit, this method is compatible follow APIS:
  201.      *  - {@link http://mirror.oops.org/pub/oops/mysql/lib_mysqludf_aes256/ MySQL UDF lib_mysqludf_aes256}
  202.      *  - {@link http://mirror.oops.org/pub/oops/javascript/mysqlAES/ Javascript mysqlAES class}
  203.      *
  204.      * Example:
  205.      * {@example mysqlAES/test.php}
  206.      *
  207.      * @access public
  208.      * @return string decrypted data by AES. If $cipyer or $key has empty value, return null.
  209.      * @param  string cipher data for being decryption
  210.      * @param  string decryption key
  211.      *    - 128bit : 16 byte string
  212.      *    - 192bit : 24 byte string
  213.      *    - 256bit : 32 byte string
  214.      */
  215.     static public function decrypt ($cipher$key{
  216.         if $cipher || $key )
  217.             return null;
  218.  
  219.         if ($r self::_decrypt ($cipher$key)) )
  220.             return null;
  221.         $last $r[strlen ($r1];
  222.         $r substr ($r0strlen($rord($last));
  223.         return $r;
  224.     }
  225.     // }}}
  226.  
  227.     // {{{ +-- private (string) hex2bin ($v)
  228.     /**
  229.      * Decodes a hexadecimally encoded binary string
  230.      *
  231.      * Support hex2bin function if php version is less than 5.4.0.
  232.      *
  233.      * @access public
  234.      * @return string Returns the binary representation of the given data or null on failure.
  235.      * @param  string Hexadecimal representation of data.
  236.      */
  237.     private function hex2bin ($v{
  238.         if $v || is_string ($v) )
  239.             return null;
  240.  
  241.         if function_exists ('hex2bin') ) {
  242.             $r hex2bin ($v);
  243.             return !$r null $r;
  244.         }
  245.  
  246.         $len strlen ($v);
  247.         for $i=0$i<$len$i+={
  248.             $r .= chr (hexdec ($v{$i$v{($i+1)}));
  249.         }
  250.  
  251.         return $r;
  252.     }
  253.     // }}}
  254. }
  255.  
  256. // {{{ +-- public mysqlAES_REQUIRES (void)
  257. /**
  258.  * mysqlAES 패키지에서 필요한 의존성을 검사한다.
  259.  *
  260.  * @access public
  261.  * @return void 
  262.  */
  263. function mysqlAES_REQUIRES ({
  264.     if extension_loaded ('mcrypt') )
  265.         mysqlAES::$extname 'mcrypt';
  266.     else if extension_loaded ('openssl') )
  267.         mysqlAES::$extname 'openssl';
  268.     else
  269.         throw new Exception ('mysqlAES class must need mcrypt or openssl extension'E_USER_ERROR);
  270. }
  271. // }}}
  272.  
  273. ?>

Documentation generated on Sun, 25 Feb 2018 07:14:52 +0900 by phpDocumentor 1.4.4