File: freeotp.py

package info (click to toggle)
rust-cotp 1.9.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,392 kB
  • sloc: python: 168; xml: 14; sh: 9; makefile: 4
file content (51 lines) | stat: -rw-r--r-- 1,340 bytes parent folder | download
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
import base64
import sys
import xml.etree.ElementTree as Et
import json


def fetch_json(filename):
    element_list = []
    tree = Et.parse(filename)
    for e in tree.findall("string"):
        element_list.append(json.loads(e.text.replace(""", "\"")))
    return element_list


def object_to_cotp_json(d):
    final = []
    for element in d:
        if type(element) is dict:
            final.append(
                {
                    'label': element['label'],
                    'secret': byte_array_to_base32(element['secret']),
                    'issuer': element['issuerExt'],
                    'algorithm': element['algo'],
                    'type': element['type'],
                    'digits': int(element['digits']),
                    'counter': element['counter'],
                }
            )
    return final

def byte_array_to_base32(secret: list):
    b = bytearray()
    for n in secret:
        # convert signed byte to unsigned
        b.append(n & 255)

    return base64.b32encode(b).decode('utf-8')

def main():
    if len(sys.argv) != 3:
        print("Usage: python authy.py [INPUT_FILE] [OUTPUT_FILE]")
        return

    data = fetch_json(sys.argv[1])
    output_file = open(sys.argv[2], 'w')
    output_file.write(json.dumps(object_to_cotp_json(data)))
    output_file.close()


main()