From 91b63a22d36df217814e928b2a9a0bbb732b7ebe Mon Sep 17 00:00:00 2001 From: derped Date: Mon, 25 Nov 2024 11:05:20 +0100 Subject: [PATCH] add sha hash family --- fck/cktype/__init__.py | 12 ++++++++++-- fck/cktype/sha1.py | 26 ++++++++++++++++++++++++++ fck/cktype/sha224.py | 26 ++++++++++++++++++++++++++ fck/cktype/sha256.py | 26 ++++++++++++++++++++++++++ fck/cktype/sha384.py | 26 ++++++++++++++++++++++++++ fck/cktype/sha512.py | 26 ++++++++++++++++++++++++++ 6 files changed, 140 insertions(+), 2 deletions(-) create mode 100644 fck/cktype/sha1.py create mode 100644 fck/cktype/sha224.py create mode 100644 fck/cktype/sha256.py create mode 100644 fck/cktype/sha384.py create mode 100644 fck/cktype/sha512.py diff --git a/fck/cktype/__init__.py b/fck/cktype/__init__.py index 0dd0dec..d7451c6 100644 --- a/fck/cktype/__init__.py +++ b/fck/cktype/__init__.py @@ -4,10 +4,18 @@ from fck.cktype.cktypeinterface import CKTYPEINTERFACE as CKTYPEINTERFACE from .crc32 import CRC32 from .md5 import MD5 +from .sha1 import SHA1 +from .sha224 import SHA224 +from .sha256 import SHA256 +from .sha512 import SHA512 -CKTYPES: Dict[str, Type[MD5 | CRC32]] = { - "MD5": (MD5), +CKTYPES: Dict[str, Type[CRC32 | MD5 | SHA1 | SHA224 | SHA256 | SHA512]] = { "CRC32": (CRC32), + "MD5": (MD5), + "SHA1": (SHA1), + "SHA224": (SHA224), + "SHA256": (SHA256), + "SHA512": (SHA512), } diff --git a/fck/cktype/sha1.py b/fck/cktype/sha1.py new file mode 100644 index 0000000..fa8c3d4 --- /dev/null +++ b/fck/cktype/sha1.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import sha1 + + +class SHA1(object): + NAME: str = "SHA1" + EXT: List[str] = [".sha1"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{40}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{40}") + NULL: str = "DA39A3EE5E6B4B0D3255BFEF95601890AFD80709" + + def __init__(self): + self.cksum = sha1(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper() diff --git a/fck/cktype/sha224.py b/fck/cktype/sha224.py new file mode 100644 index 0000000..981526b --- /dev/null +++ b/fck/cktype/sha224.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import sha224 + + +class SHA224(object): + NAME: str = "SHA224" + EXT: List[str] = [".sha224"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{56}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{56}") + NULL: str = "D14A028C2A3A2BC9476102BB288234C415A2B01F828EA62AC5B3E42F" + + def __init__(self): + self.cksum = sha224(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper() diff --git a/fck/cktype/sha256.py b/fck/cktype/sha256.py new file mode 100644 index 0000000..75ebf3c --- /dev/null +++ b/fck/cktype/sha256.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import sha256 + + +class SHA256(object): + NAME: str = "SHA256" + EXT: List[str] = [".sha256"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{64}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{64}") + NULL: str = "E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855" + + def __init__(self): + self.cksum = sha256(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper() diff --git a/fck/cktype/sha384.py b/fck/cktype/sha384.py new file mode 100644 index 0000000..7bbbb33 --- /dev/null +++ b/fck/cktype/sha384.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import sha384 + + +class SHA384(object): + NAME: str = "SHA384" + EXT: List[str] = [".sha384"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{96}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{96}") + NULL: str = "38B060A751AC96384CD9327EB1B1E36A21FDB71114BE07434C0CC7BF63F6E1DA274EDEBFE76F65FBD51AD2F14898B95B" + + def __init__(self): + self.cksum = sha384(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper() diff --git a/fck/cktype/sha512.py b/fck/cktype/sha512.py new file mode 100644 index 0000000..9f617fc --- /dev/null +++ b/fck/cktype/sha512.py @@ -0,0 +1,26 @@ +from typing import List +from re import compile, Pattern +from hashlib import sha512 + + +class SHA512(object): + NAME: str = "SHA512" + EXT: List[str] = [".sha512"] + SYNTAX: List[Pattern] = [compile(r"^;*$"), compile(r"^.* [0-9a-fA-F]{128}$")] + REGEX: Pattern = compile(r"[0-9a-fA-F]{128}") + NULL: str = "CF83E1357EEFB8BDF1542850D66D8007D620E4050B5715DC83F4A921D36CE9CE47D0D13C5D85F2B0FF8318D2877EEC2F63B931BD47417A81A538327AF927DA3E" + + def __init__(self): + self.cksum = sha512(usedforsecurity=False) + + def gensum(self, data: bytes): + self.cksum.update(data) + + def reset(self): + self.__init__() + + def format(self, fpath) -> str: + return " ".join([self.__repr__(), fpath]) + + def __repr__(self) -> str: + return self.cksum.hexdigest().upper()