Coverage for silkaj/checksum.py: 100%

22 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-04 17:03 +0000

1# Copyright 2016-2024 Maël Azimi <m.a@moul.re> 

2# 

3# Silkaj is free software: you can redistribute it and/or modify 

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

5# the Free Software Foundation, either version 3 of the License, or 

6# (at your option) any later version. 

7# 

8# Silkaj is distributed in the hope that it will be useful, 

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

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

11# GNU Affero General Public License for more details. 

12# 

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

14# along with Silkaj. If not, see <https://www.gnu.org/licenses/>. 

15 

16import re 

17import sys 

18 

19import rich_click as click 

20 

21from silkaj.auth import auth_method, has_auth_method 

22from silkaj.public_key import ( 

23 PUBKEY_CHECKSUM_PATTERN, 

24 PUBKEY_DELIMITED_PATTERN, 

25 gen_checksum, 

26 gen_pubkey_checksum, 

27) 

28 

29MESSAGE = "You should specify a pubkey or an authentication method" 

30 

31 

32@click.command( 

33 "checksum", 

34 help="Generate checksum out of a passed pubkey or an authentication method.\ 

35 Can also check if the checksum is valid", 

36) 

37@click.argument("pubkey_checksum", nargs=-1) 

38def checksum_command(pubkey_checksum: str) -> None: 

39 if has_auth_method(): 

40 key = auth_method() 

41 click.echo(gen_pubkey_checksum(key.pubkey)) 

42 elif not pubkey_checksum: 

43 sys.exit(MESSAGE) 

44 elif re.search(re.compile(PUBKEY_DELIMITED_PATTERN), pubkey_checksum[0]): 

45 click.echo(gen_pubkey_checksum(pubkey_checksum[0])) 

46 elif re.search(re.compile(PUBKEY_CHECKSUM_PATTERN), pubkey_checksum[0]): 

47 pubkey, checksum = pubkey_checksum[0].split(":") 

48 if checksum == gen_checksum(pubkey): 

49 click.echo("The checksum is valid") 

50 else: 

51 click.echo("The checksum is invalid") 

52 else: 

53 sys.exit("Error: Wrong public key format")