前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Python实现的hash加密小工具

Python实现的hash加密小工具

原创
作者头像
艳艳代码杂货店
修改2021-11-02 11:20:23
5400
修改2021-11-02 11:20:23
举报

[Python]代码    

代码语言:javascript
复制
#!/usr/bin/env python
#-*-encoding = utf-8-*-
# hasher.py
#@By:dengyongkai

import sys
import hashlib
import linecache

def alglist():
    print """
    (1)MD5     (4)SHA256
    (2)SHA1    (5)SHA384
    (3)SHA224  (6)SHA512 """

class hasher(object):
    def single(self):
        try:
            alglist()
            alg = raw_input("Select an algorithm:")
            functions = {"1":hashlib.md5,"2":hashlib.sha1,'3':hashlib.sha224,'4':hashlib.sha256,'5':hashlib.sha384,'6':hashlib.sha512}
            if alg in functions.keys():
                hashob = functions[alg]()
                outfile = raw_input("Enter output file:")
                while 1:
                    word = raw_input("Enter string: ")
                    hashob.update(word)
                    hashword = hashob.hexdigest()
                    hashlst = open(outfile, 'a+')
                    hashlst.write(hashword + '\n')
                    print '[*] Output successful...'
                    raw_input("[*] Press Return to continue OR 'Ctrl-C' to Quit...")
            else:
                alglist()
                print "[*] Your input not found..."
                sys.exit(1)

        except KeyboardInterrupt:
#            main()
            raise
        except IOError:
            print "[*] Input file not found..."
            sys.exit(1)
#            hasher.single()
    def hashlist(self):
        try:
            alglist()
            alg = raw_input("Select an algorithm:")
            functions = {'1':hashlib.md5, '2':hashlib.sha1, '3':hashlib.sha224, '4':hashlib.sha256, '5':hashlib.sha384, '6':hashlib.sha512}
            if alg in functions.keys():
                hashob = functions[alg]()
                infile = raw_input("Enter input file:")
                outfile = raw_input("Enter output file:")
                count = len(open(infile).readlines())
                i = 1
                while i <= count + 1:
                    word = linecache.getline(infile,i)
                    hashob.update(word)
                    hashword = hashob.hexdigest()
                    outlist = open(outfile,'a++')
                    outlist.write(hashword+'\n')
                    i += 1
                print "[*]Output successful..."
                raw_input("Press enter to return to the main menu OR 'Ctrl-C' to Quit...")
                main()
            else:
                alglist()
                print "[*] Your input not found..."                                  
                sys.exit(1)
        except KeyboardInterrupt:
#            main()
            raise
        except IOError:
            print "[*] Input file not found..."
            sys.exit(1)
#            main()

def main():
    try:
        print """
        O))                      O))                                 
        O))        O))     O)))) O))        O))      O))    O)) O))  
        O) O)    O))  O)) O))    O) O)    O))  O)) O)   O))  O))  O))
        O))  O))O))   O))   O))) O))  O))O))   O))O))))) O)) O))  O))
        O)   O))O))   O))     O))O)   O)) O))  O))O)         O))  O))
        O))  O))  O)) O)))O)0)O))O))  O))     O))   O))))   O)))  O))
                                          O))        

               http://blog.sina.com.cn/kaiyongdeng




        (1) Single Mode - Enter strings to be hashed one by one
        (2) List Mode - Input a text file of strings to be hashed"""

        mode = raw_input("Select a mode:")
        if mode == '1':
            hasher.single()
        elif mode == '2':
            hasher.hashlist()
        else:
            main()
    except KeyboardInterrupt:
        print "\n[*] Exiting..."
        sys.exit(1)


if __name__ == "__main__":
    hasher = hasher()
    main()

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • [Python]代码    
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档