Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supplier Management System API Authentication (Python)
# Sample values
url = "/api/suppliers"
params = {"trading_name": "Joe's Widgets", "registration_number": 123}
api_key = '123' # Your private API key
required_folds = 3 # The number of times to re-encrypt the data
# The calculation itself
import hmac
import hashlib
import base64
param_pairs = params.items()
param_pairs.sort()
flattened_parameters = ["%s=%s" % (i[0], i[1]) for i in param_pairs]
# ["registration_number=123", "trading_name=Joe's Widgets"]
flattened_parameters = ''.join(flattened_parameters)
# "registration_number=123trading_name=Joe's Widgets"
data = "%s%s" % (url, flattened_parameters)
# "/api/suppliersregistration_number=123trading_name=Joe's Widgets"
for i in xrange(required_folders):
data = hmac.new(api_key, msg=data, digestmod=hashlib.sha256).hexdigest()
# 1. e3c9a484dc5f5b56bc1d185e5b8fc2f6693567478d51b64c510dd4218b97e1ba
# 2. 8512aadbbac06b695dc542f944df1b04b122e666919553888ad7509636093f62
# 3. af37df231d49a3ecfdae8d6f37422b3d6f936677b7086a013884b83b947c43cc
base64.b64encode(data)
# YWYzN2RmMjMxZDQ5YTNlY2ZkYWU4ZDZmMzc0MjJiM2Q2ZjkzNjY3N2I3MDg2YTAxMzg4NGI4M2I5NDdjNDNjYw==
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.