Code Samples: PHP
IMPORTANT! C# is our official client and the only code that we support. This PHP code has been provided by another customer; and we encourage you to let us know if you find any problems, so that we can update accordingly.
Common
// Common to all and references the two files needed for the encryption process
<?php
require_once('padCrypt.php');
require_once('AES_Encryption.php');
class FTSSamples
// Common to all
{
protected $serviceEndpointUrl;
protected $securityContext;
protected $securityToken;
protected $apiKey;
public function __construct ()
{
$this->serviceEndpointUrl = "https://api.sfaxme.com/api/";
$this->securityContext = ""; //<--- Required but leave blank exactly as it is here
$this->apiKey = "";//Required Key
}
public function OutboundFaxCreate
{
// Service Connection and Security Settings
$isSuccess = false;
// IMPORTANT: key parameters
$faxNumber = "15123668506"; //<--- IMPORTANT: Enter a valid fax number
$filePath = getcwd() . "/Page1.tif"; //<--- IMPORTANT: Enter a valid path to primary file to be faxed
$faxRecipient = "GeneTest";
$optionalParams="CoverPageName=None;CoverPageSubject=PHPTest;CoverPageReference=PhpTest1234;TrackingCode=PHPTest1234";//Parameters to pass for CoverPages
// Set Security Token
$FTSAES = new FTSAESHelper($this->securityContext);
$this->securityToken = $FTSAES->GenerateSecurityTokenUrl();
// Construct the base service URL endpoint
$url = $this->serviceEndpointUrl;
$url .= "sendfax?";
$url .= "token=". urlencode($this->securityToken);
$url .= "&ApiKey=" . urlencode($this->apiKey);
// Add the method specific parameters
$url .= "&RecipientFax=" . urlencode($faxNumber);
$url .= "&RecipientName=" . urlencode($faxRecipient);
$url .= "&OptionalParams=" . urlencode($optionalParams);
//echo "URL: " . $url;
//reference primary file to fax
$postData = array('file'=>"@$filePath");
//initialize cURL and set cURL options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
//curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
//specific cURL options for HTTPS sites
//see <a href="http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/">http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</a>
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/EquifaxSecureGlobalBusinessCA-1.crt");
//trust any cert - FOR DEVELOPMENT ONLY
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute curl and get response information
$responseBody = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$error = curl_error($ch);
curl_close ($ch);
//get headers and response data
$helper = new FTSHelper();
$headers = $helper->GetHeaders($responseBody, $responseInfo);
if ($responseInfo["http_code"] == 200)
{
//get additional information from XML payload
//response data xml payload
$xResponseData = $helper->GetResponseData($responseBody, $responseInfo);
if ($xResponseData != null)
{
}
}
else
{
//something went wrong so investigate result and error information
//get result information from response headers
$xwsResultCode = $responseInfo["http_code"];
echo "ResultCode=" . $xwsResultCode ;
//get error information from response headers
$xwsErrorCode = $responseInfo["http_code"];
echo "ErrorCode=" . $xwsErrorCode ;
}
}
public function OutboundFaxStatus
// key parameters
$sendfaxQueueId = ""; //<--- IMPORTANT: Enter a valid transmissionId
// Set Security Token
$FTSAES = new FTSAESHelper($this->securityContext);
$this->securityToken = $FTSAES->GenerateSecurityTokenUrl();
// Construct the base service URL endpoint
$url = $this->serviceEndpointUrl;
$url .= "sendfaxstatus?";
$url .= "token=". urlencode($this->securityToken);
$url .= "&ApiKey=" . urlencode($this->apiKey);
// Add the method specific parameters
$url .= "&SendFaxQueueId=" . urlencode($sendfaxQueueId);
//initialize cURL and set cURL options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_GETHTTP, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "");
//specific cURL options for HTTPS sites
//see <a href="http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/">http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</a>
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/EquifaxSecureGlobaleBusinessCA-1.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute curl and get response information
//echo "URL: " . $url;
$responseBody = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$error = curl_error($ch);
curl_close ($ch);
//get headers and response data
$helper = new FTSHelper();
$headers = $helper->GetHeaders($responseBody, $responseInfo);
if ($responseInfo["http_code"] == 200)
{
//get additional information from XML payload
//response data xml payload
$xResponseData = $helper->GetResponseData($responseBody, $responseInfo);
if ($xResponseData != null)
{
}
}
else
{
//something went wrong so investigate result and error information
//get result information from response headers
$xwsResultCode = $responseInfo["http_code"];
echo "ResultCode=" . $xwsResultCode ;
//get error information from response headers
$xwsErrorCode = $responseInfo["http_code"];
echo "ErrorCode=" . $xwsErrorCode ;
}
}
public function InboundFaxRetrieveSet
{
// Set Security Token
$FTSAES = new FTSAESHelper($this->securityContext);
$this->securityToken = $FTSAES->GenerateSecurityTokenUrl();
// Construct the base service URL endpoint
$url = $this->serviceEndpointUrl;
$url .= "receiveinboundfax?";
$url .= "token=". urlencode($this->securityToken);
$url .= "&ApiKey=" . urlencode($this->apiKey);
// Add the method specific parameters
$url .= "&StartDateUTC=" . urlencode("");//Optional provide a start date 01/01/2013
$url .= "&EndDateUTC=" . urlencode("");//Option provide a end date 02/01/2013
$url .= "&MaxItems=" . urlencode("");//Option provide a numeric number from 1 to 500. Defaults to 500 results
//initialize cURL and set cURL options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//specific cURL options for HTTPS sites
//see <a href="http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/">http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</a>
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/EquifaxSecureGlobaleBusinessCA-1.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute curl and get response information
$responseBody = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$error = curl_error($ch);
curl_close ($ch);
//get headers and response data
$helper = new FTSHelper();
$headers = $helper->GetHeaders($responseBody, $responseInfo);
if ($responseInfo["http_code"] == 200)
{
//get additional information from XML payload
//response data xml payload
$xResponseData = $helper->GetResponseData($responseBody, $responseInfo);
if ($xResponseData != null)
{
}
}
else
{
//something went wrong so investigate result and error information
//get result information from response headers
$xwsResultCode = $responseInfo["http_code"];
echo "ResultCode=" . $xwsResultCode ;
//get error information from response headers
$xwsErrorCode = $responseInfo["http_code"];
echo "ErrorCode=" . $xwsErrorCode ;
}
}
public function InboundFaxDownloadAsTIF
// IMPORTANT: key parameters
$faxId = ""; //<--- IMPORTANT: Enter a valid faxId
// Set Security Token
$FTSAES = new FTSAESHelper($this->securityContext);
$this->securityToken = $FTSAES->GenerateSecurityTokenUrl();
// Construct the base service URL endpoint
$url = $this->serviceEndpointUrl;
$url .= "downloadinboundfaxastif?";
$url .= "token=". urlencode($this->securityToken);
$url .= "&ApiKey=" . urlencode($this->apiKey);
// Add the method specific parameters
$url .= "&FaxId=" . urlencode($faxId);
//initialize cURL and set cURL options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "");
//specific cURL options for HTTPS sites
//see <a href="http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/">http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</a>
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/EquifaxSecureGlobaleBusinessCA-1.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute curl and get response information
$responseBody = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$error = curl_error($ch);
curl_close ($ch);
//get headers and response data
$helper = new FTSHelper();
$headers = $helper->GetHeaders($responseBody, $responseInfo);
if ($responseInfo["http_code"] == 200)
{
//$faxId = $headers["XwsReturnData"];
$helper->WriteResponseToFile($responseBody, $responseInfo, $faxId . ".tif");
}
else
{
//something went wrong so investigate result and error information
//get result information from response headers
$xwsResultCode = $responseInfo["http_code"];
echo "ResultCode=" . $xwsResultCode ;
//get error information from response headers
$xwsErrorCode = $responseInfo["http_code"];
echo "ErrorCode=" . $xwsErrorCode ;
}
}
public function InboundFaxDownloadAsPDF
{
// IMPORTANT: key parameters
$faxId = ""; //<--- IMPORTANT: Enter a valid faxId
// Set Security Token
$FTSAES = new FTSAESHelper($this->securityContext);
$this->securityToken = $FTSAES->GenerateSecurityTokenUrl();
// Construct the base service URL endpoint
$url = $this->serviceEndpointUrl;
$url .= "downloadinboundfaxaspdf?";
$url .= "token=". urlencode($this->securityToken);
$url .= "&ApiKey=" . urlencode($this->apiKey);
// Add the method specific parameters
$url .= "&FaxId=" . urlencode($faxId);
//initialize cURL and set cURL options
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//curl_setopt($ch, CURLOPT_POST, true);
//curl_setopt($ch, CURLOPT_POSTFIELDS, "");
//specific cURL options for HTTPS sites
//see <a href="http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/">http://unitstep.net/blog/2009/05/05/using-curl-in-php-to-access-https-ssltls-protected-sites/</a>
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
//curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
//curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "/EquifaxSecureGlobaleBusinessCA-1.crt");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//execute curl and get response information
$responseBody = curl_exec($ch);
$responseInfo = curl_getinfo($ch);
$error = curl_error($ch);
curl_close ($ch);
//get headers and response data
$helper = new FTSHelper();
$headers = $helper->GetHeaders($responseBody, $responseInfo);
if ($responseInfo["http_code"] == 200)
{
//$faxId = $headers["XwsReturnData"];
$helper->WriteResponseToFile($responseBody, $responseInfo, $faxId . ".pdf");
}
else
{
//something went wrong so investigate result and error information
//get result information from response headers
$xwsResultCode = $responseInfo["http_code"];
echo "ResultCode=" . $xwsResultCode ;
//get error information from response headers
$xwsErrorCode = $responseInfo["http_code"];
echo "ErrorCode=" . $xwsErrorCode ;
}
}
}
class FTSHelper
// Common to all
{
public static function GetHeaders($responseBody, $responseInfo)
{
$header_text = substr($responseBody, 0, $responseInfo['header_size']);
$headers = array();
foreach(explode("\n",$header_text) as $line)
{
$parts = explode(": ",$line);
if(count($parts) == 2)
{
if (isset($headers[$parts[0]]))
{
if (is_array($headers[$parts[0]])) $headers[$parts[0]][] = chop($parts[1]);
else $headers[$parts[0]] = array($headers[$parts[0]], chop($parts[1]));
} else
{
$headers[$parts[0]] = chop($parts[1]);
}
}
}
return $headers;
}
public static function GetResponseData($responseBody, $responseInfo)
// Common to all
{
$body = "" . substr($responseBody, $responseInfo['header_size']);
echo "SendFaxResponse: " . $body;
}
public static function GetInboundResponseData($responseBody, $responseInfo)
// Common ONLY to Inbound Fax RetrieveSet
{
$body = "" . substr($responseBody, $responseInfo['header_size']);
echo "InboundRetrieveSets: " . $body;
}
public static function WriteResponseToFile($responseBody, $responseInfo, $localFileName)
// Common to ONLY InboundFaxDownloadTIF & InboundFaxDownloadPDF
{
$data = substr($responseBody, $responseInfo['header_size']);
$fp = fopen($localFileName, "w");
fwrite($fp, $data, strlen($data));
fclose($fp);
}
}
class FTSAESHelper
// Common to All
{
protected $pTokenContext;
protected $pTokenUsername;
protected $pTokenApiKey;
protected $pTokenClient;
protected $pEncryptionKey;
protected $pEncryptionInitVector;
public function __construct($pSecurityContext)
{
$this->pTokenContext=$pSecurityContext;
$this->pTokenUsername=""; //<--- IMPORTANT: Enter a valid Username
$this->pTokenApiKey= ""; //<--- IMPORTANT: Enter a valid ApiKey
$this->pTokenClient=""; //<--- IMPORTANT: Leave Blank
$this->pEncryptionKey=""; //<--- IMPORTANT: Enter a valid Encryption key
$this->pEncryptionInitVector="x49e*wJVXr8BrALE"; //<--- IMPORTANT: Enter a valid Init vector
}
public function GenerateSecurityTokenUrl()
{
$tokenDataInput;
$tokenDataEncoded;
$tokenGenDT;
$tokenGenDT = gmdate("Y-m-d") . "T" . gmdate("H:i:s") . "Z";
$tokenDataInput = "Context=" . $this->pTokenContext . "&Username=" . $this->pTokenUsername. "&ApiKey=" . $this->pTokenApiKey . "&GenDT=" . $tokenGenDT . "";
if($this->pTokenClient != null && $this->pTokenClient != "")
{
$tokenDataInput .= "&Client=" . $this->pTokenClient;
}
$AES = new AES_Encryption($this->pEncryptionKey, $this->pEncryptionInitVector, "PKCS7", "cbc");
$tokenDataEncoded = base64_encode($AES->encrypt($tokenDataInput));
return $tokenDataEncoded;
}
}
AES_Encryption
<?php
// Common to All
/***
* AES_Encryption
* This class allows you to easily encrypt and decrypt text in AES format
* The class automatically determines whether you need 128, 192, or 256 bits
* based on your key size. It handles multiple padding formats.
*
* Dependencies:
* This class is dependent on PHP's mcrypt extension and a class called padCrypt
*
* Information about mcrypt extension is at:
* <a href="http://us.php.net/mcrypt">http://us.php.net/mcrypt</a>
*
* padCrypt class is published at:
* <a href="http://dev.strategystar.net/2011/10/php-cryptography-padding-ansi-x-923-iso-10126-pkcs7-bit-zero/">http://dev.strategystar.net/2011/10/php-cryptography-padding-ansi-x-923-iso-10126-pkcs7-bit-zero/</a>
*
* The padCrypt class provides methods for padding strings with the
* common methods described at:
* <a href="http://en.wikipedia.org/wiki/Padding_%28cryptography%29">http://en.wikipedia.org/wiki/Padding_%28cryptography%29</a>
*
* -- AES_Encryption Information
*
* Key Sizes:
* 16 bytes = 128 bit encryption
* 24 bytes = 192 bit encryption
* 32 bytes = 256 bit encryption
*
* Padding Formats:
* ANSI_X.923
* ISO_10126
* PKCS7
* BIT
* ZERO
*
* The default padding method in this AES_Encryption class is ZERO padding
* ZERO padding is generally OK for paddings in messages because
* null bytes stripped at the end of a readable message should not hurt
* the point of the text. If you are concerned about message integrity,
* you can use PKCS7 instead
*
* This class does not generate keys or vectors for you. You have to
* generate them yourself because you need to keep track of them yourself
* anyway in order to decrypt AES encryptions.
*
* -- Example Usage:
*
* $key = "bac09c63f34c9845c707228b20cac5e0";
* $iv = "47c743d1b21de03034e0842352ae6b98";
* $message = "Meet me at 11 o'clock behind the monument.";
*
* $AES = new AES_Encryption($key, $iv);
* $encrypted = $AES->encrypt($message);
* $decrypted = $AES->decrypt($encrypted);
* $base64_encrypted = base64_encode($encrypted);
*
* -- Credits:
*
* @author Strategy Star Inc.
* @website <a href="http://www.strategystar.net">http://www.strategystar.net</a>
**/
class AES_Encryption
{
private $key, $initVector, $mode, $cipher, $encryption = null;
private $allowed_bits = array(128, 192, 256);
private $allowed_modes = array('ecb', 'cfb', 'cbc', 'nofb', 'ofb');
private $vector_modes = array('cbc','cfb','ofb');
private $allowed_paddings = array(
'ANSI_X.923' => 'ANSI_X923',
'ISO_10126' => 'ISO_10126',
'PKCS7' => 'PKCS7',
'BIT' => 'BIT',
'ZERO' => 'ZERO',
);
private $padCrypt_url = 'http://dev.strategystar.net/2011/10/php-cryptography-padding-ansi-x-923-iso-10126-pkcs7-bit-zero/';
private $aesEncrypt_url = 'http://dev.strategystar.net/';
/***
* String $key = Your secret key that you will use to encrypt/decrypt
* String $initVector = Your secret vector that you will use to encrypt/decrypt if using CBC, CFB, OFB, or a STREAM algorhitm that requires an IV
* String $padding = The padding method you want to use. The default is ZERO (aka NULL byte) [ANSI_X.923,ISO_10126,PKCS7,BIT,ZERO]
* String $mode = The encryption mode you want to use. The default is cbc [ecb,cfb,cbc,stream,nofb,ofb]
**/
public function __construct($key, $initVector='', $padding='ZERO', $mode='cbc')
{
$mode = strtolower($mode);
$padding = strtoupper($padding);
if(!class_exists('padCrypt'))
{
throw new Exception('The padCrypt class must be loaded for AES_Encryption to work: '.$padCrypt_url);
}
if(!function_exists('mcrypt_module_open'))
{
throw new Exception('The mcrypt extension must be loaded.');
}
if(strlen($initVector) != 16 && in_array($mode, $this->vector_modes))
{
throw new Exception('The $initVector is supposed to be 16 bytes in for CBC, CFB, NOFB, and OFB modes.');
}
elseif(!in_array($mode, $this->vector_modes) && !empty($initVector))
{
throw new Exception('The specified encryption mode does not use an initialization vector. You should pass an empty string, zero, FALSE, or NULL.');
}
$this->encryption = strlen($key)*8;
if(!in_array($this->encryption, $this->allowed_bits))
{
throw new Exception('The $key must be either 16, 24, or 32 bytes in length for 128, 192, and 256 bit encryption respectively.');
}
$this->key = $key;
$this->initVector = $initVector;
if(!in_array($mode, $this->allowed_modes))
{
throw new Exception('The $mode must be one of the following: '.implode(', ', $this->allowed_modes));
}
if(!array_key_exists($padding, $this->allowed_paddings))
{
throw new Exception('The $padding must be one of the following: '.implode(', ', $this->allowed_paddings));
}
$this->mode = $mode;
$this->padding = $padding;
$this->cipher = mcrypt_module_open('rijndael-128', '', $this->mode, '');
$this->block_size = mcrypt_get_block_size('rijndael-128', $this->mode);
}
/***
* String $text = The text that you want to encrypt
**/
public function encrypt($text)
{
mcrypt_generic_init($this->cipher, $this->key, $this->initVector);
$encrypted_text = mcrypt_generic($this->cipher, $this->pad($text, $this->block_size));
mcrypt_generic_deinit($this->cipher);
return $encrypted_text;
}
/***
* String $text = The text that you want to decrypt
**/
public function decrypt($text)
{
mcrypt_generic_init($this->cipher, $this->key, $this->initVector);
$decrypted_text = mdecrypt_generic($this->cipher, $text);
mcrypt_generic_deinit($this->cipher);
return $this->unpad($decrypted_text);
}
/***
* Use this function to export the key, init_vector, padding, and mode
* This information is necessary to later decrypt an encrypted message
**/
public function getConfiguration()
{
return array(
'key' => $this->key,
'init_vector' => $this->initVector,
'padding' => $this->padding,
'mode' => $this->mode,
'encryption' => $this->encryption . ' Bit',
'block_size' => $this->block_size,
);
}
private function pad($text, $block_size)
{
return call_user_func_array(array('padCrypt', 'pad_'.$this->allowed_paddings[$this->padding]), array($text, $block_size));
}
private function unpad($text)
{
return call_user_func_array(array('padCrypt', 'unpad_'.$this->allowed_paddings[$this->padding]), array($text));
}
public function __destruct()
{
mcrypt_module_close($this->cipher);
}
}
padCrypt
// Common to All
<?php
/**
* padCrypt.php
*
* This class can be used to pad strings with the following methods:
* ANSI X.923, ISO 10126, PKCS7, Zero Padding, and Bit Padding
*
* The methods are implemented as documented at:
* <a href="http://en.wikipedia.org/wiki/Padding_(cryptography">http://en.wikipedia.org/wiki/Padding_(cryptography</a>)
*
* @author Strategy Star Inc.
* @website <a href="http://www.strategystar.net">http://www.strategystar.net</a>
*/
class padCrypt
{
public static function pad_ISO_10126($data, $block_size)
{
$padding = $block_size - (strlen($data) % $block_size);
for($x=1; $x<$padding; $x++)
{
mt_srand();
$data .= chr(mt_rand(0,255));
}
return $data . chr($padding);
}
public static function unpad_ISO_10126($data)
{
$length = ord(substr($data, -1));
return substr($data, 0, strlen($data)-$length);
}
public static function pad_ANSI_X923($data, $block_size)
{
$padding = $block_size - (strlen($data) % $block_size);
return $data . str_repeat(chr(0), $padding - 1) . chr($padding);
}
public static function unpad_ANSI_X923($data)
{
$length = ord(substr($data, -1));
$padding_position = strlen($data) - $length;
$padding = substr($data, $padding_position, -1);
for($x=0; $x<$length; $x++)
{
if(ord(substr($padding, $x, 1)) != 0)
{
return $data;
}
}
return substr($data, 0, $padding_position);
}
public static function pad_PKCS7($data, $block_size)
{
$padding = $block_size - (strlen($data) % $block_size);
$pattern = chr($padding);
return $data . str_repeat($pattern, $padding);
}
public static function unpad_PKCS7($data)
{
$pattern = substr($data, -1);
$length = ord($pattern);
$padding = str_repeat($pattern, $length);
$pattern_pos = strlen($data) - $length;
if(substr($data, $pattern_pos) == $padding)
{
return substr($data, 0, $pattern_pos);
}
return $data;
}
public static function pad_BIT($data, $block_size)
{
$length = $block_size - (strlen($data) % $block_size) - 1;
return $data . "\x80" . str_repeat("\x00", $length);
}
public static function unpad_BIT($data)
{
if(substr(rtrim($data, "\x00"), -1) == "\x80")
{
return substr(rtrim($data, "\x00"), 0, -1);
}
return $data;
}
public static function pad_ZERO($data, $block_size)
{
$length = $block_size - (strlen($data) % $block_size);
return $data . str_repeat("\x00", $length);
}
public static function unpad_ZERO($data)
{
return rtrim($data, "\x00");
}
}
?>