带密钥的MD5加密

时间:2015-04-21 09:51:10

标签: python python-2.7 debian md5 md5sum

我正在尝试使用MD5算法散列txt文件,问题是出于安全原因我想使用特殊键散列文本文件。 其中的想法是让两台机器交换消息,并使用密钥通过消息的MD5散列应用安全检查。 并且没有人应该知道这个特定的密钥。

我的机器是基于Linux的(Debian OS)。 我正在使用python作为编程语言。

我已经通过互联网挖掘了我可以在python中找到一个MD5库,但它可以选择更改使用的密钥

import md5

import string

hash = md5.new()

hash.update("this is the text to be hashed")

value = hash.digest()

print hash.hexdigest()

我也尝试使用Debian标准库MD5sum,但我仍然无法想象如何更改用于创建哈希的密钥。

感谢任何人有任何想法,我不能使用任何在线工具,因为我想在我的python程序中包含这个MD5哈希或至少使用标准的Debian库。

我不能使用任何C#或php或除python和标准linux库之外的任何其他编程工具。

非常感谢

2 个答案:

答案 0 :(得分:1)

您可以使用python中的hmac模块对消息进行密钥散列。更多信息here

这是一个示例程序:

import hmac
import md5

hash_key = 'secret hashing key'
hash = hmac.new(hash_key, 'this is the text to be hashed', md5)
value = hash.digest()
print hash.hexdigest()

答案 1 :(得分:0)

非常感谢您的反馈和答案,我找到了一种使用称为HMAC(哈希消息身份验证代码)的密钥身份验证的方法 而它

echo -n'value'| openssl dgst -md5 -hmac'key'

(stdin)= 01433efd5f16327ea4b31144572c67f6

有关更多信息,请参阅openssl库

openssl dgst [-md5 | -md4 | -md2 | -sha1 | -sha | -mdc2 | -ripemd160 | -dss1] [-c] [-d] [-hex] [-binary] [-out filename ] [-sign filename] [-keyform arg] [-passin arg] [-verify filename] [-prverify filename] [-signature filename] [-hmac key] [file ...] [md5 | md4 | md2 | sha1 | sha | mdc2 | ripemd160] [-c] [-d] [file ...]

密钥认证的解释如下:

-hmac key:使用“key”创建一个散列MAC。

-mac alg:创建MAC(密钥消息认证码)。

最流行的MAC算法是HMAC(基于散列的MAC),但是还有其他MAC算法不基于散列,例如gost-mac算法,由ccgost引擎支持。应通过-macopt参数设置MAC密钥和其他选项。

供参考,我使用了以下博客和MD5手册页

http://nwsmith.blogspot.com/2012/07/using-openssl-to-generate-hmac-using.html

http://linux.die.net/man/1/md5