Private
Public Access
1
0
Files
rowsandall/rowers/opaque.py
2019-02-25 16:47:50 +01:00

65 lines
2.4 KiB
Python

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import struct
import base64
from rowsandall_app.settings import (
C2_CLIENT_ID, C2_REDIRECT_URI, C2_CLIENT_SECRET,
STRAVA_CLIENT_ID, STRAVA_REDIRECT_URI, STRAVA_CLIENT_SECRET,
POLAR_CLIENT_ID, POLAR_REDIRECT_URI, POLAR_CLIENT_SECRET,
SPORTTRACKS_CLIENT_ID, SPORTTRACKS_REDIRECT_URI,
SPORTTRACKS_CLIENT_SECRET,
UNDERARMOUR_CLIENT_ID, UNDERARMOUR_REDIRECT_URI,
UNDERARMOUR_CLIENT_SECRET,UNDERARMOUR_CLIENT_KEY,
RUNKEEPER_CLIENT_ID,RUNKEEPER_REDIRECT_URI,RUNKEEPER_CLIENT_SECRET,
TP_CLIENT_ID,TP_REDIRECT_URI,TP_CLIENT_KEY,TP_CLIENT_SECRET,
BRAINTREE_MERCHANT_ID,BRAINTREE_PUBLIC_KEY,BRAINTREE_PRIVATE_KEY,
PAYMENT_PROCESSING_ON,OPAQUE_SECRET_KEY
)
class OpaqueEncoder:
"""
Opaque ID encoder.
Translates between 32-bit integers (such as resource IDs) and obfuscated
scrambled values, as a one-to-one mapping. Supports hex and base64 url-safe
string representations. Expects a secret integer key in the constructor.
(c) 2011 Marek Z. @marekweb
"""
def __init__(self, key):
self.key = key
self.extra_chars = b'.-';
def transform(self, i):
"""Produce an integer hash of a 16-bit integer, returning a transformed 16-bit integer."""
i = (self.key ^ i) * 0x9e3b
return i >> (i & 0xf) & 0xffff
def transcode(self, i):
"""Reversibly transcode a 32-bit integer to a scrambled form, returning a new 32-bit integer."""
r = i & 0xffff
l = i >> 16 & 0xffff ^ self.transform(r)
return ((r ^ self.transform(l)) << 16) + l
def encode_hex(self, i):
"""Transcode an integer and return it as an 8-character hex string."""
return "%08x" % self.transcode(i)
def encode_base64(self, i):
"""Transcode an integer and return it as a 6-character base64 string."""
return base64.b64encode(struct.pack('!L', self.transcode(i)), self.extra_chars)[:6]
def decode_hex(self, s):
"""Decode an 8-character hex string, returning the original integer."""
return self.transcode(int(s, 16))
def decode_base64(self, s):
"""Decode a 6-character base64 string, returning the original integer."""
return self.transcode(struct.unpack('!L', base64.b64decode(s + '==', self.extra_chars))[0])
encoder = OpaqueEncoder(OPAQUE_SECRET_KEY)