Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Supplier Management System API Authentication (Ruby)
# 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
flattened_parameters = params.sort.map { |pair| "#{ pair[0] }=#{ pair[1] }" }
# ["registration_number=123", "trading_name=Joe's Widgets"]
flattened_parameters = flattened_parameters.join
# "registration_number=123trading_name=Joe's Widgets"
data = "#{ url }#{ flattened_parameters }"
# "/api/suppliersregistration_number=123trading_name=Joe's Widgets"
required_folds.times do
data = OpenSSL::HMAC.hexdigest(OpenSSL::Digest::Digest.new('sha256'), api_key, data)
# 1. e3c9a484dc5f5b56bc1d185e5b8fc2f6693567478d51b64c510dd4218b97e1ba
# 2. 8512aadbbac06b695dc542f944df1b04b122e666919553888ad7509636093f62
# 3. af37df231d49a3ecfdae8d6f37422b3d6f936677b7086a013884b83b947c43cc
end
Base64.strict_encode64(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.