forked from helei112g/payment
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStrUtil.php
More file actions
92 lines (84 loc) · 2.52 KB
/
Copy pathStrUtil.php
File metadata and controls
92 lines (84 loc) · 2.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
namespace Payment\Utils;
/**
* Class StrUtil
* @dec 字符串处理类
* @package Payment\Utils
* @link https://www.gitbook.com/book/helei112g1/payment-sdk/details
* @link https://helei112g.github.io/
*/
class StrUtil
{
/**
* 产生随机字符串,不长于32位
* @param int $length
* @return string 产生的随机字符串
*/
public static function getNonceStr($length = 32)
{
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
}
return $str;
}
/**
* 转码字符集转码 仅支持 转码 到 UTF-8
* @param string $str
* @param string $targetCharset
* @return mixed|string
*/
public static function characet($str, $targetCharset)
{
if (empty($str)) {
return $str;
}
if (strcasecmp('UTF-8', $targetCharset) != 0) {
$str = mb_convert_encoding($str, $targetCharset, 'UTF-8');
}
return $str;
}
/**
* 转成16进制
* @param string $string
* @return string
*/
public static function String2Hex($string)
{
$hex = '';
$len = strlen($string);
for ($i=0; $i < $len; $i++) {
$hex .= dechex(ord($string[$i]));
}
return $hex;
}
/**
* 获取rsa密钥内容
* @param string $key 传入的密钥信息, 可能是文件或者字符串
* @param string $type
*
* @return string
*/
public static function getRsaKeyValue($key, $type = 'private')
{
if (is_file($key)) {// 是文件
$keyStr = @file_get_contents($key);
} else {
$keyStr = $key;
}
$keyStr = str_replace(PHP_EOL, '', $keyStr);
// 为了解决用户传入的密钥格式,这里进行统一处理
if ($type === 'private') {
$beginStr = ['-----BEGIN RSA PRIVATE KEY-----', '-----BEGIN PRIVATE KEY-----'];
$endStr = ['-----END RSA PRIVATE KEY-----', '-----END PRIVATE KEY-----'];
} else {
$beginStr = ['-----BEGIN PUBLIC KEY-----', ''];
$endStr = ['-----END PUBLIC KEY-----', ''];
}
$keyStr = str_replace($beginStr, ['', ''], $keyStr);
$keyStr = str_replace($endStr, ['', ''], $keyStr);
$rsaKey = $beginStr[0] . PHP_EOL . wordwrap($keyStr, 64, PHP_EOL, true) . PHP_EOL . $endStr[0];
return $rsaKey;
}
}