Last active
April 17, 2016 00:19
-
-
Save ewust/c4d96cb440a89a9ad94ac938170bc43c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
import struct | |
import socket | |
import time | |
import nacl.secret | |
import nacl.utils | |
from nacl.encoding import HexEncoder | |
import sys | |
KEY = '1de37598478f6f9a62c3d19fa8e1b99ea35b6f0e9de0ede83cce6226fe51cd86' | |
def gen_key(): | |
return nacl.utils.random(nacl.secret.SecretBox.KEY_SIZE) | |
def gen_token(ip_addr): | |
data = struct.pack('!L', int(time.time())) + socket.inet_aton(ip_addr) | |
box = nacl.secret.SecretBox(KEY, HexEncoder) | |
message = b"" | |
nonce = nacl.utils.random(nacl.secret.SecretBox.NONCE_SIZE) | |
encrypted = box.encrypt(data, nonce) | |
return encrypted.encode('base64').replace('\n', '') | |
def decrypt_token(token): | |
box = nacl.secret.SecretBox(KEY, HexEncoder) | |
plain = box.decrypt(token.decode('base64')) | |
time_d, = struct.unpack('!L', plain[0:4]) | |
return time_d, socket.inet_ntoa(plain[4:]) | |
#print gen_token('192.168.1.55') | |
print gen_key().encode('hex') | |
test = gen_token(sys.argv[1]) | |
print test | |
print decrypt_token(test) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment