Fixed dumb human's attempt at hashing

This commit is contained in:
J.A.R.V.I.S 2021-06-27 18:27:11 -06:00
parent f861c758d8
commit 36124269dd

View file

@ -17,11 +17,14 @@ def hash_obj(hash, data, text: bool = True) -> str:
Data can be text or binary
"""
if text:
hash.update(data)
hash.update(data.encode("UTF-8"))
return hash.hexdigest()
BSIZE = 65536
while block := data.read(BSIZE):
hash.update(block)
block_idx = 0
while block_idx * BSIZE < len(data):
block = data[BSIZE * block_idx:BSIZE * (block_idx + 1)]
hash.update(block)
block_idx += 1
return hash.hexdigest()
@ -30,14 +33,14 @@ class DevCog(commands.Cog):
self.bot = bot
@commands.command(name="hash")
async def _hash(self, ctx, method: str, *, data: str):
async def _hash(self, ctx, method: str, *, data: str = None):
if method not in supported_hashes:
algo_txt = ", ".join(f"`{x}`" for x in supported_hashes)
await ctx.send(
"Unsupported hash algorithm. Supported:\n" + algo_txt
)
return
if len(data) == 0 and len(ctx.message.attachments) == 0:
if not data and len(ctx.message.attachments) == 0:
await ctx.send(
"No data to hash. Either attach a file or send text to hash"
)
@ -46,16 +49,16 @@ class DevCog(commands.Cog):
if len(ctx.message.attachments) > 0:
text = False
data = await ctx.message.attachments[0].read()
# Default to sha256, just in case
hash = getattr(hashlib, method, hashlib.sha256)()
hash = hash_obj(hash, data, text)
hex = hash_obj(hash, data, text)
data_size = convert_bytesize(len(data))
title = data if text else ctx.message.attachments[0].filename
description = "Hashed using " + method
fields = [
Field("Data Size", data_size, False),
Field("Hash", hash, False),
Field("Hash", hex, False),
]
embed = build_embed(