php 生成 加密 压缩,PHP数据压缩和加解密——pack、unpack

存储一段数据,加密格式为:整数2位长度字符串10位长度整数1位长度。优点:一、数据大小最优化二、在不知道"i2a7i1"这样的压缩格式时,即使拿到文件,也无法正确读出二进制文件转化为明文。三、数据增加时,文件存储大小是等量递增。每次都是以19byte递增。

案例三、key-value型文件存储存储生成的文件为两个:索引文件,数据文件文件中数据存储的格式如下图:

3c80cde08c8f05eefb2f46a997688747.png代码实现:

_node_struct = array(

'next'=>array(1, 'V'),

'prev'=>array(1, 'V'),

'data_offset'=>array(1,'V'),//数据存储起始位置

'data_size'=>array(1,'V'),//数据长度

'ref_count'=>array(1,'V'),//引用此处,模仿PHP的引用计数销毁模式

'key'=>array(16,'H*'),//存储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+");//二进制存储需要使用b

}

}

//创建索引文件

private function _create_index(){

$this->_file_index = fopen($this->_file_index_name, "wb+");//二进制存储需要使用b

if(!$this->_file_index)

throw new fileCacheException("Could't open index file:".$this->_file_index_name);

$this->_index_puts(0, '');//定位文件流至起始位置0, 放置php标记防止下载

$this->_index_puts($this->_file_header_size, pack("V1", 0));

}

//创建存储文件

private function _create_data(){

$this->_file_data = fopen($this->_file_data_name, "wb+");//二进制存储需要使用b

if(!$this->_file_index)

throw new fileCacheException("Could't open index file:".$this->_file_data_name);

$this->_data_puts(0, '');//定位文件流至起始位置0, 放置php标记防止下载

}

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 是否独占、阻塞锁

*/

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())

);

服务器端与客户端根据解析COMMAND格式,找到对应的DATA解码方式,得到正确的数据

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值