码迷,mamicode.com
首页 > Web开发 > 详细

[李景山php]每天laravel-20160828|McryptEncrypter-2

时间:2016-06-13 11:56:50      阅读:166      评论:0      收藏:0      [点我收藏+]

标签:function   return   public   

/**
 * Determine if the given key and cipher combination is valid.
 *
 * @param  string  $key
 * @param  string  $cipher
 * @return bool
 */
public static function supported($key, $cipher)
{// check key and cipher is valid
    return defined(‘MCRYPT_RIJNDAEL_128‘) &&
            ($cipher === MCRYPT_RIJNDAEL_128 || $cipher === MCRYPT_RIJNDAEL_256);
}// if defined this 128 so we can use it

/**
 * Encrypt the given value.
 *
 * @param  string  $value
 * @return string
 *
 * @throws \Illuminate\Contracts\Encryption\EncryptException
 */
public function encrypt($value)
{// the big function ,is the encrypt means to this value
    $iv = mcrypt_create_iv($this->getIvSize(), $this->getRandomizer());// get the iv by size and randomizer

    $value = base64_encode($this->padAndMcrypt($value, $iv));// use this base64 function to has the true value

    // Once we have the encrypted value we will go ahead base64_encode the input
    // vector and create the MAC for the encrypted value so we can verify its
    // authenticity. Then, we‘ll JSON encode the data in a "payload" array.
    $mac = $this->hash($iv = base64_encode($iv), $value);// get the mac value

    $json = json_encode(compact(‘iv‘, ‘value‘, ‘mac‘));// get the json string

    if (! is_string($json)) {
        throw new EncryptException(‘Could not encrypt the data.‘);
    }// if the value is wrong throw exception

    return base64_encode($json);// the return the base64_encode
}

/**
 * Pad and use mcrypt on the given value and input vector.
 *
 * @param  string  $value
 * @param  string  $iv
 * @return string
 */
protected function padAndMcrypt($value, $iv)
{
    $value = $this->addPadding(serialize($value));// value be change like value

    return mcrypt_encrypt($this->cipher, $this->key, $value, MCRYPT_MODE_CBC, $iv);// then  return the encrypt value
}// the value is given value and input vector

/**
 * Decrypt the given value.
 *
 * @param  string  $payload
 * @return string
 */
public function decrypt($payload)
{
    $payload = $this->getJsonPayload($payload);// use the payload get the json payload

    // We‘ll go ahead and remove the PKCS7 padding from the encrypted value before
    // we decrypt it. Once we have the de-padded value, we will grab the vector
    // and decrypt the data, passing back the unserialized from of the value.
    $value = base64_decode($payload[‘value‘]);// first get the payload by base64_decode

    $iv = base64_decode($payload[‘iv‘]);//iv use the same method

    return unserialize($this->stripPadding($this->mcryptDecrypt($value, $iv)));// too big function
}


本文出自 “专注php” 博客,请务必保留此出处http://jingshanls.blog.51cto.com/3357095/1788513

[李景山php]每天laravel-20160828|McryptEncrypter-2

标签:function   return   public   

原文地址:http://jingshanls.blog.51cto.com/3357095/1788513

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!