Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env ruby
- require "securerandom"
- require "benchmark"
- require "openssl"
- class Svc2Svc
- def initialize(count: 1000)
- @keys = Array.new(1000) { SecureRandom.hex }
- @services = keys.each_with_object({paysvc_live: [], paysvc_sandbox: [], banking: []}) do |key, services|
- services[services.keys.shuffle.first] << hmac_encode(key)
- end
- end
- attr_reader :keys, :services
- def authorized?(token)
- @services.each do |service, keys|
- return service if keys.find { |key| key == hmac_encode(token) }
- end
- nil
- end
- private
- def hmac_encode(string)
- OpenSSL::HMAC.new(string, OpenSSL::Digest.new("sha256")).to_s
- end
- end
- gatekeeper = Svc2Svc.new
- # Takes about 1.4s, so 1.4ms to authorize a token when we have 1000 service-to-service
- # keys to verify.
- puts Benchmark.measure { gatekeeper.keys.each { |key| gatekeeper.authorized?(key) } }
Add Comment
Please, Sign In to add comment