def GetIntegrity(self, data):
"""Calculate the integirty value of given data using remote peers hash"""
if self.hash_type == None:
return None
elif self.hash_type == 0:
# SHA-1
return hashlib.sha1(data).digest()
elif self.hash_type == 1:
# SHA-224
return hashlib.sha224(data).digest()
elif self.hash_type == 2:
# SHA-256
return hashlib.sha256(data).digest()
elif self.hash_type == 3:
# SHA-384
return hashlib.sha384(data).digest()
elif self.hash_type == 4:
# SHA-512
return hashlib.sha512(data).digest()
else:
return None
python类sha1()的实例源码
def add_credentials_options(self):
"""Add credentials to the Options dictionary (if necessary)."""
api_username = self.configuration.username
api_key = self.configuration.password
self.options['api_username'] = api_username
if self.configuration.secure_auth == True:
timestamp = datetime.utcnow().strftime('%Y-%m-%dT%H:%M:%SZ')
uri = '/' + self.configuration.sub_url + ('/' if self.domain_name.strip()=='' else '/' + self.domain_name + '/') + self.service_name
self.options['timestamp'] = timestamp
params = ''.join([api_username, timestamp, uri])
self.options['signature'] = hmac.new(api_key, params, digestmod=hashlib.sha1).hexdigest()
else:
self.options['api_key'] = api_key
def copy_from_host(module):
compress = module.params.get('compress')
src = module.params.get('src')
if not os.path.exists(src):
module.fail_json(msg="file not found: {}".format(src))
if not os.access(src, os.R_OK):
module.fail_json(msg="file is not readable: {}".format(src))
mode = oct(os.stat(src).st_mode & 0o777)
with open(src, 'rb') as f:
raw_data = f.read()
sha1 = hashlib.sha1(raw_data).hexdigest()
data = zlib.compress(raw_data) if compress else raw_data
module.exit_json(content=base64.b64encode(data), sha1=sha1, mode=mode,
source=src)
webhook_helper.py 文件源码
项目:repository-gardener
作者: GoogleCloudPlatform
项目源码
文件源码
阅读 25
收藏 0
点赞 0
评论 0
def check_signature(header_signature, request_body):
if not header_signature:
raise ValueError('No X-Hub-Signature header.')
algorithm, signature_digest = header_signature.split('=')
if algorithm != 'sha1':
raise ValueError('Unsupported digest algorithm {}.'.format(algorithm))
body_digest = hmac.new(
webhook_secret(), msg=request_body, digestmod=hashlib.sha1).hexdigest()
if not hmac.compare_digest(body_digest, signature_digest):
raise ValueError('Body digest did not match signature digest')
return True
def client_proc(host, port, input, task=None):
# client reads input file and sends data in chunks
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = pycos.AsyncSocket(sock)
yield sock.connect((host, port))
# data can be written to this asynchronous socket; however, for
# illustration, convert its file descriptor to asynchronous file
# and write to that instead
afd = pycos.asyncfile.AsyncFile(sock)
input = open(input)
csum = hashlib.sha1()
while True:
data = os.read(input.fileno(), 16*1024)
if not data:
break
csum.update(data)
n = yield afd.write(data, full=True)
afd.close()
print('client sha1 csum: %s' % csum.hexdigest())
def server_proc(conn, task=None):
# conn is a synchronous socket (as it is obtained from synchronous
# 'accept'); it's file-descriptor is converted to asynchronous
# file to read data from that
afd = pycos.asyncfile.AsyncFile(conn)
csum = hashlib.sha1()
nlines = 0
while True:
# read lines from data
line = yield afd.readline()
if not line:
break
csum.update(line)
nlines += 1
afd.close()
print('server sha1 csum: %s' % (csum.hexdigest()))
print('lines: %s' % (nlines))
def client_proc(host, port, input, task=None):
# client reads input file and sends data in chunks
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = pycos.AsyncSocket(sock)
yield sock.connect((host, port))
# data can be written to this asynchronous socket; however, for
# illustration, convert its file descriptor to asynchronous file
# and write to that instead
afd = pycos.asyncfile.AsyncFile(sock)
input = open(input)
csum = hashlib.sha1()
while True:
data = os.read(input.fileno(), 16*1024)
if not data:
break
csum.update(data)
n = yield afd.write(data, full=True)
afd.close()
print('client sha1 csum: %s' % csum.hexdigest())
def server_proc(conn, task=None):
# conn is a synchronous socket (as it is obtained from synchronous
# 'accept'); it's file-descriptor is converted to asynchronous
# file to read data from that
afd = pycos.asyncfile.AsyncFile(conn)
csum = hashlib.sha1()
nlines = 0
while True:
# read lines from data
line = yield afd.readline()
if not line:
break
csum.update(line)
nlines += 1
afd.close()
print('server sha1 csum: %s' % (csum.hexdigest()))
print('lines: %s' % (nlines))
def client_proc(host, port, input, task=None):
# client reads input file and sends data in chunks
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock = pycos.AsyncSocket(sock)
yield sock.connect((host, port))
# data can be written to this asynchronous socket; however, for
# illustration, convert its file descriptor to asynchronous file
# and write to that instead
afd = pycos.asyncfile.AsyncFile(sock)
input = open(input)
csum = hashlib.sha1()
while True:
data = os.read(input.fileno(), 16*1024)
if not data:
break
csum.update(data)
n = yield afd.write(data, full=True)
afd.close()
print('client sha1 csum: %s' % csum.hexdigest())
def server_proc(conn, task=None):
# conn is a synchronous socket (as it is obtained from synchronous
# 'accept'); it's file-descriptor is converted to asynchronous
# file to read data from that
afd = pycos.asyncfile.AsyncFile(conn)
csum = hashlib.sha1()
nlines = 0
while True:
# read lines from data
line = yield afd.readline()
if not line:
break
csum.update(line)
nlines += 1
afd.close()
print('server sha1 csum: %s' % (csum.hexdigest()))
print('lines: %s' % (nlines))
def userTwo(hash, tipo, user):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+hashlib.md5(pwd).hexdigest()+user).hexdigest()
else:
d = hashlib.sha1(options.salt+hashlib.sha1(pwd).hexdigest()+user).hexdigest()
if(d == hash):
print word+"(salt+"+ word +"(pass)+user)\t[+] Senha encontrada: "+pwd
return
print word+"(salt+"+ word +"(pass)+user)\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def userOne(hash, tipo, user):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+pwd+user).hexdigest()
else:
d = hashlib.sha1(options.salt+pwd+user).hexdigest()
if(d == hash):
print word+"(salt+pass+user)\t\t[+] Senha encontrada: "+pwd
return
print word+"(salt+pass+user)\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
#With Salt
def saltNine(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+hashlib.md5(hashlib.md5(pwd).hexdigest()+options.salt).hexdigest()).hexdigest()
else:
d = hashlib.sha1(options.salt+hashlib.sha1(hashlib.sha1(pwd).hexdigest()+options.salt).hexdigest()).hexdigest()
if(d == hash):
print word+"(salt+"+ word +"("+ word +"(pass)+salt))\t[+] Senha encontrada: "+pwd
return
print word+"(salt+"+ word +"("+ word +"(pass)+salt))\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltEight(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+hashlib.md5(options.salt+pwd).hexdigest()).hexdigest()
else:
d = hashlib.sha1(options.salt+hashlib.sha1(options.salt+pwd).hexdigest()).hexdigest()
if(d == hash):
print word+"(salt+"+ word +"(salt+pass))\t[+] Senha encontrada: "+pwd
return
print word+"(salt+"+ word +"(salt+pass))\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltSeven(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+hashlib.md5(pwd+options.salt).hexdigest()).hexdigest()
else:
d = hashlib.sha1(options.salt+hashlib.sha1(pwd+options.salt).hexdigest()).hexdigest()
if(d == hash):
print word+"(salt+"+ word +"(pass+salt))\t[+] Senha encontrada: "+pwd
return
print word+"(salt+"+ word +"(pass+salt))\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltFive(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+hashlib.md5(pwd).hexdigest()).hexdigest()
else:
d = hashlib.sha1(options.salt+hashlib.sha1(pwd).hexdigest()).hexdigest()
if(d == hash):
print word+"(salt+"+ word +"(pass))\t\t[+] Senha encontrada: "+pwd
return
print word+"(salt+"+ word +"(pass))\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltFour(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+pwd+options.salt).hexdigest()
else:
d = hashlib.sha1(options.salt+pwd+options.salt).hexdigest()
if(d == hash):
print word+"(salt+pass+salt)\t\t[+] Senha encontrada: "+pwd
return
print word+"(salt+pass+salt)\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltThree(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(pwd+options.salt+pwd).hexdigest()
else:
d = hashlib.sha1(pwd+options.salt+pwd).hexdigest()
if(d == hash):
print word+"(pass+salt+pass)\t\t[+] Senha encontrada: "+pwd
return
print word+"(pass+salt+pass)\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltTwo(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(pwd+options.salt).hexdigest()
else:
d = hashlib.sha1(pwd+options.salt).hexdigest()
if(d == hash):
print word+"(pass+salt)\t\t\t[+] Senha encontrada: "+pwd
return
print word+"(pass+salt)\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def saltOne(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(options.salt+pwd).hexdigest()
else:
d = hashlib.sha1(options.salt+pwd).hexdigest()
if(d == hash):
print word+"(salt+pass)\t\t\t[+] Senha encontrada: "+pwd
return
print word+"(salt+pass)\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
#Without Salt
def sextuple(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()
else:
d = hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()
if(d == hash):
print "Sextuple "+ word +"\t\t\t[+] Senha encontrada: "+pwd
return
print "Sextuple "+ word +"\t\t\t[+] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def quintuple(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()
else:
d = hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()).hexdigest()
if(d == hash):
print "Quintuple "+ word +"\t\t\t[+] Senha encontrada: "+pwd
return
print "Quintuple "+ word +"\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def quadruple(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(hashlib.md5(hashlib.md5(hashlib.md5(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()
else:
d = hashlib.sha1(hashlib.sha1(hashlib.sha1(hashlib.sha1(pwd).hexdigest()).hexdigest()).hexdigest()).hexdigest()
if(d == hash):
print "Quadruple "+ word +"\t\t\t[+] Senha encontrada: "+pwd
return
print "Quadruple "+ word +"\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def triple(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(hashlib.md5(hashlib.md5(pwd).hexdigest()).hexdigest()).hexdigest()
else:
d = hashlib.sha1(hashlib.sha1(hashlib.sha1(pwd).hexdigest()).hexdigest()).hexdigest()
if(d == hash):
print "Triple "+ word +"\t\t\t[+] Senha encontrada: "+pwd
return
print "Triple "+ word +"\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def double(hash, tipo):
global word
try:
f = open(options.wl)
for pwd in f.readlines():
pwd = pwd.strip()
if(tipo == 0):
d = hashlib.md5(hashlib.md5(pwd).hexdigest()).hexdigest()
else:
d = hashlib.sha1(hashlib.sha1(pwd).hexdigest()).hexdigest()
if(d == hash):
print "Double "+ word +"\t\t\t[+] Senha encontrada: "+pwd
return
print "Double "+ word +"\t\t\t[-] Senha nao encontrada! :-("
except IOError:
print "Nao foi possivel abrir sua wordlist, tente novamente."
except Exception as e:
print "Erro: "+str(e)
def decrypt(hash, tipo):
global word
try:
if(tipo == 0):
url = BeautifulSoup(urllib.urlopen("https://md5.gromweb.com/?md5=" + hash), "html.parser")
else:
url = BeautifulSoup(urllib.urlopen("https://sha1.gromweb.com/?hash=" + hash), "html.parser")
password = url.find("em", {"class": "long-content string"})
password = re.sub(re.compile("<.*?>"), "", str(password)).strip()
if str(password) == "None":
print word+"\t\t\t\t[-] Senha nao encontrada! :-("
else:
print word+"\t\t\t\t[+] Senha encontrada: " + password
except IOError:
decryptwl(hash, tipo)
def main():
argument_spec = dict(
compress=dict(default=True, type='bool'),
dest=dict(type='str'),
mode=dict(default='0644', type='str'),
sha1=dict(default=None, type='str'),
src=dict(required=True, type='str')
)
module = AnsibleModule(argument_spec)
dest = module.params.get('dest')
try:
if dest:
copy_to_host(module)
else:
copy_from_host(module)
except Exception:
module.exit_json(failed=True, changed=True,
msg=repr(traceback.format_exc()))
# import module snippets
def encrypt(self, data, offset=None, length=None):
"""Encrypts the given data with the current key"""
if offset is None:
offset = 0
if length is None:
length = len(data)
with BinaryWriter() as writer:
# Write SHA
writer.write(sha1(data[offset:offset + length]).digest())
# Write data
writer.write(data[offset:offset + length])
# Add padding if required
if length < 235:
writer.write(os.urandom(235 - length))
result = int.from_bytes(writer.get_bytes(), byteorder='big')
result = pow(result, self.e, self.m)
# If the result byte count is less than 256, since the byte order is big,
# the non-used bytes on the left will be 0 and act as padding,
# without need of any additional checks
return int.to_bytes(
result, length=256, byteorder='big', signed=False)
def find_by_cookie(cls, cookie_str):
if not cookie_str:
return None
try:
L = cookie_str.split('-')
if len(L) != 3:
return None
uid, expires, sha1 = L
if int(expires) < time.time():
return None
user = await cls.find(uid)
if not user:
return None
s = '%s-%s-%s-%s' % (uid, user.get('password'), expires, COOKIE_KEY)
if sha1 != hashlib.sha1(s.encode('utf-8')).hexdigest():
logging.info('invalid sha1')
return None
user.password = '******'
return user
except Exception as e:
logging.exception(e)
return None
# ?????
def setUpClass(cls):
cls.__repodir = tempfile.TemporaryDirectory()
fn = os.path.join(cls.__repodir.name, "test.txt")
cls.url = "file://" + fn
with open(fn, "w") as f:
f.write("Hello world!")
with open(fn, "rb") as f:
d = hashlib.sha1()
d.update(f.read())
cls.urlSha1 = asHexStr(d.digest())
with open(fn, "rb") as f:
d = hashlib.sha256()
d.update(f.read())
cls.urlSha256 = asHexStr(d.digest())