var util = {};

 util.indexOf = function (array, item) {
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
return i;
}
}
return -1;
}; util.isFunction = function (source) {
return '[object Function]' === Object.prototype.toString.call(source);
}; util.isIE = function () {
var myNav = navigator.userAgent.toLowerCase();
return (myNav.indexOf('msie') != -1) ? parseInt(myNav.split('msie')[1]) : false;
}; util.extend = function (dst, obj) {
for (var i in obj) {
if (obj.hasOwnProperty(i)) {
dst[i] = obj[i];
}
}
}; util.getName = function (prefix) {
return prefix + Math.random().toString(36).replace(/[^a-z]+/g, '').substr(0, 5);
}; util.createScript = function (url, charset) {
var script = document.createElement_x('script');
script.setAttribute('type', 'text/;
charset && script.setAttribute('charset', charset);
script.setAttribute('src', url);
script.async = true;
return script;
}; util.jsonp = function (url, onsuccess, onerror, charset) {
var callbackName = util.getName('tt_player');
window[callbackName] = function () {
if (onsuccess && util.isFunction(onsuccess)) {
onsuccess(arguments[0]);
}
};
var script = util.createScript(url + '&callback=' + callbackName, charset);
script.onload = script.onreadystatechange = function () {
if (!script.readyState || /loaded|complete/.test(script.readyState)) {
script.onload = script.onreadystatechange = null;
// 移除该script的 DOM 对象
if (script.parentNode) {
script.parentNode.removeChild(script);
}
// 删除函数或变量
window[callbackName] = null;
}
};
script.onerror = function () {
if (onerror && util.isFunction(onerror)) {
onerror();
}
};
document.getElementsByTagName_r('head')[0].appendChild(script);
}; util.json = function (options) {
var opt = {
url: '',
type: 'get',
data: {},
success: function () {},
error: function () {},
};
util.extend(opt, options);
if (opt.url) {
var xhr = XMLHttpRequest
? new XMLHttpRequest()
: new ActiveXObject('Microsoft.XMLHTTP');
var data = opt.data,
url = opt.url,
type = opt.type.toUpperCase(),
dataArr = [];
for (var k in data) {
dataArr.push(k + '=' + data[k]);
}
if (type === 'GET') {
url = url + '?' + dataArr.join('&');
xhr.open(type, url.replace(/\?$/g, ''), true);
xhr.send();
}
if (type === 'POST') {
xhr.open(type, url, true);
xmlhttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
xhr.send(dataArr.join('&'));
}
xhr.onload = function () {
if (xhr.status === 200 || xhr.status === 304) {
var res;
if (opt.success && opt.success instanceof Function) {
res = xhr.responseText;
if (typeof res ==== 'string') {
res = JSON.parse(res);
opt.success.call(xhr, res);
}
}
} else {
if (opt.error && opt.error instanceof Function) {
opt.error.call(xhr, res);
}
}
};
}
}; util.crc32 = function (url) {
var a = document.createElement_x('a');
a.href = url;
var T = (function () {
var c = 0,
table = new Array(256);
for (var n = 0; n != 256; ++n) {
c = n;
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
c = ((c & 1) ? (-306674912 ^ (c >>> 1)) : (c >>> 1));
table[n] = c;
}
return typeof Int32Array !=== 'undefined' ? new Int32Array(table) : table;
})();
var crc32_str = function (str) {
var C = -1;
for (var i = 0, L = str.length, c, d; i < L;) {
c = str.charCodeAt(i++);
if (c < 0x80) {
C = (C >>> 8) ^ T[(C ^ c) & 0xFF];
} else if (c < 0x800) {
C = (C >>> 8) ^ T[(C ^ (192 | ((c >> 6) & 31))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
} else if (c >= 0xD800 && c < 0xE000) {
c = (c & 1023) + 64;
d = str.charCodeAt(i++) & 1023;
C = (C >>> 8) ^ T[(C ^ (240 | ((c >> 8) & 7))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 2) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((d >> 6) & 15) | ((c & 3) << 4))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (d & 63))) & 0xFF];
} else {
C = (C >>> 8) ^ T[(C ^ (224 | ((c >> 12) & 15))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | ((c >> 6) & 63))) & 0xFF];
C = (C >>> 8) ^ T[(C ^ (128 | (c & 63))) & 0xFF];
}
}
return C ^ -1;
};
var r = a.pathname + '?r=' + Math.random().toString(10).substring(2);
if (r[0] != '/') {
r = '/' + r;
}
var s = crc32_str(r) >>> 0;
var is_web = location.protocol.indexOf('http') > -1;
return (is_web ? [location.protocol, a.hostname] : ['http:', a.hostname]).join('//') + r + '&s=' + s;
}; export default util;

我的上几篇文章,还傻不拉几的把js生成crc32的算法用php翻译:

其实完全不用: (Warning由于 PHP 的整数是带符号的,所以在 32 位系统上许多 crc32 校验码将返回负整数。 尽管在 64 位上所有 crc32() 的结果将都是正整数。)

$g = "http://www.baidu.com";
printf("%u", crc32($g));

我的硬翻译:

<?php
function uright($a, $n)
{
$c = 2147483647>>($n-1);
return $c&($a>>$n);
}
function JS_charCodeAt($str, $index){
//not working! $char = mb_substr($str, $index, 1, 'UTF-8');
if (mb_check_encoding($char, 'UTF-8'))
{
$ret = mb_convert_encoding($char, 'UTF-32BE', 'UTF-8');
return hexdec(bin2hex($ret));
} else {
return null;
}
}
function modulo($a, $b) {
return $a - floor($a/$b)*$b;
}
function ToUint32($x) {
return modulo(intval($x), pow(2, 32));
} function pcrc32()
{
for ($d = 0, $f = array(256), $g = 0; 256 != $g; ++$g) {
$d = $g;
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$d = 1 & $d ? -306674912 ^ uright($d, 1) : uright($d, 1);
$f[$g] = $d;
}
//return "undefined" != typeof Int32Array ? new Int32Array($f) : $f;
return $f;
} function pcrc32_str($g,$c)
{
for ($j = -1, $k = -1, $h = -1, $f = 0, $d =strlen($g); $f < $d;)
{
//开始的代码⑴ j = g.charCodeAt(f++),
//$j = $g.charCodeAt($f++); //js代码
$j = JS_charCodeAt($g, $f++); //php代码
//开始的代码⑵ j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] :
if($j < 128)
{
$h = uright($h, 8) ^ $c[255 & ($h ^ $j)]; }
else
{
//开始的代码⑶ j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) :
if($j < 2048)
{
$h = uright($h, 8) ^ $c[255 & ($h ^ (192 | $j >> 6 & 31))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $j))];
}
else
{
//开始的代码⑷ j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCodeAt(f++), h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 15 | (3 & j) << 4))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & k))]) : (h = h >>> 8 ^ c[255 & (h ^ (224 | j >> 12 & 15))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 6 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]);
if($j >= 55296 && $j < 57344)
{
$j = (1023 & $j) + 64;
//$k = 1023 & $g.charCodeAt($f++); //js代码
$k = 1023 & JS_charCodeAt($g, $f++); //php代码
$h = uright($h, 8) ^ $c[255 & ($h ^ (240 | $j >> 8 & 7))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $j >> 2 & 63))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $k >> 6 & 15 | (3 & $j) << 4))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $k))];
}
else
{
$h = uright($h, 8) ^ $c[255 & ($h ^ (224 | $j >> 12 & 15))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | $j >> 6 & 63))];
$h = uright($h, 8) ^ $c[255 & ($h ^ (128 | 63 & $j))];
}
}
} }
return $h ^ -1; } $g = "http://www.baidu.com";
$c = pcrc32();
$res = pcrc32_str($g,$c);
echo ToUint32($res); //js 的代码res >>> 0 翻译过来的 (很深奥) https://www.cnblogs.com/fps2tao/p/9983508.html ?>

//下面就是js的crc32,不过代码被格式化了,和上面的js工具包,一致
<script>
function test01(a) {
var c = function() {
for (var d = 0, f = new Array(256), g = 0; 256 != g; ++g) {
d = g, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, d = 1 & d ? -306674912 ^ d >>> 1 : d >>> 1, f[g] = d
}
return "undefined" != typeof Int32Array ? new Int32Array(f) : f
}(),
b = function(g) {
for (var j, k, h = -1, f = 0, d = g.length; f < d;) {
j = g.charCodeAt(f++),j < 128 ? h = h >>> 8 ^ c[255 & (h ^ j)] : j < 2048 ? (h = h >>> 8 ^ c[255 & (h ^ (192 | j >> 6 & 31))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))]) : j >= 55296 && j < 57344 ? (j = (1023 & j) + 64, k = 1023 & g.charCodeAt(f++), h = h >>> 8 ^ c[255 & (h ^ (240 | j >> 8 & 7))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 2 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | k >> 6 & 15 | (3 & j) << 4))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & k))]) : (h = h >>> 8 ^ c[255 & (h ^ (224 | j >> 12 & 15))], h = h >>> 8 ^ c[255 & (h ^ (128 | j >> 6 & 63))], h = h >>> 8 ^ c[255 & (h ^ (128 | 63 & j))])
}
return h ^ -1
};
return b(a) >>> 0
} var g = "http://www.baidu.com";
ss = test01(g);
console.log(ss); </script>

转:http://blog.sina.com.cn/s/blog_8c0f2f270102xn76.html

参考: http://php.net/manual/zh/function.crc32.php

crc32 : https://baike.baidu.com/item/CRC32/7460858?fr=aladdin

JS 工具函数 方法(其中js的crc32和php的crc32区别)的更多相关文章

  1. js Date 函数方法及日期计算

    js Date 函数方法 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份 ...

  2. JS工具函数汇总

    备注:http://phpjs.org/  这个站点把PHP常用的方法用js实现了,推荐一下 1.从数组中随机获取几个不重复项 //从一个给定的数组arr中,随机返回num个不重复项 function ...

  3. 纯JS前端分页方法(JS分页)

    1.JS分页函数:开发过程中,分页功能一般是后台提供接口,前端只要传page(当前页码)和pageSize(每页最大显示条数)及对应的其他查询条件,就可以返回所需分页显示的数据. 但是有时也需要前端本 ...

  4. JS高级面试题思路(装箱和拆箱、栈和堆、js中sort()方法、.js中Date对象中的getMounth() 需要注意的、开发中编码和解码使用场景有哪些)

    1.装箱和拆箱: 装箱:把基本数据类型转化为对应的引用数据类型的操作: var num = 123 // num var objNum = new Num(123) // object console ...

  5. 常用的js工具函数

    JS选取DOM元素的方法注意:原生JS选取DOM元素比使用jQuery类库选取要快很多1.通过ID选取元素document.getElementById('myid');2.通过CLASS选取元素do ...

  6. js工具函数《转载收藏》

    1.等待所有图片加载 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 var imgObjs = [], count = 0; rotate.prize ...

  7. 更新常用的js工具函数

    在手机调试时打印代码<script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></ ...

  8. 封装一些常用的js工具函数-不定时更新(希望大家积极留言,反馈bug^_^)

    /*华丽------------------------------------------------------------------------------------------------ ...

  9. js Date 函数方法 和 移动端数字键盘调用

    var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); //获取完整的年份(4位,1970-???? ...

随机推荐

  1. JERSEY中文翻译(第一章、Getting Started、1.1.7)

    最近发现jersey特别流行,但是中文资料非常少,深感没有资料的痛苦,所以分享一下看到的内容供他人快速入门. 今天翻译第一章.Getting Started.https://jersey.java.n ...

  2. 算法笔记_228:信用卡号校验(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 当你输入信用卡号码的时候,有没有担心输错了而造成损失呢?其实可以不必这么担心,因为并不是一个随便的信用卡号码都是合法的,它必须通过Luhn算法来验证 ...

  3. go1.8之安装配置

    说明: 之前学习过go语言(大概是0.9版本),后来更新太快,也没怎么使用,就荒废掉了,今年有项目需要用go开发,重新捡起. 这是我在学习go语言过程中整理的内容,这里记录下,也方便我以后查阅. 操作 ...

  4. golang 垃圾回收机制

    用任何带 GC 的语言最后都要直面 GC 问题.在以前学习 C# 的时候就被迫读了一大堆 .NET Garbage Collection 的文档.最近也学习了一番 golang 的垃圾回收机制,在这里 ...

  5. 【Linux】文件夹及作用说明

    Tips Linux关机注意事项: 远程重启服务前,先停止相关服务 使用安全命令重启shutdown –r now,该命令在重启时会正常保存和终止服务器上正在运行的程序 不建议在本地直接对远程服务器关 ...

  6. VTK三维点集轮廓凸包提取

    碰撞检测问题在虚拟现实.计算机辅助设计与制造.游戏及机器人等领域有着广泛的应用,甚至成为关键技术.而包围盒算法是进行碰撞干涉初步检测的重要方法之一.包围盒算法是一种求解离散点集最优包围空间的方法.基本 ...

  7. ORA-01034: ORACLE not available解决

    问题现象: alter user scott account unlock; ERROR at line 1:ORA-01034: ORACLE not availableProcess ID: 0S ...

  8. Heroku免费版限制

    SLEEPS AFTER 30 MINS OF INACTIVITY   30分钟无人访问就休眠 Verified accounts come with a monthly pool of 1000 ...

  9. QQ登录整合/oauth2.0认证-04-调整到QQ互联进行QQ登录

    ---------------------------------目录------------------------------------- QQ登录整合/oauth2.0认证-03-对第二节的代 ...

  10. (原)tensorflow中提示CUDA_ERROR_LAUNCH_FAILED

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6606092.html 参考网址: https://github.com/tensorflow/tens ...