<commit_before>require 'openssl'

class Signer

	def initialize(client_id, client_secret, options = {})
		@client_id = client_id
		@client_secret = client_secret
		options = {self_key: 'WePay', hash_algo: 'sha512'}.merge(options);
		@self_key = options[:self_key] || 'WePay'
		@hash_algo = options[:client_id] || 'sha512'
	end

	def createScope
		return sprintf "%s/%s/signer", @self_key, @client_id
	end

	def createContext(payload)
		canonical_payload = ""

		payload.keys.sort.each do |key| 
			val = payload[key].downcase
			key = key.downcase
			canonical_payload = canonical_payload + sprintf("%s=%s\n", key, val)
		end
		signed_headers_string = payload.keys.sort.join(";")
		return canonical_payload + "\n" + signed_headers_string
	end

	def getSigningSalt
		self_key_sign = OpenSSL::HMAC.digest(@hash_algo, @client_secret, @self_key)
		client_id_sign = OpenSSL::HMAC.digest(@hash_algo, self_key_sign, @client_id)
		salt = OpenSSL::HMAC.digest(@hash_algo, client_id_sign, 'signer')
		return salt
	end

	def sign(payload)
		payload = payload.merge({'client_id'=>@client_id, 'client_secret'=>@client_secret})
		scope = createScope
		context = createContext(payload)
		s2s = createStringToSign(scope, context)
		signing_key = getSigningSalt
		signature = OpenSSL::HMAC.hexdigest(@hash_algo, signing_key, s2s)

		return signature
	end

	def createStringToSign(scope, context)
		scopeHash = OpenSSL::Digest.new(@hash_algo, scope)
		contextHash = OpenSSL::Digest.new(@hash_algo, context)
		return sprintf "SIGNER-HMAC-%s\n%s\n%s\n%s\n%s", @hash_algo.upcase, @self_key, @client_id, scopeHash, contextHash
	end

	def generateQueryStringParams(payload)
		signedToken = sign(payload)
		payload['client_id'] = @client_id
		payload['stoken'] = signedToken
		qsa = Array.new
		payload.keys.sort.each do |key|
			qsa.push sprintf("%s=%s", key, payload[key])
		end
		return qsa.join("&")
	end
end<commit_msg>Added namespace<commit_after>require 'openssl'

module Biplap
	class Signer

		def initialize(client_id, client_secret, options = {})
			@client_id = client_id
			@client_secret = client_secret
			options = {self_key: 'WePay', hash_algo: 'sha512'}.merge(options);
			@self_key = options[:self_key] || 'WePay'
			@hash_algo = options[:client_id] || 'sha512'
		end

		def createScope
			return sprintf "%s/%s/signer", @self_key, @client_id
		end

		def createContext(payload)
			canonical_payload = ""

			payload.keys.sort.each do |key| 
				val = payload[key].downcase
				key = key.downcase
				canonical_payload = canonical_payload + sprintf("%s=%s\n", key, val)
			end
			signed_headers_string = payload.keys.sort.join(";")
			return canonical_payload + "\n" + signed_headers_string
		end

		def getSigningSalt
			self_key_sign = OpenSSL::HMAC.digest(@hash_algo, @client_secret, @self_key)
			client_id_sign = OpenSSL::HMAC.digest(@hash_algo, self_key_sign, @client_id)
			salt = OpenSSL::HMAC.digest(@hash_algo, client_id_sign, 'signer')
			return salt
		end

		def sign(payload)
			payload = payload.merge({'client_id'=>@client_id, 'client_secret'=>@client_secret})
			scope = createScope
			context = createContext(payload)
			s2s = createStringToSign(scope, context)
			signing_key = getSigningSalt
			signature = OpenSSL::HMAC.hexdigest(@hash_algo, signing_key, s2s)

			return signature
		end

		def createStringToSign(scope, context)
			scopeHash = OpenSSL::Digest.new(@hash_algo, scope)
			contextHash = OpenSSL::Digest.new(@hash_algo, context)
			return sprintf "SIGNER-HMAC-%s\n%s\n%s\n%s\n%s", @hash_algo.upcase, @self_key, @client_id, scopeHash, contextHash
		end

		def generateQueryStringParams(payload)
			signedToken = sign(payload)
			payload['client_id'] = @client_id
			payload['stoken'] = signedToken
			qsa = Array.new
			payload.keys.sort.each do |key|
				qsa.push sprintf("%s=%s", key, payload[key])
			end
			return qsa.join("&")
		end
	end
{'fraction_non_alphanumeric': 0.1297828335056877, 'fraction_numerical': 0.004136504653567736, 'mean_word_length': 4.915902140672783, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4767117', 'n_tokens_mistral': 1541, 'n_tokens_neox': 1376, 'n_words': 268}