Skip to content
Snippets Groups Projects
verify_tor_utils.py 2.03 KiB
Newer Older
#!/usr/bin/env python3
import os
import sys
from subprocess import check_call
Nico's avatar
Nico committed
from utils import get_sha256, get_build_versions, get_final_file_name, get_version_tag, get_version
REF_DIR = "reference"

Nico's avatar
Nico committed
def main(platform):
    # get Tor version from command or show usage information
    version = get_version()

Nico's avatar
Nico committed
    if verify(version, platform):
        sys.exit(0)
    else:
        sys.exit(1)


Nico's avatar
Nico committed
def verify(version, platform):
    # get Tor version and versions of its dependencies
    versions = get_build_versions(version)

    # download reference binary
    os.makedirs(REF_DIR, exist_ok=True)
Nico's avatar
Nico committed
    file_name = get_final_file_name(versions, platform)
    ref_file = os.path.join(REF_DIR, file_name)
    # try downloading from maven central
    check_call(['wget', '--no-verbose', get_url(versions, platform), '-O', ref_file])

    # check if Tor was already build
    if not os.path.isfile(file_name):
Nico's avatar
Nico committed
            check_call(["./build_tor_%s.py" % platform])
Nico's avatar
Nico committed
            check_call(["./build_tor_%s.py" % platform, version])

    # calculate hashes for both files
    reference_hash = get_sha256(ref_file)
    build_hash = get_sha256(file_name)
    print("Reference sha256: %s" % reference_hash)
    print("Build sha256:     %s" % build_hash)

    # compare hashes
Nico's avatar
Nico committed
    suffix = ""
    if platform == "android":
        suffix = " for Android"
    elif platform == "linux":
        suffix = " for Linux"
    elif platform == "windows":
        suffix = " for Windows"
    if reference_hash == build_hash:
        print("Tor%s version %s was successfully verified! \\o/" % (suffix, versions['tor']))
        print("Hashes for Tor%s version %s do not match! :(" % (suffix, versions['tor']))
        return False
def get_url(versions, platform):
    version = get_version_tag(versions)
Nico's avatar
Nico committed
    directory = "tor-%s" % platform
    file = get_final_file_name(versions, platform)
    return "https://repo.maven.apache.org/maven2/org/briarproject/%s/%s/%s" % (directory, version, file)