Rework the hashing Helpers

This commit is contained in:
zontreck 2024-08-29 07:19:16 -07:00
parent 842a2b8f0c
commit 7bcb3a755a

View file

@ -3,33 +3,43 @@ import 'dart:convert';
import 'package:crypto/crypto.dart'; import 'package:crypto/crypto.dart';
class Hashing { class Hashing {
/// This will generate the md5 bytes and hash it using #bytes2Hash
static String md5Hash(String input) { static String md5Hash(String input) {
var hash = md5.convert(utf8.encode(input)).bytes; return bytes2Hash(md5Sum(input));
String x = "";
for (int byte in hash) {
x += byte.toRadixString(16).padLeft(2, '0');
}
return x;
} }
/// This will generate the Sha1 bytes and hash it using #bytes2Hash
static String sha1Hash(String input) { 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<int> bytes) {
String x = ""; String x = "";
for (int byte in hash) { for (int byte in bytes) {
x += byte.toRadixString(16).padLeft(2, '0'); x += byte.toRadixString(16).padLeft(2, '0');
} }
return x; return x;
} }
static String sha256Hash(String input) { /// This function returns the bytes instead of a hash string
var hash = sha256.convert(utf8.encode(input)).bytes; static List<int> md5Sum(String input) {
String x = ""; return md5.convert(utf8.encode(input)).bytes;
for (int byte in hash) { }
x += byte.toRadixString(16).padLeft(2, '0');
}
return x; /// This functions returns the bytes instead of a hash string
static List<int> sha1Sum(String input) {
return sha1.convert(utf8.encode(input)).bytes;
}
/// This function returns the bytes instead of a hash string
static List<int> sha256Sum(String input) {
return sha256.convert(utf8.encode(input)).bytes;
} }
} }