js实现php的urlencode函数
时间: 2023-11-16 12:19:54 浏览: 139
可以使用以下JavaScript代码实现PHP的urlencode函数:
```javascript
function urlencode(str) {
str = encodeURIComponent(str);
str = str.replace(/%20/g, '+');
return str;
}
```
使用方法:
```javascript
var encodedStr = urlencode('Hello, world!');
console.log(encodedStr); // 输出:Hello%2C+world%21
```
注意:JavaScript的encodeURIComponent方法和PHP的urlencode函数在URL编码的实现上存在细微差异,因此完全相同的结果可能无法实现。
相关问题
js实现php的utf8版本urlencode函数
可以使用以下JavaScript代码实现PHP的utf8版本urlencode函数:
```javascript
function utf8Urlencode(str) {
str = encodeURIComponent(str);
str = str.replace(/%20/g, '+');
var matches = str.match(/(%[89ABab][0-9A-Fa-f])|(%[0-7][0-9A-Fa-f])/g);
if (matches) {
for (var i = 0; i < matches.length; i++) {
var byte = matches[i].substr(1, 2);
byte = parseInt(byte, 16);
if (byte >= 128) {
str = str.replace(matches[i], '%25' + byte.toString(16).toUpperCase());
}
}
}
return str;
}
```
使用方法:
```javascript
var encodedStr = utf8Urlencode('你好,世界!');
console.log(encodedStr); // 输出:%E4%BD%A0%E5%A5%BD%EF%BC%8C%E4%B8%96%E7%95%8C%EF%BC%81
```
注意:和PHP的utf8版本urlencode函数一样,这个JavaScript函数只对非ASCII字符进行了编码。
我要使用js实现这个PHP签名函数,不使用第三方库,纯js实现且参数及值要urlencode下,public function generateSignature($data, $secretKey) { unset($data['sign']); // Sort the data by key ksort($data); // Convert the data to a query string $queryString = http_build_query($data); // Add the secret key to the query string $queryString .= $secretKey; // Generate the signature using the SHA256 algorithm $signature = hash('sha256', $queryString); // Return the signature return $signature; }
可以参考以下代码实现:
```javascript
function generateSignature(data, secretKey) {
delete data['sign'];
var sortedData = {};
Object.keys(data).sort().forEach(function(key) {
sortedData[key] = data[key];
});
var queryString = Object.keys(sortedData).map(function(key) {
return encodeURIComponent(key) + '=' + encodeURIComponent(sortedData[key]);
}).join('&');
queryString += secretKey;
var signature = sha256(queryString);
return signature;
}
```
其中,`sha256` 函数可以使用 CryptoJS 库实现。如果不想使用 CryptoJS,你可以参考以下代码实现:
```javascript
function sha256(str) {
var utf8Str = new TextEncoder().encode(str);
return crypto.subtle.digest('SHA-256', utf8Str).then(function(hash) {
return hex(hash);
});
}
function hex(buffer) {
var hexCodes = [];
var view = new DataView(buffer);
for (var i = 0; i < view.byteLength; i += 4) {
var value = view.getUint32(i);
var stringValue = value.toString(16);
var padding = '00000000';
var paddedValue = (padding + stringValue).slice(-padding.length);
hexCodes.push(paddedValue);
}
return hexCodes.join('');
}
```
注意,这里使用了 `TextEncoder` 和 `crypto.subtle.digest` API,所以需要在支持这些 API 的浏览器中运行。如果需要在不支持这些 API 的浏览器中运行,你可以使用以下代码实现:
```javascript
function sha256(str) {
var sha256Hash = sha256_digest(str);
var hexCodes = [];
for (var i = 0; i < sha256Hash.length; i++) {
var hexCode = sha256Hash[i].toString(16);
var paddedHexCode = ('00' + hexCode).slice(-2);
hexCodes.push(paddedHexCode);
}
return hexCodes.join('');
}
function sha256_digest(str) {
var utf8Str = unescape(encodeURIComponent(str));
var bytes = new Uint8Array(utf8Str.length);
for (var i = 0; i < utf8Str.length; i++) {
bytes[i] = utf8Str.charCodeAt(i);
}
return sha256_digest_bytes(bytes);
}
function sha256_digest_bytes(bytes) {
var blockSize = 64;
var hashSize = 32;
var messageLength = bytes.length;
var paddingSize = blockSize - ((messageLength + 9) % blockSize) - 1;
var paddedMessageLength = messageLength + paddingSize + 9;
var paddedMessage = new Uint8Array(paddedMessageLength);
paddedMessage.set(bytes);
paddedMessage.set([0x80], messageLength);
for (var i = messageLength + 1; i < paddedMessageLength - 8; i++) {
paddedMessage[i] = 0;
}
var bitLength = messageLength * 8;
var bitLengthBytes = new Uint8Array(8);
bitLengthBytes[0] = (bitLength >>> 56) & 0xff;
bitLengthBytes[1] = (bitLength >>> 48) & 0xff;
bitLengthBytes[2] = (bitLength >>> 40) & 0xff;
bitLengthBytes[3] = (bitLength >>> 32) & 0xff;
bitLengthBytes[4] = (bitLength >>> 24) & 0xff;
bitLengthBytes[5] = (bitLength >>> 16) & 0xff;
bitLengthBytes[6] = (bitLength >>> 8) & 0xff;
bitLengthBytes[7] = bitLength & 0xff;
paddedMessage.set(bitLengthBytes, paddedMessageLength - 8);
var hash = sha256_compress(paddedMessage);
var hashBytes = new Uint8Array(hashSize);
for (var i = 0; i < hashSize; i++) {
hashBytes[i] = (hash[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
}
return hashBytes;
}
function sha256_compress(paddedMessage) {
var blockSize = 64;
var hashSize = 32;
var k = new Uint32Array([
0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5,
0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5,
0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3,
0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174,
0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc,
0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da,
0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7,
0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967,
0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13,
0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85,
0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3,
0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070,
0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5,
0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3,
0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208,
0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2
]);
var h = new Uint32Array([
0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a,
0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19
]);
var w = new Uint32Array(blockSize);
for (var i = 0; i < paddedMessage.length; i += blockSize) {
for (var j = 0; j < blockSize; j++) {
w[j] = (j < 16) ? paddedMessage[i + j] :
sha256_sigma1(w[j - 2]) + w[j - 7] + sha256_sigma0(w[j - 15]) + w[j - 16];
}
var a = h[0];
var b = h[1];
var c = h[2];
var d = h[3];
var e = h[4];
var f = h[5];
var g = h[6];
var h0 = h[7];
for (var j = 0; j < blockSize; j++) {
var t1 = h0 + sha256_sigma1(e) + sha256_ch(e, f, g) + k[j] + w[j];
var t2 = sha256_sigma0(a) + sha256_maj(a, b, c);
h0 = g;
g = f;
f = e;
e = d + t1;
d = c;
c = b;
b = a;
a = t1 + t2;
}
h[0] += a;
h[1] += b;
h[2] += c;
h[3] += d;
h[4] += e;
h[5] += f;
h[6] += g;
h[7] += h0;
}
return h.buffer;
}
function sha256_ch(x, y, z) {
return (x & y) ^ (~x & z);
}
function sha256_maj(x, y, z) {
return (x & y) ^ (x & z) ^ (y & z);
}
function sha256_sigma0(x) {
return sha256_rotr(2, x) ^ sha256_rotr(13, x) ^ sha256_rotr(22, x);
}
function sha256_sigma1(x) {
return sha256_rotr(6, x) ^ sha256_rotr(11, x) ^ sha256_rotr(25, x);
}
function sha256_rotr(n, x) {
return (x >>> n) | (x << (32 - n));
}
```
这里实现了 SHA256 算法的压缩函数和常用函数,以及一个 `sha256_digest` 函数和一个 `sha256` 函数,用于计算 SHA256 哈希值。注意,这里的 `sha256` 函数和 CryptoJS 库中的 `sha256` 函数返回的结果格式不同,需要根据具体情况进行调整。
阅读全文