当前位置: 首页>>代码示例>>PHP>>正文


PHP Crypt\Hmac类代码示例

本文整理汇总了PHP中Zend\Crypt\Hmac的典型用法代码示例。如果您正苦于以下问题:PHP Hmac类的具体用法?PHP Hmac怎么用?PHP Hmac使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


在下文中一共展示了Hmac类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: validate

 /**
  * Validate the message originated from MailGun
  * {@link http://documentation.mailgun.net/user_manual.html#securing-webhooks}
  *
  * @return boolean
  */
 protected function validate()
 {
     if (!$this->hasHeader('signature')) {
         return false;
     }
     return $this->getHeader('signature') == Hmac::compute($this->apikey, 'sha256', $this->getHeader('timestamp') . $this->getHeader('token'));
 }
开发者ID:outeredge,项目名称:edge-zf2,代码行数:13,代码来源:MailGunMessage.php

示例2: generateSignature

 /**
  * Add the S3 Authorization signature to the request headers
  *
  * @param  string $method
  * @param  string $path
  * @param  array &$headers
  * @return string
  */
 public function generateSignature($method, $path, &$headers)
 {
     if (!is_array($headers)) {
         $headers = array($headers);
     }
     $type = $md5 = $date = '';
     // Search for the Content-type, Content-MD5 and Date headers
     foreach ($headers as $key => $val) {
         if (strcasecmp($key, 'content-type') == 0) {
             $type = $val;
         } elseif (strcasecmp($key, 'content-md5') == 0) {
             $md5 = $val;
         } elseif (strcasecmp($key, 'date') == 0) {
             $date = $val;
         }
     }
     // If we have an x-amz-date header, use that instead of the normal Date
     if (isset($headers['x-amz-date']) && isset($date)) {
         $date = '';
     }
     $sig_str = "{$method}\n{$md5}\n{$type}\n{$date}\n";
     // For x-amz- headers, combine like keys, lowercase them, sort them
     // alphabetically and remove excess spaces around values
     $amz_headers = array();
     foreach ($headers as $key => $val) {
         $key = strtolower($key);
         if (substr($key, 0, 6) == 'x-amz-') {
             if (is_array($val)) {
                 $amz_headers[$key] = $val;
             } else {
                 $amz_headers[$key][] = preg_replace('/\\s+/', ' ', $val);
             }
         }
     }
     if (!empty($amz_headers)) {
         ksort($amz_headers);
         foreach ($amz_headers as $key => $val) {
             $sig_str .= $key . ':' . implode(',', $val) . "\n";
         }
     }
     $sig_str .= '/' . parse_url($path, PHP_URL_PATH);
     if (strpos($path, '?location') !== false) {
         $sig_str .= '?location';
     } else {
         if (strpos($path, '?acl') !== false) {
             $sig_str .= '?acl';
         } else {
             if (strpos($path, '?torrent') !== false) {
                 $sig_str .= '?torrent';
             }
         }
     }
     $signature = Hmac::compute($this->_secretKey, 'sha1', utf8_encode($sig_str), Hmac::OUTPUT_BINARY);
     $headers['Authorization'] = 'AWS ' . $this->_accessKey . ':' . base64_encode($signature);
     return $sig_str;
 }
开发者ID:robertodormepoco,项目名称:zf2,代码行数:64,代码来源:S3.php

示例3: _signParameters

 /**
  * Computes the RFC 2104-compliant HMAC signature for request parameters
  *
  * This implements the Amazon Web Services signature, as per the following
  * specification:
  *
  * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  *    excluding <tt>Signature</tt>, the value of which is being created),
  *    ignoring case.
  *
  * 2. Iterate over the sorted list and append the parameter name (in its
  *    original case) and then its value. Do not URL-encode the parameter
  *    values before constructing this string. Do not use any separator
  *    characters when appending strings.
  *
  * @param string $url Queue URL
  * @param array $parameters the parameters for which to get the signature.
  *
  * @return string the signed data.
  */
 protected function _signParameters($url, array &$parameters)
 {
     $data = '';
     uksort($parameters, 'strcasecmp');
     unset($parameters['Signature']);
     foreach ($parameters as $key => $value) {
         $data .= $key . $value;
     }
     $hmac = Hmac::compute($this->_secretKey, 'SHA1', $data, Hmac::OUTPUT_BINARY);
     $parameters['Signature'] = base64_encode($hmac);
     return $data;
 }
开发者ID:no-reply,项目名称:cbpl-vufind,代码行数:32,代码来源:V1.php

示例4: calc

 /**
  * Generate the new key
  *
  * @param  string  $hash       The hash algorithm to be used by HMAC
  * @param  string  $password   The source password/key
  * @param  string  $salt
  * @param  integer $iterations The number of iterations
  * @param  integer $length     The output size
  * @return string
  */
 public static function calc($hash, $password, $salt, $iterations, $length)
 {
     if (!in_array($hash, Hmac::getSupportedAlgorithms())) {
         throw new Exception\InvalidArgumentException("The hash algorihtm {$hash} is not supported by " . __CLASS__);
     }
     $num = ceil($length / Hmac::getOutputSize($hash, Hmac::BINARY));
     $result = '';
     for ($block = 0; $block < $num; $block++) {
         $hmac = Hmac::compute($password, $hash, $salt . pack('N', $block), Hmac::BINARY);
         for ($i = 1; $i < $iterations; $i++) {
             $hmac ^= Hmac::compute($password, $hash, $hmac, Hmac::BINARY);
         }
         $result .= $hmac;
     }
     return substr($result, 0, $length);
 }
开发者ID:noirsoldats,项目名称:zf2,代码行数:26,代码来源:Pbkdf2.php

示例5: calc

 /**
  * Generate the new key
  *
  * @param  string  $hash       The hash algorithm to be used by HMAC
  * @param  string  $password   The source password/key
  * @param  string  $salt
  * @param  int $iterations The number of iterations
  * @param  int $length     The output size
  * @throws Exception\InvalidArgumentException
  * @return string
  */
 public static function calc($hash, $password, $salt, $iterations, $length)
 {
     if (!Hmac::isSupported($hash)) {
         throw new Exception\InvalidArgumentException("The hash algorithm {$hash} is not supported by " . __CLASS__);
     }
     $num = ceil($length / Hmac::getOutputSize($hash, Hmac::OUTPUT_BINARY));
     $result = '';
     for ($block = 1; $block <= $num; $block++) {
         $hmac = hash_hmac($hash, $salt . pack('N', $block), $password, Hmac::OUTPUT_BINARY);
         $mix = $hmac;
         for ($i = 1; $i < $iterations; $i++) {
             $hmac = hash_hmac($hash, $hmac, $password, Hmac::OUTPUT_BINARY);
             $mix ^= $hmac;
         }
         $result .= $mix;
     }
     return substr($result, 0, $length);
 }
开发者ID:leonardovn86,项目名称:zf2_basic2013,代码行数:29,代码来源:Pbkdf2.php

示例6: testHmacRIPEMD160_7

 public function testHmacRIPEMD160_7()
 {
     $data = 'Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data';
     $key = str_repeat("ª", 80);
     $hmac = HMAC::compute($key, 'RIPEMD160', $data);
     $this->assertEquals('69ea60798d71616cce5fd0871e23754cd75d5a0a', $hmac);
 }
开发者ID:bradley-holt,项目名称:zf2,代码行数:7,代码来源:HMACTest.php

示例7: signParameters

 /**
  * Computes the RFC 2104-compliant HMAC signature for request parameters
  *
  * This implements the Amazon Web Services signature, as per the following
  * specification:
  *
  * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  *    excluding <tt>Signature</tt>, the value of which is being created),
  *    ignoring case.
  *
  * 2. Iterate over the sorted list and append the parameter name (in its
  *    original case) and then its value. Do not URL-encode the parameter
  *    values before constructing this string. Do not use any separator
  *    characters when appending strings.
  *
  * @param array  $parameters the parameters for which to get the signature.
  * @param string $secretKey  the secret key to use to sign the parameters.
  *
  * @return string the signed data.
  */
 protected function signParameters(array $paramaters)
 {
     $data = "POST\n";
     $data .= $this->_getRegion() . $this->_ec2Endpoint . "\n";
     $data .= "/\n";
     uksort($paramaters, 'strcmp');
     unset($paramaters['Signature']);
     $arrData = array();
     foreach ($paramaters as $key => $value) {
         $arrData[] = $key . '=' . str_replace("%7E", "~", rawurlencode($value));
     }
     $data .= implode('&', $arrData);
     $hmac = Crypt\Hmac::compute($this->_getSecretKey(), 'SHA256', $data, Crypt\Hmac::BINARY);
     return base64_encode($hmac);
 }
开发者ID:bradley-holt,项目名称:zf2,代码行数:35,代码来源:AbstractEc2.php

示例8: addSignature

 /**
  * Add the S3 Authorization signature to the request headers
  *
  * @param  string $method
  * @param  string $path
  * @param  array  $headers
  * @return array
  */
 protected function addSignature($method, $path, $headers)
 {
     if (!is_array($headers)) {
         $headers = array($headers);
     }
     $type = $md5 = $date = '';
     // Search for the Content-type, Content-MD5 and Date headers
     foreach ($headers as $key => $val) {
         if (strcasecmp($key, 'content-type') == 0) {
             $type = $val;
         } elseif (strcasecmp($key, 'content-md5') == 0) {
             $md5 = $val;
         } elseif (strcasecmp($key, 'date') == 0) {
             $date = $val;
         }
     }
     // If we have an x-amz-date header, use that instead of the normal Date
     if (isset($headers['x-amz-date']) && isset($date)) {
         $date = '';
     }
     $sig_str = "{$method}\n{$md5}\n{$type}\n{$date}\n";
     // For x-amz- headers, combine like keys, lowercase them, sort them
     // alphabetically and remove excess spaces around values
     $amz_headers = array();
     foreach ($headers as $key => $val) {
         $key = strtolower($key);
         if (substr($key, 0, 6) == 'x-amz-') {
             if (is_array($val)) {
                 $amz_headers[$key] = $val;
             } else {
                 $amz_headers[$key][] = preg_replace('/\\s+/', ' ', $val);
             }
         }
     }
     if (!empty($amz_headers)) {
         ksort($amz_headers);
         foreach ($amz_headers as $key => $val) {
             $sig_str .= $key . ':' . implode(',', $val) . "\n";
         }
     }
     //US General bucket names must always be lowercased for the signature
     $urlPath = parse_url($path, PHP_URL_PATH);
     $urlPathParts = explode('/', $urlPath);
     if (!empty($urlPathParts[0])) {
         $urlPathParts[0] = strtolower($urlPathParts[0]);
     }
     $sig_str .= '/' . implode('/', $urlPathParts);
     if (strpos($path, '?location') !== false) {
         $sig_str .= '?location';
     } elseif (strpos($path, '?acl') !== false) {
         $sig_str .= '?acl';
     } elseif (strpos($path, '?torrent') !== false) {
         $sig_str .= '?torrent';
     }
     $signature = base64_encode(Hmac::compute($this->_getSecretKey(), 'sha1', utf8_encode($sig_str), true));
     $headers['Authorization'] = 'AWS ' . $this->_getAccessKey() . ':' . $signature;
     return $headers;
 }
开发者ID:robertodormepoco,项目名称:zf2,代码行数:66,代码来源:S3.php

示例9: decrypt

 /**
  * Decrypt
  *
  * @param  string $data
  * @return string|boolean
  * @throws Exception\InvalidArgumentException
  */
 public function decrypt($data)
 {
     if (!is_string($data)) {
         throw new Exception\InvalidArgumentException('The data to decrypt must be a string');
     }
     if ('' === $data) {
         throw new Exception\InvalidArgumentException('The data to decrypt cannot be empty');
     }
     if (empty($this->key)) {
         throw new Exception\InvalidArgumentException('No key specified for the decryption');
     }
     if (empty($this->cipher)) {
         throw new Exception\InvalidArgumentException('No symmetric cipher specified');
     }
     $hmacSize = Hmac::getOutputSize($this->hash);
     $hmac = substr($data, 0, $hmacSize);
     $ciphertext = substr($data, $hmacSize);
     if (!$this->binaryOutput) {
         $ciphertext = base64_decode($ciphertext);
     }
     $iv = substr($ciphertext, 0, $this->cipher->getSaltSize());
     $keySize = $this->cipher->getKeySize();
     // generate the encryption key and the HMAC key for the authentication
     $hash = Pbkdf2::calc(self::KEY_DERIV_HMAC, $this->getKey(), $iv, $this->keyIteration, $keySize * 2);
     // set the decryption key
     $this->cipher->setKey(substr($hash, 0, $keySize));
     // set the key for HMAC
     $keyHmac = substr($hash, $keySize);
     $hmacNew = Hmac::compute($keyHmac, $this->hash, $this->cipher->getAlgorithm() . $ciphertext);
     if (!Utils::compareStrings($hmacNew, $hmac)) {
         return false;
     }
     return $this->cipher->decrypt($ciphertext);
 }
开发者ID:haoyanfei,项目名称:zf2,代码行数:41,代码来源:BlockCipher.php

示例10: decrypt

 /**
  * Decrypt a file
  *
  * @param  string                             $fileIn
  * @param  string                             $fileOut
  * @param  bool                               $compress
  * @return bool
  * @throws Exception\InvalidArgumentException
  */
 public function decrypt($fileIn, $fileOut)
 {
     $this->checkFileInOut($fileIn, $fileOut);
     if (empty($this->key)) {
         throw new Exception\InvalidArgumentException('No key specified for decryption');
     }
     $read = fopen($fileIn, "r");
     $write = fopen($fileOut, "w");
     $hmacRead = fread($read, Hmac::getOutputSize($this->getHashAlgorithm()));
     $iv = fread($read, $this->cipher->getSaltSize());
     $tot = filesize($fileIn);
     $hmac = $iv;
     $size = mb_strlen($iv, '8bit') + mb_strlen($hmacRead, '8bit');
     $keys = Pbkdf2::calc($this->getPbkdf2HashAlgorithm(), $this->getKey(), $iv, $this->getKeyIteration(), $this->cipher->getKeySize() * 2);
     $padding = $this->cipher->getPadding();
     $this->cipher->setPadding(new Symmetric\Padding\NoPadding());
     $this->cipher->setKey(mb_substr($keys, 0, $this->cipher->getKeySize(), '8bit'));
     $this->cipher->setMode('cbc');
     $blockSize = $this->cipher->getBlockSize();
     $hashAlgo = $this->getHashAlgorithm();
     $algorithm = $this->cipher->getAlgorithm();
     $saltSize = $this->cipher->getSaltSize();
     $keyHmac = mb_substr($keys, $this->cipher->getKeySize(), null, '8bit');
     while ($data = fread($read, self::BUFFER_SIZE)) {
         $size += mb_strlen($data, '8bit');
         // Unpadding if last block
         if ($size + $blockSize >= $tot) {
             $this->cipher->setPadding($padding);
             $data .= fread($read, $blockSize);
         }
         $result = $this->cipher->decrypt($iv . $data);
         $hmac = Hmac::compute($keyHmac, $hashAlgo, $algorithm . $hmac . $data);
         $iv = mb_substr($data, -1 * $saltSize, null, '8bit');
         if (fwrite($write, $result) !== mb_strlen($result, '8bit')) {
             return false;
         }
     }
     fclose($write);
     fclose($read);
     // check for data integrity
     if (!Utils::compareStrings($hmac, $hmacRead)) {
         unlink($fileOut);
         return false;
     }
     return true;
 }
开发者ID:chrisbautista,项目名称:bcit-applied-development,代码行数:55,代码来源:FileCipher.php

示例11: _signS3UploadPolicy

 /**
  * Signed S3 Upload Policy
  *
  * @param string $policy            Base64 Encoded string that is the upload policy
  * @return string                   SHA1 encoded S3 Upload Policy
  */
 protected function _signS3UploadPolicy($policy)
 {
     $hmac = Crypt\Hmac::compute($this->_getSecretKey(), 'SHA1', $policy, Crypt\Hmac::BINARY);
     return $hmac;
 }
开发者ID:niallmccrudden,项目名称:zf2,代码行数:11,代码来源:WindowsInstance.php

示例12: testTransform

 public function testTransform()
 {
     $this->assertEquals(Hmac::compute(self::KEY, self::ALGORITHM, self::VALUE, self::BINARY), $this->transformer->transform(self::VALUE));
 }
开发者ID:mediamonks,项目名称:doctrine-extensions,代码行数:4,代码来源:ZendCryptHmacTransformerTest.php

示例13: _signParameters

 /**
  * Computes the RFC 2104-compliant HMAC signature for request parameters
  *
  * This implements the Amazon Web Services signature, as per the following
  * specification:
  *
  * 1. Sort all request parameters (including <tt>SignatureVersion</tt> and
  *    excluding <tt>Signature</tt>, the value of which is being created),
  *    ignoring case.
  *
  * 2. Iterate over the sorted list and append the parameter name (in its
  *    original case) and then its value. Do not URL-encode the parameter
  *    values before constructing this string. Do not use any separator
  *    characters when appending strings.
  *
  * @param  string $queue_url  Queue URL
  * @param  array  $parameters the parameters for which to get the signature.
  *
  * @return string the signed data.
  */
 protected function _signParameters($url, array &$paramaters)
 {
     $data = $this->_httpMethod . "\n";
     $data .= parse_url($url, PHP_URL_HOST) . "\n";
     $data .= '' == ($path = parse_url($url, PHP_URL_PATH)) ? '/' : $path;
     $data .= "\n";
     uksort($paramaters, 'strcmp');
     unset($paramaters['Signature']);
     $arrData = array();
     foreach ($paramaters as $key => $value) {
         $arrData[] = $key . '=' . str_replace('%7E', '~', rawurlencode($value));
     }
     $data .= implode('&', $arrData);
     $hmac = Hmac::compute($this->_secretKey, 'SHA256', $data, Hmac::OUTPUT_BINARY);
     $paramaters['Signature'] = base64_encode($hmac);
     return $data;
 }
开发者ID:navassouza,项目名称:zf2,代码行数:37,代码来源:V2.php

示例14: _hmacMd5

 /**
  * Prepare CRAM-MD5 response to server's ticket
  *
  * @param  string $key   Challenge key (usually password)
  * @param  string $data  Challenge data
  * @param  int    $block Length of blocks (deprecated; unused)
  * @return string
  */
 protected function _hmacMd5($key, $data, $block = 64)
 {
     return Hmac::compute($key, 'md5', $data);
 }
开发者ID:eltondias,项目名称:Relogio,代码行数:12,代码来源:Crammd5.php

示例15: testBinaryOutput

 public function testBinaryOutput()
 {
     $data = Hmac::compute('key', 'sha256', 'test', Hmac::OUTPUT_BINARY);
     $this->assertEquals('Aq+1YwSQLGVvy3N83QPeYgW7bUAdooEu/ZstNqCK8Vk=', base64_encode($data));
 }
开发者ID:rajanlamic,项目名称:IntTest,代码行数:5,代码来源:HmacTest.php


注:本文中的Zend\Crypt\Hmac类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。