From 36124269dd741b11b9dd624481b08103de4aa154 Mon Sep 17 00:00:00 2001 From: "J.A.R.V.I.S" Date: Sun, 27 Jun 2021 18:27:11 -0600 Subject: [PATCH] Fixed dumb human's attempt at hashing --- jarvis/cogs/dev.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/jarvis/cogs/dev.py b/jarvis/cogs/dev.py index c8a219a..bff434b 100644 --- a/jarvis/cogs/dev.py +++ b/jarvis/cogs/dev.py @@ -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(