signature.php
4.04 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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
<?php
/**
* signature.php
* 用于生成参数权限签名的小工具。
* 比如:JSSDK 需要用到的签名 signature 或 采用类似算法的签名计算
* @author Deepseath
* @version $Id$
*/
// 以下代码仅为完成功能而写,代码逻辑性可能不严谨,有洁癖的可以完善一下,跪拜先!
$signature = '';
$json = '';
$string = '';
if (!empty($_POST)) {
$params = [];
$arrays = [];
// print_r($_POST);
$jsonParams = [];
foreach ($_POST['key'] as $_key => $_val) {
if (!$_val) {
continue;
}
if ($_val == 'timestamp' && isset($_POST['timestamp'])) {
$_POST['value'][$_key] = time();
}
$params[] = [
'key' => $_val,
'value' => $_POST['value'][$_key]
];
$arrays[$_val] = $_POST['value'][$_key];
$jsonParams[$_val] = $_POST['value'][$_key];
}
if (isset($arrays['signature'])) {
unset($arrays['signature']);
}
ksort($arrays);
// 参数数组
$arr = [];
foreach ($arrays as $_key => $_value) {
$arr[] = strtolower($_key) . '=' . $_value;
}
$string = implode('&', $arr);
// 根据请求的参数计算的签名值
$signature = sha1($string);
$jsonParams['signature'] = $signature;
$json = json_encode($jsonParams, JSON_UNESCAPED_UNICODE);
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>权限签名(sha1)获取</title>
<meta name="author" content="Deepseath@vchangyi.com">
<style type="text/css">
table {
width: 100%
}
.result th {
width: 280px;
text-align: right
}
.key {
width: 280px
}
.value {
text-align: left
}
input {
width: 99%
}
textarea {
width: 99%;
height: 70px
}
input.timestamp {
width: auto !important
}
button, label {
cursor: pointer
}
button {
width: 280px
}
</style>
</head>
<body>
<table class="result">
<tr>
<th>字符串:</th>
<td><input type="text" id="string" value="<?php echo htmlspecialchars($string);?>" /></td>
</tr>
<tr>
<th>生成的签名:</th>
<td><input type="text" id="signature"
value="<?php echo $signature; ?>" /></td>
</tr>
<tr>
<th>生成的请求参数(JSON):</th>
<td><textarea id="params"><?php echo $json;?></textarea></td>
</tr>
</table>
<form method="post">
<button type="submit">点击这里将下面的参数提交生成签名字符串</button>
<label><input type="checkbox" class="timestamp" name="timestamp" value="1" <?php if (isset($_POST['timestamp'])) { ?> checked="checked" <?php }?> /> 强制将参数“timestamp”设置为当前时间戳(勾选后,timestamp 参数每次提交都是最新的时间戳)</label>
<table class="params">
<thead>
<tr>
<th class="key">参数名</th>
<th class="value">参数对应的值</th>
</tr>
</thead>
<tbody>
<?php
if (empty($params)) {
$params[] = [
'key' => 'ticket',
'value' => '6cf450071cd51538fb850fe899caa6be'
];
$params[] = [
'key' => 'timestamp',
'value' => time()
];
$params[] = [
'key' => 'noncestr',
'value' => mt_rand(100000000, 999999999)
];
$params[] = [
'key' => 'requester',
'value' => 'crm'
];
}
for ($i = 0; $i < 30; $i ++) {
?>
<tr>
<td><input type="text" name="key[<?php echo $i;?>]" value="<?php echo isset($params[$i]) ? htmlspecialchars($params[$i]['key']) : '';?>" /></td>
<td><input type="text" name="value[<?php echo $i; ?>]" value="<?php echo isset($params[$i]) ? htmlspecialchars($params[$i]['value']) : '';?>" /></td>
</tr>
<?php
}
?>
</table>
</form>
</body>
</html>