1# -*- coding: utf-8 -*- 

2 

3""" 

4interfaces around python-apt 

5 

6@copyright: 2020 😸 <😸@43-1.org> 

7@license: GNU General Public License version 2 or later 

8""" 

9 

10# This program is free software; you can redistribute it and/or modify 

11# it under the terms of the GNU General Public License as published by 

12# the Free Software Foundation; either version 2 of the License, or 

13# (at your option) any later version. 

14# 

15# This program is distributed in the hope that it will be useful, 

16# but WITHOUT ANY WARRANTY; without even the implied warranty of 

17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

18# GNU General Public License for more details. 

19# 

20# You should have received a copy of the GNU General Public License 

21# along with this program. If not, see <https://www.gnu.org/licenses/>. 

22 

23import apt_pkg 

24 

25 

26class DakHashes: 

27 """ 

28 wrapper around `apt_pkg.Hashes` 

29 """ 

30 

31 def __init__(self, *args, **kwargs): 

32 self._apt_hashes = apt_pkg.Hashes(*args, **kwargs) 

33 

34 # python-apt in Debian 10 (buster) uses 

35 # `apt_pkg.Hashes(...).md5` 

36 # but in Debian bullseye it switched to 

37 # `apt_pkg.Hashes(...).find('md5sum').hashvalue` 

38 def _hashvalue(self, name): 

39 h = self._apt_hashes.hashes.find(name) 

40 try: 

41 return h.hashvalue 

42 except AttributeError: 

43 return str(h)[len(name) + 1 :] 

44 

45 @property 

46 def md5(self) -> str: 

47 return self._hashvalue("md5sum") 

48 

49 @property 

50 def sha1(self) -> str: 

51 return self._hashvalue("sha1") 

52 

53 @property 

54 def sha256(self) -> str: 

55 return self._hashvalue("sha256")