Skip to content

Commit

Permalink
Merge documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
Carlos Requena López authored Dec 3, 2018
2 parents bdc2d5a + 47daccb commit f287b03
Show file tree
Hide file tree
Showing 12 changed files with 284 additions and 132 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# TORrents

## [Explore the TORrents docs »](https://theonionbay.github.io/TORrents/)

## Setup

Install pipenv and python3, then:
Expand Down Expand Up @@ -35,3 +37,10 @@ To start an instance of the tracker, run:

where `<path-to-list-of-files>` can be `client/a.json` or
`client/b.json` etc.

### Node

```
(venv shell)$ python node.py <ip>
```
Where `<ip>` is the public ip of the machine.
5 changes: 4 additions & 1 deletion client.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,12 @@ def __init__(self, name, filenames):
self.connected = False

def run(self):
"""Runs the client application.
"""
super().run(host='0.0.0.0', use_reloader=False)

def index(self):
"""Serves HTML page with input to request file.
"""
data = {
"owned_files": self.owned_files,
Expand Down Expand Up @@ -89,6 +90,8 @@ def main_handler(self):
return ("Unexpected payload type", 400)

def handle_request(self, message):
"""Send the requested file if it exists, else sends an error.
"""
filename = message["file"]

self.log += "Got file request with filename " + filename + "\n"
Expand Down
4 changes: 4 additions & 0 deletions common/encoding.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
encoding = "utf-8"

def json_to_bytes(obj):
"""Returns a byte object of a given json file.
"""
return bytes(json.dumps(obj), encoding)

def bytes_to_json(bytes_seq):
"""Returns a json object/dictionary of a given sequence of bytes.
"""
return json.loads(bytes_seq.decode(encoding))

def file_to_payload(file_path):
Expand Down
7 changes: 3 additions & 4 deletions common/network_info.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,20 @@
}

def get_url(ip):
"""Returns a valid url.
"""
return "http://" + ip + ":" + str(port)

# tracker = "192.168.1.60"

# node_pool = [
# node_pool = [
# "192.168.1.11",
# "192.168.1.12",
# "192.168.1.19",
# "192.168.1.3",
# "192.168.1.55"
# ]


# For now private keys are stored here, we should decide how to create them
# and make the public keys available to the client
public_keys = json.load(open("common/public_keys.json"))
for ip, key in public_keys.items():
public_keys[ip] = rsa.RSAPublicKey(key["n"], key["e"])
Expand Down
2 changes: 1 addition & 1 deletion crypto/aes_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,4 @@ def expand_key(key):
0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d]
0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d]
3 changes: 3 additions & 0 deletions crypto/aes_decrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
from collections import deque

def decrypt(cipher_text, key):
"""Decrypts a message using AES-128.
"""

assert type(cipher_text) == bytes, "cipher_text must be of type bytes"
assert len(cipher_text) % block_size == 0, "cipher_text must consist of a whole number of blocks"
assert type(key) == bytes, "key must be of type bytes"
Expand Down
2 changes: 2 additions & 0 deletions crypto/aes_encrypt.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from .random_bytes import generate_bytes

def encrypt(plain_text, key):
"""Encrypts a message usin AES-128.
"""
assert type(plain_text) == bytes, "plain_text must be of type bytes"
assert type(key) == bytes, "key must be of type bytes"
assert len(key) == block_size, "key must be 128 bits"
Expand Down
5 changes: 4 additions & 1 deletion crypto/random_bytes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import os

def generate_bytes(n):
return os.urandom(n) # cryptographically secure RNG
"""Returns random bytes to be used
as an Universally Unique Identifier.
"""
return os.urandom(n) # cryptographically secure RNG
4 changes: 0 additions & 4 deletions crypto/rsa.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ def generate_rsa():
decrypt messages (with the function rsa_decrypt).
"""
while True:
# TODO Understand RSA
p = random_prime(key_width // 2)
q = random_prime(key_width // 2)
lambda_n = lcm(p - 1, q - 1)
Expand Down Expand Up @@ -74,7 +73,6 @@ def rsa_encrypt(plain_text, key):
assert type(key) in (RSAPublicKey, RSAPrivateKey), "key must be a RSA key"

# Convert plain_text to number
# TODO use more secure padding
plain_text = int.from_bytes(plain_text, byteorder="big", signed=False)

assert plain_text < key.n, "plain_text must have a numerical value less than the modulus of the key"
Expand Down Expand Up @@ -120,7 +118,6 @@ def mod_inverse(a, b):
"""Calculates the inverse of a in a ring of modulus b. That is,
returns an integer x such that ax = 1 mod b.
"""
# TODO Understand this code
s = 0
old_s = 1
r = b
Expand All @@ -136,7 +133,6 @@ def mod_inverse(a, b):

def lcm(a, b):
"""Returns the least common multiple of a and b."""
# TODO check that
return abs(a * b) // gcd(a, b)


Expand Down
Loading

0 comments on commit f287b03

Please sign in to comment.