JavaScript MD5 加密

js实现MD5加密可用于网页前端对字符串加密处理,可用于减轻server端的加密运算负担

官网:http://pajhome.org.uk/crypt/md5/index.html

下载地址:

下载文件 点击下载此文件: http://pajhome.org.uk/crypt/md5/jshash-2.2.zip

官网介绍使用方法:
Common Usage

First download the bundle. For common use, I recommend using the minified scripts, which have been processed by the YUI Compressor. Note that these only support utf-8 input and hex output. Use it like this:

<script type="text/javascript" src="md5-min.js"></script>
<script type="text/javascript">
    hash = hex_md5("string");
    hmac = hex_hmac_md5("key", "data");
</script>

The usage for other hashes is essentially the same - sha1, sha256, sha512 or rmd160.
Other Output Encodings

The scripts support base64 encoding, although you must use the full script, not the minified version. If necessary, you can create you own minified script, with the functionality you need. Use it like this:

<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
    hash = b64_md5("string");
    hmac = b64_hmac_md5("key", "data");
</script>

There is also a mode called "any output encoding". This lets you specify a string of characters, and all those characters will be used to encode the password. The string can be any length - it does not need to be a power of 2. This is useful for applications like password generation, when you want to get as much unpredictability as possible into a short password. Use it like this:

<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
    hash = any_md5("string", "encoding");
</script>

If the encoding is 0123456789ABCDEF the output will be identical to hex_md5. It isn't possible to create output that's identical to base64 encoding.
Advanced Usage

If you want to use more advanced features, such as multiple repetitions of a hash, or utf-16 encoding, you need to use a slightly lower-level interface to the scripts. These have the concept of a "raw string"; this is a JavaScript string, but all the characters are between 0 and 255 - essentially a binary array. To get a hex hash, using utf-16 encoding:

<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
    hash = rstr2hex(rstr_md5(str2rstr_utf16le("string")));
</script>

You can also use str2rstr_utf16be. To perform a double hash:

<script type="text/javascript" src="md5.js"></script>
<script type="text/javascript">
    hash = rstr2hex(rstr_md5(rstr_md5(str2rstr_utf8("string"))));
</script>

You can use variants of this to produce just about any hash you may need.
Unit Tests

To run the unit tests, you will need Python 2.5 or newer. The script test.py generates an HTML file that runs the tests:

python test.py > test.html

Next, open test.html in a browser to run the tests, and see the results. If you want to test the minified versions of the scripts, use test-min.py.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值