From 7bcb3a755ab97eeb5949f91dd574e810a39753c5 Mon Sep 17 00:00:00 2001 From: zontreck Date: Thu, 29 Aug 2024 07:19:16 -0700 Subject: [PATCH] Rework the hashing Helpers --- lib/utils/Hashing.dart | 42 ++++++++++++++++++++++++++---------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/lib/utils/Hashing.dart b/lib/utils/Hashing.dart index 89f44e5..7c7d36a 100644 --- a/lib/utils/Hashing.dart +++ b/lib/utils/Hashing.dart @@ -3,33 +3,43 @@ import 'dart:convert'; import 'package:crypto/crypto.dart'; class Hashing { + /// This will generate the md5 bytes and hash it using #bytes2Hash static String md5Hash(String input) { - var hash = md5.convert(utf8.encode(input)).bytes; - String x = ""; - for (int byte in hash) { - x += byte.toRadixString(16).padLeft(2, '0'); - } - - return x; + return bytes2Hash(md5Sum(input)); } + /// This will generate the Sha1 bytes and hash it using #bytes2Hash static String sha1Hash(String input) { - var hash = sha1.convert(utf8.encode(input)).bytes; + return bytes2Hash(sha1Sum(input)); + } + + /// This will generate the Sha256 bytes and hash it using #bytes2Hash. + static String sha256Hash(String input) { + return bytes2Hash(sha256Sum(input)); + } + + /// This function takes a list of bytes and returns a hash string + static String bytes2Hash(List bytes) { String x = ""; - for (int byte in hash) { + for (int byte in bytes) { x += byte.toRadixString(16).padLeft(2, '0'); } return x; } - static String sha256Hash(String input) { - var hash = sha256.convert(utf8.encode(input)).bytes; - String x = ""; - for (int byte in hash) { - x += byte.toRadixString(16).padLeft(2, '0'); - } + /// This function returns the bytes instead of a hash string + static List md5Sum(String input) { + return md5.convert(utf8.encode(input)).bytes; + } - return x; + /// This functions returns the bytes instead of a hash string + static List sha1Sum(String input) { + return sha1.convert(utf8.encode(input)).bytes; + } + + /// This function returns the bytes instead of a hash string + static List sha256Sum(String input) { + return sha256.convert(utf8.encode(input)).bytes; } }