網(wǎng)絡(luò)通信、文件存儲(chǔ)中經(jīng)常需要交換數(shù)據(jù),為了減少網(wǎng)絡(luò)通信流量、文件存儲(chǔ)大小以及加密通信規(guī)則,經(jīng)常需要對(duì)數(shù)據(jù)進(jìn)行雙向加解密以保證數(shù)據(jù)的安全。
PHP中實(shí)現(xiàn)此功能主要需要使用的函數(shù)主要是pack及unpack函數(shù)
pack
壓縮資料到位字符串之中。
語(yǔ)法: string pack(string format, mixed [args]...);
返回值: 字符串
本函數(shù)用來(lái)將資料壓縮打包到位的字符串之中。
a - NUL- 字符串填滿[padded string] 將字符串空白以 NULL 字符填滿
A - SPACE- 字符串填滿[padded string]
h ? 十六進(jìn)制字符串,低“四位元”[low nibble first] (低位在前)
H - 十六進(jìn)制字符串,高“四位元”[high nibble first](高位在前)
c ? 帶有符號(hào)的字符
C ? 不帶有符號(hào)的字符
s ? 帶有符號(hào)的短模式[short](通常是16位,按機(jī)器字節(jié)順序)
S ? 不帶有符號(hào)的短模式[short](通常是16位,按機(jī)器字節(jié)排序)
n -不帶有符號(hào)的短模式[short](通常是16位,按大endian字節(jié)排序)
v -不帶有符號(hào)的短模式[short](通常是16位,按小endian字節(jié)排序)
i ? 帶有符號(hào)的整數(shù)(由大小和字節(jié)順序決定)
I ? 不帶有符號(hào)的整數(shù)(由大小和字節(jié)順序決定)
l? 帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按機(jī)器字節(jié)順序)
L ? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按機(jī)器字節(jié)順序)
N ? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按大edian字節(jié)順序)
V? 不帶有符號(hào)的長(zhǎng)模式[long](通常是32位,按小edian字節(jié)順序)
f ?浮點(diǎn)(由大小和字節(jié)順序決定)
d ? 雙精度(由大小和字節(jié)順序決定)
x ? 空字節(jié)[NUL byte]
X- 后面一個(gè)字節(jié)[Back up one byte](倒回一位)
unpack
解壓縮位字符串資料。
語(yǔ)法: string pack(string format, mixed [args]...);
返回值: 數(shù)組
本函數(shù)用來(lái)將位的字符串的資料解壓縮。本函數(shù)和 Perl 的同名函數(shù)功能用法完全相同。
案例一、pack實(shí)現(xiàn)縮減文件數(shù)據(jù)存儲(chǔ)大小
<?php //存儲(chǔ)整數(shù)1234567890 file_put_contents("test.txt", 1234567890);
此時(shí)test.txt的文件大小是10byte。注意此時(shí)文件大小是10字節(jié),實(shí)際占用空間大小是1KB。
上面存儲(chǔ)的整數(shù)實(shí)際是以字符串形式存儲(chǔ)于文件test.txt中。
但如果以整數(shù)的二進(jìn)制字符串存jy儲(chǔ),將會(huì)縮減至4byte。
<?php print_r(unpack("i", file_get_contents("test.txt")));
案例二、數(shù)據(jù)加密
以字符串形式存儲(chǔ)一段有意義數(shù)據(jù),7-110-abcdefg-117。
字符"-"分割后,第一位表示字符串長(zhǎng)度,第二位表示存儲(chǔ)位置,第三位表示實(shí)際存儲(chǔ)的字符串,第四位表示結(jié)尾位置。
<?php file_put_contents("test.txt", "7-110-abcdefg-117");
上述方法缺點(diǎn):
一、數(shù)據(jù)存儲(chǔ)大小
二、數(shù)據(jù)以明文方式存儲(chǔ),如果是任何敏感信息,都可能造成不安全訪問(wèn)。
三、文件存儲(chǔ)大小,以不規(guī)則方式遞增。
加密:
<?php file_put_contents("test.txt", pack("i2a7i1", 7, 110, "abcdefg", 117));
存儲(chǔ)一段數(shù)據(jù),加密格式為:整數(shù)2位長(zhǎng)度字符串10位長(zhǎng)度整數(shù)1位長(zhǎng)度。
優(yōu)點(diǎn):
一、數(shù)據(jù)大小最優(yōu)化
二、在不知道"i2a7i1"這樣的壓縮格式時(shí),即使拿到文件,也無(wú)法正確讀出二進(jìn)制文件轉(zhuǎn)化為明文。
三、數(shù)據(jù)增加時(shí),文件存儲(chǔ)大小是等量遞增。每次都是以19byte遞增。
案例三、key-value型文件存儲(chǔ)
存儲(chǔ)生成的文件為兩個(gè):索引文件,數(shù)據(jù)文件
文件中數(shù)據(jù)存儲(chǔ)的格式如下圖:
代碼實(shí)現(xiàn):
<?php error_reporting(E_ALL); class fileCacheException extends Exception{ } //Key-Value型文件存儲(chǔ) class fileCache{ private $_file_header_size = 14; private $_file_index_name; private $_file_data_name; private $_file_index;//索引文件句柄 private $_file_data;//數(shù)據(jù)文件句柄 private $_node_struct;//索引結(jié)點(diǎn)結(jié)構(gòu)體 private $_inx_node_size = 36;//索引結(jié)點(diǎn)大小 public function __construct($file_index="filecache_index.dat", $file_data="filecache_data.dat"){ $this->_node_struct = array( 'next'=>array(1, 'V'), 'prev'=>array(1, 'V'), 'data_offset'=>array(1,'V'),//數(shù)據(jù)存儲(chǔ)起始位置 'data_size'=>array(1,'V'),//數(shù)據(jù)長(zhǎng)度 'ref_count'=>array(1,'V'),//引用此處,模仿PHP的引用計(jì)數(shù)銷毀模式 'key'=>array(16,'H*'),//存儲(chǔ)KEY ); $this->_file_index_name = $file_index; $this->_file_data_name = $file_data; if(!file_exists($this->_file_index_name)){ $this->_create_index(); }else{ $this->_file_index = fopen($this->_file_index_name, "rb+"); } if(!file_exists($this->_file_data_name)){ $this->_create_data(); }else{ $this->_file_data = fopen($this->_file_data_name, "rb+");//二進(jìn)制存儲(chǔ)需要使用b } } //創(chuàng)建索引文件 private function _create_index(){ $this->_file_index = fopen($this->_file_index_name, "wb+");//二進(jìn)制存儲(chǔ)需要使用b if(!$this->_file_index) throw new fileCacheException("Could't open index file:".$this->_file_index_name); $this->_index_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標(biāo)記防止下載 $this->_index_puts($this->_file_header_size, pack("V1", 0)); } //創(chuàng)建存儲(chǔ)文件 private function _create_data(){ $this->_file_data = fopen($this->_file_data_name, "wb+");//二進(jìn)制存儲(chǔ)需要使用b if(!$this->_file_index) throw new fileCacheException("Could't open index file:".$this->_file_data_name); $this->_data_puts(0, '<'.'?php exit()?'.'>');//定位文件流至起始位置0, 放置php標(biāo)記防止下載 } private function _index_puts($offset, $data, $length=false){ fseek($this->_file_index, $offset); if($length) fputs($this->_file_index, $data, $length); else fputs($this->_file_index, $data); } private function _data_puts($offset, $data, $length=false){ fseek($this->_file_data, $offset); if($length) fputs($this->_file_data, $data, $length); else fputs($this->_file_data, $data); } /** * 文件鎖 * @param $is_block 是否獨(dú)占、阻塞鎖 */ private function _lock($file_res, $is_block=true){ flock($file_res, $is_block ? LOCK_EX : LOCK_EX|LOCK_NB); } private function _unlock($file_res){ flock($file_res, LOCK_UN); } public function add($key, $value){ $key = md5($key); $value = serialize($value); $this->_lock($this->_file_index, true); $this->_lock($this->_file_data, true); fseek($this->_file_index, $this->_file_header_size); list(, $index_count) = unpack('V1', fread($this->_file_index, 4)); $data_size = filesize($this->_file_data_name); fseek($this->_file_data, $data_size); $value_size = strlen($value); $this->_data_puts(filesize($this->_file_data_name), $value); $node_data = pack("V1V1V1V1V1H32", ($index_count==0) ? 0 : $index_count*$this->_inx_node_size, 0, filesize($this->_file_data_name), strlen($value), 0, $key); $index_count++; $this->_index_puts($this->_file_header_size, $index_count, 4); $this->_index_puts($this->get_new_node_pos($index_count), $node_data); $this->_unlock($this->_file_data); $this->_unlock($this->_file_index); } public function get_new_node_pos($index_count){ return $this->_file_header_size + 4 + $this->_inx_node_size * ($index_count-1); } public function get_node($key){ $key = md5($key); fseek($this->_file_index, $this->_file_header_size); $index_count = fread($this->_file_index, 4); if($index_count>0) { for ($i=0; $i < $index_count ; $i++) { fseek($this->_file_index, $this->_file_header_size + 4 + $this->_inx_node_size * $i); $data = fread($this->_file_index, $this->_inx_node_size); $node = unpack("V1next/V1prev/V1data_offset/V1data_size/V1ref_count/H32key", $data); if($key == $node['key']){ return $node; } } }else{ return null; } } public function get_data($offset, $length){ fseek($this->_file_data, $offset); return unserialize(fread($this->_file_data, $length)); } } //使用方法 $cache = new fileCache(); $cache->add('abcdefg' , 'testabc'); $data = $cache->get_node('abcdefg'); print_r($data); echo $cache->get_data($data['data_offset'], $data['data_size']);
案例四、socket通信加密
通信雙方都定義好加密格式:
例如:
$LOGIN = array( 'COMMAND'=>array('a30', 'LOGIN'), 'DATA'=>array('a30', 'HELLO') ); $LOGOUT = array( 'COMMAND'=>array('a30', 'LOGOUT'), 'DATA'=>array('a30', 'GOOD BYE') ); $LOGIN_SUCCESS = array( 'COMMAND'=>array('a30', 'LOGIN_SUCCESS'), 'DATA'=>array('V1', 1) ); $LOGOUT_SUCCESS = array( 'COMMAND'=>array('a30', 'LOGIN_SUCCESS'), 'DATA'=>array('V1', time()) );
服務(wù)器端與客戶端根據(jù)解析COMMAND格式,找到對(duì)應(yīng)的DATA解碼方式,得到正確的數(shù)據(jù)
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持綠夏網(wǎng)。