opaquification part 1
This commit is contained in:
59
rowers/opaque.py
Normal file
59
rowers/opaque.py
Normal file
@@ -0,0 +1,59 @@
|
||||
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)
|
||||
Reference in New Issue
Block a user