Coverage for /pythoncovmergedfiles/medio/medio/src/python-crc32c/src/google_crc32c/_checksum.py: 36%

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

25 statements  

1# Copyright 2020 Google LLC 

2# 

3# Licensed under the Apache License, Version 2.0 (the "License"); 

4# you may not use this file except in compliance with the License. 

5# You may obtain a copy of the License at 

6# 

7# https://www.apache.org/licenses/LICENSE-2.0 

8# 

9# Unless required by applicable law or agreed to in writing, software 

10# distributed under the License is distributed on an "AS IS" BASIS, 

11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 

12# See the License for the specific language governing permissions and 

13# limitations under the License. 

14 

15import struct 

16 

17 

18class CommonChecksum(object): 

19 """Hashlib-alike helper for CRC32C operations. 

20 

21 This class should not be used directly and requires an update implementation. 

22 

23 Args: 

24 initial_value (Optional[bytes]): the initial chunk of data from 

25 which the CRC32C checksum is computed. Defaults to b''. 

26 """ 

27 __slots__ = () 

28 

29 def __init__(self, initial_value=b""): 

30 self._crc = 0 

31 if initial_value != b"": 

32 self.update(initial_value) 

33 

34 def update(self, data): 

35 """Update the checksum with a new chunk of data. 

36 

37 Args: 

38 chunk (Optional[bytes]): a chunk of data used to extend 

39 the CRC32C checksum. 

40 """ 

41 raise NotImplemented() 

42 

43 def digest(self): 

44 """Big-endian order, per RFC 4960. 

45 

46 See: https://cloud.google.com/storage/docs/json_api/v1/objects#crc32c 

47 

48 Returns: 

49 bytes: An eight-byte digest string. 

50 """ 

51 return struct.pack(">L", self._crc) 

52 

53 def hexdigest(self): 

54 """Like :meth:`digest` except returns as a bytestring of double length. 

55 

56 Returns 

57 bytes: A sixteen byte digest string, contaiing only hex digits. 

58 """ 

59 return "{:08x}".format(self._crc).encode("ascii") 

60 

61 def copy(self): 

62 """Create another checksum with the same CRC32C value. 

63 

64 Returns: 

65 Checksum: the new instance. 

66 """ 

67 clone = self.__class__() 

68 clone._crc = self._crc 

69 return clone 

70 

71 def consume(self, stream, chunksize): 

72 """Consume chunks from a stream, extending our CRC32 checksum. 

73 

74 Args: 

75 stream (BinaryIO): the stream to consume. 

76 chunksize (int): the size of the read to perform 

77 

78 Returns: 

79 Generator[bytes, None, None]: Iterable of the chunks read from the 

80 stream. 

81 """ 

82 while True: 

83 chunk = stream.read(chunksize) 

84 if not chunk: 

85 break 

86 self.update(chunk) 

87 yield chunk