File: ble_rpa_tool.py

package info (click to toggle)
python-bumble 0.0.225-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 9,464 kB
  • sloc: python: 75,258; java: 3,782; javascript: 823; xml: 203; sh: 172; makefile: 8
file content (64 lines) | stat: -rw-r--r-- 1,702 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# Copyright 2023 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import click

from bumble.colors import color
from bumble.hci import Address
from bumble.helpers import generate_irk, verify_rpa_with_irk


@click.group()
def cli():
    '''
    This is a tool for generating IRK, RPA,
    and verifying IRK/RPA pairs
    '''


@click.command()
def gen_irk() -> None:
    print(generate_irk().hex())


@click.command()
@click.argument("irk", type=str)
def gen_rpa(irk: str) -> None:
    irk_bytes = bytes.fromhex(irk)
    rpa = Address.generate_private_address(irk_bytes)
    print(rpa.to_string(with_type_qualifier=False))


@click.command()
@click.argument("irk", type=str)
@click.argument("rpa", type=str)
def verify_rpa(irk: str, rpa: str) -> None:
    address = Address(rpa)
    irk_bytes = bytes.fromhex(irk)
    if verify_rpa_with_irk(address, irk_bytes):
        print(color("Verified", "green"))
    else:
        print(color("Not Verified", "red"))


def main():
    cli.add_command(gen_irk)
    cli.add_command(gen_rpa)
    cli.add_command(verify_rpa)
    cli()


# -----------------------------------------------------------------------------
if __name__ == '__main__':
    main()