Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/scapy/layers/tls/crypto/hash.py: 93%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

41 statements  

1# SPDX-License-Identifier: GPL-2.0-only 

2# This file is part of Scapy 

3# See https://scapy.net/ for more information 

4# Copyright (C) 2007, 2008, 2009 Arnaud Ebalard 

5# 2015, 2016 Maxence Tury 

6 

7""" 

8Hash classes. 

9""" 

10 

11from hashlib import md5, sha1, sha224, sha256, sha384, sha512 

12from scapy.layers.tls.crypto.md4 import MD4 as md4 

13 

14 

15_tls_hash_algs = {} 

16 

17 

18class _GenericHashMetaclass(type): 

19 """ 

20 Hash classes are automatically registered through this metaclass. 

21 Furthermore, their name attribute is extracted from their class name. 

22 """ 

23 def __new__(cls, hash_name, bases, dct): 

24 if hash_name != "_GenericHash": 

25 dct["name"] = hash_name[5:] # remove leading "Hash_" 

26 the_class = super(_GenericHashMetaclass, cls).__new__(cls, hash_name, 

27 bases, dct) 

28 if hash_name != "_GenericHash": 

29 _tls_hash_algs[hash_name[5:]] = the_class 

30 return the_class 

31 

32 

33class _GenericHash(metaclass=_GenericHashMetaclass): 

34 def digest(self, tbd): 

35 return self.hash_cls(tbd).digest() 

36 

37 

38class Hash_NULL(_GenericHash): 

39 hash_len = 0 

40 

41 def digest(self, tbd): 

42 return b"" 

43 

44 

45class Hash_MD4(_GenericHash): 

46 hash_cls = md4 

47 hash_len = 16 

48 

49 

50class Hash_MD5(_GenericHash): 

51 hash_cls = md5 

52 hash_len = 16 

53 

54 

55class Hash_SHA(_GenericHash): 

56 hash_cls = sha1 

57 hash_len = 20 

58 

59 

60class Hash_SHA224(_GenericHash): 

61 hash_cls = sha224 

62 hash_len = 28 

63 

64 

65class Hash_SHA256(_GenericHash): 

66 hash_cls = sha256 

67 hash_len = 32 

68 

69 

70class Hash_SHA384(_GenericHash): 

71 hash_cls = sha384 

72 hash_len = 48 

73 

74 

75class Hash_SHA512(_GenericHash): 

76 hash_cls = sha512 

77 hash_len = 64