简体   繁体   English

在python中将md5值转换为相应的SHA1和SHA256?

[英]Convert a md5 value to corresponding SHA1 and SHA256 in python?

Here in a script I take hash as user input and based on the length I am calculating whether it is md5 or sha1 or sha256.在脚本中,我将哈希作为用户输入,并根据我计算的长度是 md5 还是 sha1 或 sha256。 But now the problem is convert every hash to corresponding other hashes and store in a variables.但是现在的问题是将每个散列转换为相应的其他散列并存储在变量中。

I tried this hashlib from a StackOverflow answer.我从 StackOverflow 的答案中尝试了这个 hashlib。

>>> import hashlib
>>> s = 'something to hash'
>>> sb = s.encode("utf8")
>>> hashlib.md5(sb)
<md5 HASH object @ 0x7f36f5ce3440>
>>> hashlib.md5(sb).hexdigest()
'6f4815fdf1f1fd3f36ac295bf39d26b4'
>>> hashlib.sha1(sb).hexdigest()
'72668bc961b0a78bfa1633f6141bcea69ca37468'

But this solves when a text is given but in my case direct hash is given like this.但这在给出文本时解决了,但在我的情况下,直接散列是这样给出的。

hash_value = sys.argv[1]

#print len(hash_value)
if len(hash_value) == 64:
        type_of_hash = 'sha256'
elif len(hash_value) == 40:
        type_of_hash = 'sha1'
elif len(hash_value) == 32:
        type_of_hash = 'md5'
else:
        print "Invalid Hash"

Now all I wanted to do is convert hash_value to corresponding others, suppose given input is sha256 then I need to convert the hash value to other formats.现在我想做的就是将 hash_value 转换为相应的其他格式,假设给定的输入是 sha256 那么我需要将哈希值转换为其他格式。

Any suggestions on how to solve this?有关如何解决此问题的任何建议?

You can't do what you want to do.你不能做你想做的事。 To generate the 3 hashes you need to process the same input three times with different algorithms.要生成 3 个散列,您需要使用不同的算法处理相同的输入 3 次。

If any of these could be derived from the any of the others, there would be no reason to have have such algorithm because there would be no advantage in using one or the other in terms of security.如果其中任何一个可以从其他任何一个派生出来,那么就没有理由拥有这样的算法,因为在安全性方面使用其中一个没有任何优势。

Below is the program that i wrote to get SHA-256 and SHA-1 from MD5 using virustotal API.下面是我编写的使用virustotal API从MD5获取SHA-256和SHA-1的程序。 Hope it helps:希望能帮助到你:

 pip3 install vt-py

 import vt,pandas
    client = vt.Client("Virus Total API Key")
           
    data=pandas.read_csv("path of MD5 File ")
    
    Column1=list(data['Name of Column of MD5 File containing hashes'])
    
    with open("path of new SHA-256 file you want to create ", "a") as myfile:
    
      with open("path of new SHA-1 file you want to create", "a") as myfile2:
    
    
       for i in Column1:
        file = client.get_object("/files/{}".format(i))
        myfile.write("{}\n".format(file.sha256))
        myfile2.write("{}\n".format(file.sha1))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM