Crane
Table_bottom

Search
Loading
Table_bottom

分类
Table_bottom

随机文章
Table_bottom

标签云
Table_bottom

最新评论
Table_bottom

链接
Table_bottom

功能
Table_bottom

计算字符串md5的几个方法

Crane posted @ 2010年9月14日 17:10 in Linux with tags linux md5 php python , 10518 阅读

很多时候我们都需要计算md5用来做校验或者什么的,比如下了个iso,要做个校验看下到的文件是否没有损坏,从而刻盘啊什么的。

这样的需求很好办,直接使用linux提供的工具即可:

md5sum filename

但是有时候我们只需要计算一个字符串的md5,这时可能很自然的想把这个字符串放进一个文件中,再计算这个文件的md5不就行了么。其实这种方法有个问题,就是在文件中这个字符串的末尾有不可见字符,比如换行,这样算出来的就不是原来的字符串的md5了,而是加上这个换行后的,所以这种方法不可取。

 

一般来说还是有好多方法的,下面大概举出四种:

一、利用openssl工具

方法是这样的:

$ openssl
OpenSSL> md5
the string(ctrl+d结束)

例如:

$ openssl
OpenSSL> md5
helloworld(stdin)= fc5e038d38a57032085441e7fe7010b0

二、利用python的库

把下面的保存成py文件(比如md5.py):

import sys
import hashlib
m=hashlib.md5()
m.update(sys.argv[1])
print m.hexdigest()                   

然后运行

$ python md5.py helloworld
fc5e038d38a57032085441e7fe7010b0

三、利用php的函数

简单编辑一个php文件:

<?php
echo md5("helloworld");
?>

然后在浏览器中请求这个页面(本地要有web服务器)就可得到结果。

四、利用在线md5工具

如果上面的环境都不具备的话,可以采用这个只要能上网就能用的方法。

在google里面随便搜索一下,比如这个 http://www.google.com/search?q=md5+online

或者直接用这个 http://www.md5-creator.com/

Cheap Essay Writing 说:
2020年7月29日 18:36

fantastic sentiment of style. I value examining your blog passages since you by and large give me inspiration to have a go at something new and one of a kind. Last time I am visit essay makes students redesign standard work forming limits. Other than the school syllabus, forming limit is in like way anticipated that would grow self-progression.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter