python实现md5算法_Python生成MD5值的两种方法实例分析

本文实例讲述了Python生成MD5值的两种方法。分享给大家供大家参考,具体如下:

# -*- coding:utf-8 -*-

import datetime

# NO.1 使用MD5

import md5

src = 'this is a md5 test.'

m1 = md5.new()

m1.update(src)

print m1.hexdigest()

运行结果:

174b086fc6358db6154bd951a8947837

# -*- coding:utf-8 -*-

# NO.2 使用hashlib

import hashlib

src = 'this is a md5 test.'

m2 = hashlib.md5()

m2.update(src)

print m2.hexdigest()

运行结果:

174b086fc6358db6154bd951a8947837

对于同一个字符串而言,使用MD5和使用hashlib生成的MD5值是一样的

以下是使用file+时间戳生成一个唯一的MD5值

# -*- coding:utf-8 -*-

import md5

import time

now = 'file'+str(time.time())

print now,type(now)

m0 = md5.new()

m0.update(now)

print m0.hexdigest()

运行结果:

file1556241051.38

efdc1e1d6bbe949afb2cd0250d0244d2

############### 封装成函数 ###############################

# -*- coding:utf-8 -*-

import time

import hashlib

src = 'file'+str(time.time())

print src,type(src)

m2 = hashlib.md5()

m2.update(src)

file_id = m2.hexdigest()

print file_id,type(file_id)

def make_file_id(src):

m1 = hashlib.md5()

m1.update(src)

return m1.hexdigest()

src = 'filed_46546546464631361sdfsdfgsdgfsdgdsgfsd'+str(time.time())

print make_file_id(src)

运行结果:

file1556241114.08

4d826f2298853d5f5ae209d6bf754b62

e6c5ad9dd0fa4f3d141f94b7c990710e

PS:关于加密解密感兴趣的朋友还可以参考本站在线工具:

文字在线加密解密工具(包含AES、DES、RC4等):http://tools.jb51.net/password/txt_encode

MD5在线加密工具:http://tools.jb51.net/password/CreateMD5Password

在线散列/哈希算法加密工具:http://tools.jb51.net/password/hash_encrypt

在线MD5/hash/SHA-1/SHA-2/SHA-256/SHA-512/SHA-3/RIPEMD-160加密工具:http://tools.jb51.net/password/hash_md5_sha

在线sha1/sha224/sha256/sha384/sha512加密工具:http://tools.jb51.net/password/sha_encode

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python加密解密算法与技巧总结》、《Python编码操作技巧总结》、《Python文件与目录操作技巧汇总》、《Python数据结构与算法教程》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》及《Python入门与进阶经典教程》

希望本文所述对大家Python程序设计有所帮助。

本文标题: Python生成MD5值的两种方法实例分析

本文地址: http://www.cppcns.com/jiaoben/python/257703.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值