python 代码 加密 哈希_Python实现的hash加密小工具详解

这个Python脚本提供了一个简单的哈希加密工具,支持MD5、SHA1、SHA224、SHA256、SHA384和SHA512等算法。用户可以选择单个字符串加密或批量处理文本文件中的字符串,加密后的哈希值会被保存到指定的输出文件中。
摘要由CSDN通过智能技术生成

[Python]代码

#!/usr/bin/env python

#-*-encoding = utf-8-*-

# hasher.py

[email protected]: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()

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值