Coverage for silkaj/checksum.py: 100%
21 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 12:04 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-22 12:04 +0000
1# Copyright 2016-2025 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/>.
16import re
18import rich_click as click
20from silkaj import auth, tools
21from silkaj.public_key import (
22 PUBKEY_CHECKSUM_PATTERN,
23 PUBKEY_DELIMITED_PATTERN,
24 gen_checksum,
25 gen_pubkey_checksum,
26)
28MESSAGE = "You should specify a pubkey or an authentication method"
31@click.command(
32 "checksum",
33 help="Generate checksum out of a passed pubkey or an authentication method. \
34Checks if the passed checksum is valid.",
35)
36@click.argument("pubkey_checksum", nargs=-1)
37def checksum_command(pubkey_checksum: str) -> None:
38 if tools.has_account_defined(exit_error=False):
39 key = auth.auth_method()
40 click.echo(gen_pubkey_checksum(key.pubkey))
41 elif not pubkey_checksum:
42 tools.click_fail(MESSAGE)
43 elif re.search(re.compile(PUBKEY_DELIMITED_PATTERN), pubkey_checksum[0]):
44 click.echo(gen_pubkey_checksum(pubkey_checksum[0]))
45 elif re.search(re.compile(PUBKEY_CHECKSUM_PATTERN), pubkey_checksum[0]):
46 pubkey, checksum = pubkey_checksum[0].split(":")
47 if checksum == gen_checksum(pubkey):
48 click.echo("The checksum is valid")
49 else:
50 click.echo("The checksum is invalid")
51 else:
52 tools.click_fail("Wrong public key format")