Login   Register  
PHP Classes
elePHPant
Icontem

File: class.signature.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Lopo Lencastre de Almeida  >  Signature  >  class.signature.php  >  Download  
File: class.signature.php
Role: Class source
Content type: text/plain
Description: The class
Class: Signature
Generate signatures that identify the current user
Author: By
Last change: Added donation link. Help us in our free work.
Date: 2010-02-23 09:28
Size: 4,027 bytes
 

Contents

Class file image Download
<?
    
/**
    * Simple and easy secure encrypted unique signature
    *
    * Basically this algorithm provides a unique signature for a specific visitor
    *
    * @requirements  PHP 5.x 
    *                PEAR Crypt HMAC2    <http://pear.php.net/package/Crypt_HMAC2>
    *                PEAR Net User Agent <http://pear.php.net/package/Net_UserAgent_Detect/>
    * @usage         
    *                require_once 'class.signature.php';    
    *                $sign = new signature();
    *                $signature = $sign->create();
    *
    * @category    Cryptography
    * @package     Safe_Signature
    * @author      Lopo Lencastre de Almeida, iPublicis - Internet Agency, Portugal <http://www.ipublicis.com>
    * @license     http://opensource.org/licenses/lgpl-3.0.html GNU Lesser General Public License v3
    * @version     $Id: class.signature.php,v 1.0 2008/11/25 12:12:09 humaneasy Exp $
    * @link        http://www.phpclasses.org/safe_signature
    * @donations   http://smsh.me/7kit
    **/

    
class signature {

       
/**
        * Convert hexadecimal value to a Base64 string
        *
        * This method converts any given hexadecimal string to the corresponding Base64 string
        *
        * @access    private
        * @param     string    Hexadecimal string
        * @return    string    Base64 string
        **/
        
protected function _hex2b64 ($str)
        {
            
$raw '';
            for (
$i=0$i strlen($str); $i+=2)
            {
                
$raw .= chr(hexdec(substr($str$i2)));
            }
            return 
base64_encode($raw);
    }

       
/**
        * Create CRC16 value
        *
        * Returns CRC16 of a string as int value. Used internaly.
        *
        * @access    private
        * @param     string    Input string
        * @return    string    CRC16 int
        **/
        
protected function _crc16($string) {
            
$crc 0xFFFF;
          for (
$x 0$x strlen ($string); $x++) {
               
$crc $crc ord($string[$x]);
               for (
$y 0$y 8$y++) {
                     if ((
$crc 0x0001) == 0x0001) {
                           
$crc = (($crc >> 1) ^ 0xA001);
                     } else { 
                    
$crc $crc >> 1
                }
            }
          }
          return 
$crc;
    } 

       
/**
        * Create the signature
        * 
        * Method used for signature generation
        * 
        * @access   public
        * @param    none
        * @return   string    Encoded encrypted signature
        */
        
public function create() 
    {
            
$ip_address $_SERVER['REMOTE_ADDR'];
            
$browser serialize(array('browser'           => Net_UserAgent_Detect::_getStaticProperty('browser'),
                                   
'features'          => Net_UserAgent_Detect::_getStaticProperty('features'),
                                   
'leadingIdentifier' => Net_UserAgent_Detect::_getStaticProperty('leadingIdentifier'),
                                   
'majorVersion'      => Net_UserAgent_Detect::_getStaticProperty('majorVersion'),
                                   
'options'           => Net_UserAgent_Detect::_getStaticProperty('options'),
                                   
'os'                => Net_UserAgent_Detect::_getStaticProperty('os'),
                                   
'quirks'            => Net_UserAgent_Detect::_getStaticProperty('quirks'),
                                   
'subVersion'        => Net_UserAgent_Detect::_getStaticProperty('subVersion'),
                                   
'userAgent'         => Net_UserAgent_Detect::_getStaticProperty('userAgent'),
                                   
'version'           => Net_UserAgent_Detect::_getStaticProperty('version'),
                                    ));

            
$nonce $this->_crc16($browser);
            
$secretKey md5($this->_crc16($ip_address));   

            
$strToSign $ip_address "\n" $browser "\n" $nonce;
            
$hasher = new Crypt_HMAC2($secretKey"sha256");

            return 
urlencode ($this->_hex2b64($hasher->hash($strToSign)));
    }
    }
?>