javascript MD5加密
/*
* Javascript MD5 library - version 0.4
*
* Coded (2011) by Luigi Galli - LG@4e71.org -
* http://faultylabs.com
*/
if (typeof faultylabs == 'undefined') {
faultylabs = {}
} /*
* MD5() 输出为十六进制大写字符串
*/ faultylabs.MD5 = function(data) {
// convert number to (unsigned) 32 bit hex, zero filled string
function to_zerofilled_hex(n) {
var t1 = (n >>> 0).toString(16)
return "00000000".substr(0, 8 - t1.length) + t1
} // convert array of chars to array of bytes
function chars_to_bytes(ac) {
var retval = []
for (var i = 0; i < ac.length; i++) {
retval = retval.concat(str_to_bytes(ac[i]))
}
return retval
} // convert a 64 bit unsigned number to array of bytes. Little endian
function int64_to_bytes(num) {
var retval = []
for (var i = 0; i < 8; i++) {
retval.push(num & 0xFF)
num = num >>> 8
}
return retval
} // 32 bit left-rotation
function rol(num, places) {
return ((num << places) & 0xFFFFFFFF) | (num >>> (32 - places))
} // The 4 MD5 functions
function fF(b, c, d) {
return (b & c) | (~b & d)
} function fG(b, c, d) {
return (d & b) | (~d & c)
} function fH(b, c, d) {
return b ^ c ^ d
} function fI(b, c, d) {
return c ^ (b | ~d)
} // pick 4 bytes at specified offset. Little-endian is assumed
function bytes_to_int32(arr, off) {
return (arr[off + 3] << 24) | (arr[off + 2] << 16)
| (arr[off + 1] << 8) | (arr[off])
} /*
* Conver string to array of bytes in UTF-8 encoding See:
* http://www.dangrossman.info/2007/05/25/handling-utf-8-in-javascript-php-and-non-utf8-databases/
* http://stackoverflow.com/questions/1240408/reading-bytes-from-a-javascript-string
* How about a String.getBytes(<ENCODING>) for Javascript!? Isn't it time
* to add it?
*/
function str_to_bytes(str) {
var retval = []
for (var i = 0; i < str.length; i++)
if (str.charCodeAt(i) <= 0x7F) {
retval.push(str.charCodeAt(i))
} else {
var tmp = encodeURIComponent(str.charAt(i)).substr(1)
.split('%')
for (var j = 0; j < tmp.length; j++) {
retval.push(parseInt(tmp[j], 0x10))
}
}
return retval
} // convert the 4 32-bit buffers to a 128 bit hex string. (Little-endian is
// assumed)
function int128le_to_hex(a, b, c, d) {
var ra = ""
var t = 0
var ta = 0
for (var i = 3; i >= 0; i--) {
ta = arguments[i]
t = (ta & 0xFF)
ta = ta >>> 8
t = t << 8
t = t | (ta & 0xFF)
ta = ta >>> 8
t = t << 8
t = t | (ta & 0xFF)
ta = ta >>> 8
t = t << 8
t = t | ta
ra = ra + to_zerofilled_hex(t)
}
return ra
} // conversion from typed byte array to plain javascript array
function typed_to_plain(tarr) {
var retval = new Array(tarr.length)
for (var i = 0; i < tarr.length; i++) {
retval[i] = tarr[i]
}
return retval
} // check input data type and perform conversions if needed
var databytes = null
// String
var type_mismatch = null
if (typeof data == 'string') {
// convert string to array bytes
databytes = str_to_bytes(data)
} else if (data.constructor == Array) {
if (data.length === 0) {
// if it's empty, just assume array of bytes
databytes = data
} else if (typeof data[0] == 'string') {
databytes = chars_to_bytes(data)
} else if (typeof data[0] == 'number') {
databytes = data
} else {
type_mismatch = typeof data[0]
}
} else if (typeof ArrayBuffer != 'undefined') {
if (data instanceof ArrayBuffer) {
databytes = typed_to_plain(new Uint8Array(data))
} else if ((data instanceof Uint8Array) || (data instanceof Int8Array)) {
databytes = typed_to_plain(data)
} else if ((data instanceof Uint32Array)
|| (data instanceof Int32Array)
|| (data instanceof Uint16Array)
|| (data instanceof Int16Array)
|| (data instanceof Float32Array)
|| (data instanceof Float64Array)) {
databytes = typed_to_plain(new Uint8Array(data.buffer))
} else {
type_mismatch = typeof data
}
} else {
type_mismatch = typeof data
} if (type_mismatch) {
alert('MD5 type mismatch, cannot process ' + type_mismatch)
} function _add(n1, n2) {
return 0x0FFFFFFFF & (n1 + n2)
} return do_digest() function do_digest() { // function update partial state for each run
function updateRun(nf, sin32, dw32, b32) {
var temp = d
d = c
c = b
// b = b + rol(a + (nf + (sin32 + dw32)), b32)
b = _add(b, rol(_add(a, _add(nf, _add(sin32, dw32))), b32))
a = temp
} // save original length
var org_len = databytes.length // first append the "1" + 7x "0"
databytes.push(0x80) // determine required amount of padding
var tail = databytes.length % 64
// no room for msg length?
if (tail > 56) {
// pad to next 512 bit block
for (var i = 0; i < (64 - tail); i++) {
databytes.push(0x0)
}
tail = databytes.length % 64
}
for (i = 0; i < (56 - tail); i++) {
databytes.push(0x0)
}
// message length in bits mod 512 should now be 448
// append 64 bit, little-endian original msg length (in *bits*!)
databytes = databytes.concat(int64_to_bytes(org_len * 8)) // initialize 4x32 bit state
var h0 = 0x67452301
var h1 = 0xEFCDAB89
var h2 = 0x98BADCFE
var h3 = 0x10325476 // temp buffers
var a = 0, b = 0, c = 0, d = 0 // Digest message
for (i = 0; i < databytes.length / 64; i++) {
// initialize run
a = h0
b = h1
c = h2
d = h3 var ptr = i * 64 // do 64 runs
updateRun(fF(b, c, d), 0xd76aa478, bytes_to_int32(databytes, ptr),
7)
updateRun(fF(b, c, d), 0xe8c7b756, bytes_to_int32(databytes, ptr
+ 4), 12)
updateRun(fF(b, c, d), 0x242070db, bytes_to_int32(databytes, ptr
+ 8), 17)
updateRun(fF(b, c, d), 0xc1bdceee, bytes_to_int32(databytes, ptr
+ 12), 22)
updateRun(fF(b, c, d), 0xf57c0faf, bytes_to_int32(databytes, ptr
+ 16), 7)
updateRun(fF(b, c, d), 0x4787c62a, bytes_to_int32(databytes, ptr
+ 20), 12)
updateRun(fF(b, c, d), 0xa8304613, bytes_to_int32(databytes, ptr
+ 24), 17)
updateRun(fF(b, c, d), 0xfd469501, bytes_to_int32(databytes, ptr
+ 28), 22)
updateRun(fF(b, c, d), 0x698098d8, bytes_to_int32(databytes, ptr
+ 32), 7)
updateRun(fF(b, c, d), 0x8b44f7af, bytes_to_int32(databytes, ptr
+ 36), 12)
updateRun(fF(b, c, d), 0xffff5bb1, bytes_to_int32(databytes, ptr
+ 40), 17)
updateRun(fF(b, c, d), 0x895cd7be, bytes_to_int32(databytes, ptr
+ 44), 22)
updateRun(fF(b, c, d), 0x6b901122, bytes_to_int32(databytes, ptr
+ 48), 7)
updateRun(fF(b, c, d), 0xfd987193, bytes_to_int32(databytes, ptr
+ 52), 12)
updateRun(fF(b, c, d), 0xa679438e, bytes_to_int32(databytes, ptr
+ 56), 17)
updateRun(fF(b, c, d), 0x49b40821, bytes_to_int32(databytes, ptr
+ 60), 22)
updateRun(fG(b, c, d), 0xf61e2562, bytes_to_int32(databytes, ptr
+ 4), 5)
updateRun(fG(b, c, d), 0xc040b340, bytes_to_int32(databytes, ptr
+ 24), 9)
updateRun(fG(b, c, d), 0x265e5a51, bytes_to_int32(databytes, ptr
+ 44), 14)
updateRun(fG(b, c, d), 0xe9b6c7aa, bytes_to_int32(databytes, ptr),
20)
updateRun(fG(b, c, d), 0xd62f105d, bytes_to_int32(databytes, ptr
+ 20), 5)
updateRun(fG(b, c, d), 0x2441453, bytes_to_int32(databytes, ptr
+ 40), 9)
updateRun(fG(b, c, d), 0xd8a1e681, bytes_to_int32(databytes, ptr
+ 60), 14)
updateRun(fG(b, c, d), 0xe7d3fbc8, bytes_to_int32(databytes, ptr
+ 16), 20)
updateRun(fG(b, c, d), 0x21e1cde6, bytes_to_int32(databytes, ptr
+ 36), 5)
updateRun(fG(b, c, d), 0xc33707d6, bytes_to_int32(databytes, ptr
+ 56), 9)
updateRun(fG(b, c, d), 0xf4d50d87, bytes_to_int32(databytes, ptr
+ 12), 14)
updateRun(fG(b, c, d), 0x455a14ed, bytes_to_int32(databytes, ptr
+ 32), 20)
updateRun(fG(b, c, d), 0xa9e3e905, bytes_to_int32(databytes, ptr
+ 52), 5)
updateRun(fG(b, c, d), 0xfcefa3f8, bytes_to_int32(databytes, ptr
+ 8), 9)
updateRun(fG(b, c, d), 0x676f02d9, bytes_to_int32(databytes, ptr
+ 28), 14)
updateRun(fG(b, c, d), 0x8d2a4c8a, bytes_to_int32(databytes, ptr
+ 48), 20)
updateRun(fH(b, c, d), 0xfffa3942, bytes_to_int32(databytes, ptr
+ 20), 4)
updateRun(fH(b, c, d), 0x8771f681, bytes_to_int32(databytes, ptr
+ 32), 11)
updateRun(fH(b, c, d), 0x6d9d6122, bytes_to_int32(databytes, ptr
+ 44), 16)
updateRun(fH(b, c, d), 0xfde5380c, bytes_to_int32(databytes, ptr
+ 56), 23)
updateRun(fH(b, c, d), 0xa4beea44, bytes_to_int32(databytes, ptr
+ 4), 4)
updateRun(fH(b, c, d), 0x4bdecfa9, bytes_to_int32(databytes, ptr
+ 16), 11)
updateRun(fH(b, c, d), 0xf6bb4b60, bytes_to_int32(databytes, ptr
+ 28), 16)
updateRun(fH(b, c, d), 0xbebfbc70, bytes_to_int32(databytes, ptr
+ 40), 23)
updateRun(fH(b, c, d), 0x289b7ec6, bytes_to_int32(databytes, ptr
+ 52), 4)
updateRun(fH(b, c, d), 0xeaa127fa, bytes_to_int32(databytes, ptr),
11)
updateRun(fH(b, c, d), 0xd4ef3085, bytes_to_int32(databytes, ptr
+ 12), 16)
updateRun(fH(b, c, d), 0x4881d05, bytes_to_int32(databytes, ptr
+ 24), 23)
updateRun(fH(b, c, d), 0xd9d4d039, bytes_to_int32(databytes, ptr
+ 36), 4)
updateRun(fH(b, c, d), 0xe6db99e5, bytes_to_int32(databytes, ptr
+ 48), 11)
updateRun(fH(b, c, d), 0x1fa27cf8, bytes_to_int32(databytes, ptr
+ 60), 16)
updateRun(fH(b, c, d), 0xc4ac5665, bytes_to_int32(databytes, ptr
+ 8), 23)
updateRun(fI(b, c, d), 0xf4292244, bytes_to_int32(databytes, ptr),
6)
updateRun(fI(b, c, d), 0x432aff97, bytes_to_int32(databytes, ptr
+ 28), 10)
updateRun(fI(b, c, d), 0xab9423a7, bytes_to_int32(databytes, ptr
+ 56), 15)
updateRun(fI(b, c, d), 0xfc93a039, bytes_to_int32(databytes, ptr
+ 20), 21)
updateRun(fI(b, c, d), 0x655b59c3, bytes_to_int32(databytes, ptr
+ 48), 6)
updateRun(fI(b, c, d), 0x8f0ccc92, bytes_to_int32(databytes, ptr
+ 12), 10)
updateRun(fI(b, c, d), 0xffeff47d, bytes_to_int32(databytes, ptr
+ 40), 15)
updateRun(fI(b, c, d), 0x85845dd1, bytes_to_int32(databytes, ptr
+ 4), 21)
updateRun(fI(b, c, d), 0x6fa87e4f, bytes_to_int32(databytes, ptr
+ 32), 6)
updateRun(fI(b, c, d), 0xfe2ce6e0, bytes_to_int32(databytes, ptr
+ 60), 10)
updateRun(fI(b, c, d), 0xa3014314, bytes_to_int32(databytes, ptr
+ 24), 15)
updateRun(fI(b, c, d), 0x4e0811a1, bytes_to_int32(databytes, ptr
+ 52), 21)
updateRun(fI(b, c, d), 0xf7537e82, bytes_to_int32(databytes, ptr
+ 16), 6)
updateRun(fI(b, c, d), 0xbd3af235, bytes_to_int32(databytes, ptr
+ 44), 10)
updateRun(fI(b, c, d), 0x2ad7d2bb, bytes_to_int32(databytes, ptr
+ 8), 15)
updateRun(fI(b, c, d), 0xeb86d391, bytes_to_int32(databytes, ptr
+ 36), 21) // update buffers
h0 = _add(h0, a)
h1 = _add(h1, b)
h2 = _add(h2, c)
h3 = _add(h3, d)
}
// Done! Convert buffers to 128 bit (LE)
return int128le_to_hex(h3, h2, h1, h0).toUpperCase()
} }
当时用这个插件是因为在用jquery选择器的时候,id包含了特殊字符和jquery冲突,所以就对ID进行了加密处理... 显然是个笨办法,但也解决了实际问题。 推荐一下...
javascript MD5加密的更多相关文章
- asp.net && javascript MD5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- Javascript实现MD5加密
/* * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message * Digest Algorithm, as d ...
- 【javascript类库】zepto和jquery的md5加密插件
[javascript类库]zepto和jquery的md5加密插件 相信很多人对jQuery并不陌生,这款封装良好的插件被很多开发者使用. zepto可以说是jQuery在移动端的替代产品,它比jQ ...
- 解决Javascript md5 和 Java md5 中文加密后不同问题
Javascript md5 和 Java md5 带中文字符加密结果不一致,可以通过编码进行转化. javascript可以使用encodeURLComponent将中文先转化一次再进行MD5加密. ...
- JavaScript的MD5加密
1.首先要到http://pajhome.org.uk/crypt/md5/下载js文件. 2.在页面文件中添加: <script type="text/javascript" ...
- javascript 生成MD5加密
进行HTTP网络通信的时候,调用API向服务器请求数据,有时为了防止API调用过程中被黑客恶意篡改,所请求参数需要进行MD5算法计算,得到摘要签名.服务端会根据请求参数,对签名进行验证,签名不合法的请 ...
- javascript md5 二次加密 和 java md5 二次加密结果不同
最近研究httpclient post 时遇到了一个问题,很费解. js md5(str) 和 java md5(str),第一次md5 加密结果一样,(当时忽略了大小写问题,java 大写,js小 ...
- JS中使用MD5加密
下载 MD5 使用MD5加密的方法:下载md5.js文件,在网页中引用该文件: < script type="text/javascript" src="md5.j ...
- 一个简单的后台与数据库交互的登录与注册[sql注入处理,以及MD5加密]
一.工具: vs2013[因为我现在用的也是2013,版本随便你自己开心] sql2008[准备过久升级] 二.用到的语言: HTML+CSS+Jquery+Ajax+sqlserver HTML[相 ...
随机推荐
- vs2013update4 vs-mda-remote cordova真机测试ios 解决里面一个坑
sudo npm install -g vs-mda-remote --user=你的用户名 此步骤为安装vs-mda-remote,如果安装成功 执行vs-mda-remote –secure fa ...
- 阿里云的esc
云服务器ecs作用如下:1.完全管理权限:对云服务器的操作系统有完全控制权,用户可以通过连接管理终端自助解决系统问题,进行各项操作:2.快照备份与恢复:对云服务器的磁盘数据生成快照,用户可使用快照回滚 ...
- STM32 枚举类型和结构体的使用
结构体就是一个可以包含不同数据类型的一个结构,它是一种可以自己定义的数据类型. 首先结构体可以在一个结构中声明不同的数据类型. 第二相同结构的结构体变量是可以相互赋值的,而 ...
- BZOJ 2432 兔农
Description 农夫栋栋近年收入不景气,正在他发愁如何能多赚点钱时,他听到隔壁的小朋友在讨论兔子繁殖的问题. 问题是这样的:第一个月初有一对刚出生的小兔子,经过两个月长大后,这对兔子从第三个月 ...
- [BZOJ 1042] [HAOI2008] 硬币购物 【DP + 容斥】
题目链接:BZOJ - 1042 题目分析 首先 Orz Hzwer ,代码题解都是看的他的 blog. 这道题首先使用DP预处理,先求出,在不考虑每种硬币个数的限制的情况下,每个钱数有多少种拼凑方案 ...
- Altium Designer 从导入DXF文件,并转换成板框
大多数人都知道,PADS中导入DXF文件,然后转换成板框,是很方便的.AD也同样可以做到. PADS导入DXF见:http://www.cnblogs.com/craftor/archive/2012 ...
- Spring: DispacherServlet和ContextLoaderListener中的WebApplicationContext的关系
在Web容器(比如Tomcat)中配置Spring时,你可能已经司空见惯于web.xml文件中的以下配置代码: <context-param> <param-name>cont ...
- sublime每次打开时都提示升级,怎么取消这个弹出框?
答案其实很简单,设置如下: 进入Preferences -> Settings-User ,添加 "update_check": false 重启Sublime. 发现了什么 ...
- 【转】Android JNI编程—JNI基础
原文网址:http://www.jianshu.com/p/aba734d5b5cd 最近看到了很多关于热补的开源项目——Depoxed(阿里).AnFix(阿里).DynamicAPK(携程)等,它 ...
- HDOJ 2073 无限的路
Problem Description 甜甜从小就喜欢画图画,最近他买了一支智能画笔,由于刚刚接触,所以甜甜只会用它来画直线,于是他就在平面直角坐标系中画出如下的图形: 甜甜的好朋友蜜蜜发现上面的图还 ...