Skip to content

Commit

Permalink
Merge branch 'release/1.2.1'
Browse files Browse the repository at this point in the history
  • Loading branch information
jacklin committed Apr 15, 2020
2 parents 23a7911 + 33bad64 commit e55f665
Show file tree
Hide file tree
Showing 8 changed files with 512 additions and 0 deletions.
46 changes: 46 additions & 0 deletions README
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,49 @@ $inputFile = '/usr/local/src/2019-01-30.log';//日志文件
$outputFile = '/usr/local/src/2019-01-30.csv';//分析结果文件
LogAnalysis::setFlowLine(4);//设置日志行流量的列数;默认为4
$res = LogAnalysis::analysisLogFile($inputFile,$outputFile);

/**
* opensslRsa加解密类
* #生成私钥
* openssl genrsa -out rsa_private_key.pem 1024
* #Java开发者需要将私钥转换成PKCS8格式(备注:使用Java开发后台,客户端iOS也要中这个)
* openssl pkcs8 -topk8 -inform PEM -in rsa_private_key.pem -outform PEM -nocrypt -out rsa_private_key_pkcs8.pem
* #生成公钥
* openssl rsa -in rsa_private_key.pem -pubout -out rsa_public_key.pem
* 参考文献:https://www.jianshu.com/p/a79c78e17f6f
*/
require_once("../src/Tool/OpensslRsa.php");
$str = "test";
$public_key_file="./opensslkey/rsa_public_key.pem";
$private_key_file="./opensslkey/rsa_private_key_pkcs8.pem";
//设置公钥文件路径
OpensslRsa::setPublicKeyFilePath($public_key_file);
//设置私钥文件路径
OpensslRsa::setPrivateKeyFilePath($private_key_file);
//私钥加密
$encrypt_str = OpensslRsa::privateEncrypt($str);
echo $encrypt_str.PHP_EOL;
//公钥解密
$descrypt_str = OpensslRsa::publicDescrypt($encrypt_str);
echo $descrypt_str.PHP_EOL;
//公钥加密
$encrypt_str = OpensslRsa::publicEncrypt($str);
echo $encrypt_str.PHP_EOL;
//私钥解密
$descrypt_str = OpensslRsa::privateDescrypt($encrypt_str);
echo $descrypt_str.PHP_EOL;
//签名及验证
if (($sign = OpensslRsa::sign($str)) !== false) {
echo PHP_EOL;
echo $sign . PHP_EOL;
$verify_result = OpensslRsa::verify($str,$sign);
if ($verify_result == 1) {
echo "vild";
}elseif ($verify_result == 0) {
echo "invild"
}else{
echo "Error:" . openssl_error_string();
}
}else{
throw new \Exception("Error:" . openssl_error_string());
}
50 changes: 50 additions & 0 deletions src/Tool/IpRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
namespace Tool;
class IpRange
{
/**
* ipv4是否在指定多IP段范围内
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-04-15T10:40:30+0800
* @param string $ip 待验证IP
* @param array $ipRange ip段集合['192.168.0.0/24','172.16.0.0/16','10.0.0.1/32','101.101.101.101']
* @return boolean true-在范围内,false-不在范围内
*/
public function ipv4InRange($ip,$ipRange){
$isIn = false;
if ($ipRange && in_array($ip, $ipRange)){
$isIn = true;
}
if(!$isIn){
$rangeIps = array_filter($ipRange, function($v){return strpos($v, '/') !== false;});
foreach ($rangeIps as $rangeIp) {
if($this->v4($ip, $rangeIp)){
$isIn = true;
break;
}
}
}
return $isIn;
}
/**
* ipv4是否在指定单IP段范围内
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-04-15T10:43:47+0800
* @param string $ip 待验证IP
* @param string $range ip段 192.168.0.0/24 IP/CIDR
* @return boolean true-在范围内,false-不在范围内
*/
private function v4( $ip, $range ) {
if ( strpos( $range, '/' ) === false ) {
$range .= '/32';
}
list( $range, $netmask ) = explode( '/', $range, 2 );
$range_decimal = ip2long( $range );
$ip_decimal = ip2long( $ip );
$wildcard_decimal = pow( 2, ( 32 - $netmask ) ) - 1;
$netmask_decimal = ~ $wildcard_decimal;
return ( ( $ip_decimal & $netmask_decimal ) == ( $range_decimal & $netmask_decimal ) );
}
}
243 changes: 243 additions & 0 deletions src/Tool/OpensslRsa.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,243 @@
<?php
namespace Tool;

/**
* Openssl使用非对称加解密
*/
class OpensslRsa
{
//生成的密钥位数
private static $keySize = 2048;
//默认pkcs1
private static $openssl_padding = OPENSSL_PKCS1_PADDING;
//公钥文件路径,如:/rsa_public_key.pem
private static $publicKeyFilePath;
//私钥文件路径,如:/rsa_private_key.pem
private static $privateKeyFilePath;
//
private static $openssl_algorithm = OPENSSL_ALGO_SHA256;
/**
* 设置生成的密钥位数
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T17:48:47+0800
* @param integer $size 位数
*/
public static function setKeySize($size=2048){
self::$keySize = $size;
}
/**生成的密钥位数
* 获取
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T17:51:14+0800
* @return [type] [description]
*/
public static function getKeySize(){
return self::$keySize;
}
/**
* 设置浮动
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T17:53:39+0800
* @param void $padding 浮动OPENSSL_PKCS1_PADDING|OPENSSL_NO_PADDING
*/
public static function setPadding($padding = OPENSSL_PKCS1_PADDING){
self::$openssl_padding = $padding;
}
/**
* [setAlgorithm description]
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-06T10:35:55+0800
* @param void $algorithm 算法OPENSSL_ALGO_SHA256|OPENSSL_ALGO_MD5 OPENSSL_ALGO_SHA1
*/
public static function setAlgorithm($algorithm = OPENSSL_ALGO_SHA256){
self::$openssl_algorithm = $algorithm;
}
/**
* 设置公钥文件路径
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T17:50:08+0800
* @param boolean $filePath 文件路径
*/
public static function setPublicKeyFilePath($filePath){
self::$publicKeyFilePath = $filePath;
return true;
}
/**
* 获取公钥
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:05:21+0800
* @return string|mixed 返回public key
*/
public static function getPublicKey(){
if (is_file(self::$publicKeyFilePath)) {
return openssl_get_publickey(file_get_contents(self::$publicKeyFilePath));
}else{
throw new \Exception("no such public key file!");
}
}
/**
* 设置私钥文件路径
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T17:50:23+0800
* @param [type] $filePath 文件路径
*/
public static function setPrivateKeyFilePath($filePath){
self::$privateKeyFilePath = $filePath;
return true;
}
/**
* 获取私钥
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:06:11+0800
* @return string|mixed 返回private key
*/
public static function getPrivateKey(){
if (is_file(self::$privateKeyFilePath)) {
return openssl_get_privatekey(file_get_contents(self::$privateKeyFilePath));
}else{
throw new \Exception("no such private key file!");
}
}
/**
* 私钥加密
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:31:41+0800
* @param string $str 待加密字符串
* @return string 加密后的字符串并进行base64编码
*/
public static function privateEncrypt($str){
$data = "";

$dataArray = str_split($str, self::$keySize/8-11);
$key = self::getPrivateKey();
foreach ($dataArray as $value) {
$encryptedTemp = "";
if (openssl_private_encrypt($value,$encryptedTemp,$key,self::$openssl_padding)) {
$data .= $encryptedTemp;
}else{
throw new \Exception("Error openssl_private_encrypt false");
}
}
openssl_free_key($key);
return base64_encode($data);
}
/**
* 公钥解密
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:32:45+0800
* @param string $str 待加密字符串
* @return string 解密后的字符串
*/
public static function publicDescrypt($str){
$decrypted = "";
$decodeStr = base64_decode($str);
$key = self::getPublicKey();
$enArray = str_split($decodeStr, self::$keySize/8);
foreach ($enArray as $va) {
$decryptedTemp = "";
if (openssl_public_decrypt($va,$decryptedTemp,$key,self::$openssl_padding)) {
$decrypted .= $decryptedTemp;
}else{
throw new \Exception("Error openssl_public_decrypt false");
}
}
openssl_free_key($key);
return $decrypted;
}
/**
* 公钥加密
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:33:48+0800
* @param string $str 待加密字符串
* @return string 加密后的字符串
*/
public static function publicEncrypt($str){
$data = "";
$key = self::getPublicKey();
$dataArray = str_split($str, self::$keySize/8-11);
foreach ($dataArray as $value) {
$encryptedTemp = "";
if (openssl_public_encrypt($value,$encryptedTemp,$key,self::$openssl_padding)) {
$data .= $encryptedTemp;
}else{
throw new \Exception("Error openssl_public_encrypt false");
}
}
openssl_free_key($key);
return base64_encode($data);
}
/**
* 私钥解密
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-05T11:35:02+0800
* @param string $str 加密后的字符串
* @return string 解密后的字符串
*/
public static function privateDescrypt($str){
$decrypted = "";
$decodeStr = base64_decode($str);
$key = self::getPrivateKey();
$enArray = str_split($decodeStr, self::$keySize/8);

foreach ($enArray as $va) {
$decryptedTemp = "";
if (openssl_private_decrypt($va,$decryptedTemp,$key,self::$openssl_padding)) {
$decrypted .= $decryptedTemp;
}else{
throw new \Exception("Error openssl_private_decrypt false");

}
}
openssl_free_key($key);
return $decrypted;
}
/**
* 生成签名
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-06T10:39:46+0800
* @param string $str 待签名字符串
* @param int $algorithm 签名算法OPENSSL_ALGO_SHA256|OPENSSL_ALGO_MD5|OPENSSL_ALGO_SHA1
* @return mixed|boolean 签名|false
*/
public static function sign($str){
$sign = "";
$key = self::getPrivateKey();
if (openssl_sign($str, $sign, $key, self::$openssl_algorithm)) {
$sign = base64_encode($sign);
openssl_free_key($key);
return $sign;
}else{
openssl_free_key($key);
return false;
}
}
/**
* 签名验证
* BaZhang Platform
* @Author [email protected]
* @DateTime 2020-03-06T10:46:11+0800
* @param string $str 数据内容
* @param string $sign [description]
* @return int 如果签名正确,返回1;如果不正确,返回0;如果错误,返回-1。
*/
public static function verify($str, $sign){
$sign = base64_decode($sign);
$key = self::getPublicKey();
$result = openssl_verify($str, $sign, $key, self::$openssl_algorithm);
openssl_free_key($key);
return $result;
}
}
Loading

0 comments on commit e55f665

Please sign in to comment.