add sha hash family

This commit is contained in:
Kevin Baensch 2024-11-25 11:05:20 +01:00
parent 7210a9d0b1
commit 91b63a22d3
Signed by: derped
GPG key ID: C0F1D326C7626543
6 changed files with 140 additions and 2 deletions

View file

@ -4,10 +4,18 @@ from fck.cktype.cktypeinterface import CKTYPEINTERFACE as CKTYPEINTERFACE
from .crc32 import CRC32 from .crc32 import CRC32
from .md5 import MD5 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]] = { CKTYPES: Dict[str, Type[CRC32 | MD5 | SHA1 | SHA224 | SHA256 | SHA512]] = {
"MD5": (MD5),
"CRC32": (CRC32), "CRC32": (CRC32),
"MD5": (MD5),
"SHA1": (SHA1),
"SHA224": (SHA224),
"SHA256": (SHA256),
"SHA512": (SHA512),
} }

26
fck/cktype/sha1.py Normal file
View file

@ -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()

26
fck/cktype/sha224.py Normal file
View file

@ -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()

26
fck/cktype/sha256.py Normal file
View file

@ -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()

26
fck/cktype/sha384.py Normal file
View file

@ -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()

26
fck/cktype/sha512.py Normal file
View file

@ -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()