Guest User

Untitled

a guest
Dec 12th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  1. #!/usr/bin/env ruby
  2.  
  3. require "securerandom"
  4. require "benchmark"
  5. require "openssl"
  6.  
  7. class Svc2Svc
  8. def initialize(count: 1000)
  9. @keys = Array.new(1000) { SecureRandom.hex }
  10. @services = keys.each_with_object({paysvc_live: [], paysvc_sandbox: [], banking: []}) do |key, services|
  11. services[services.keys.shuffle.first] << hmac_encode(key)
  12. end
  13. end
  14.  
  15. attr_reader :keys, :services
  16.  
  17. def authorized?(token)
  18. @services.each do |service, keys|
  19. return service if keys.find { |key| key == hmac_encode(token) }
  20. end
  21.  
  22. nil
  23. end
  24.  
  25. private
  26.  
  27. def hmac_encode(string)
  28. OpenSSL::HMAC.new(string, OpenSSL::Digest.new("sha256")).to_s
  29. end
  30. end
  31.  
  32. gatekeeper = Svc2Svc.new
  33.  
  34. # Takes about 1.4s, so 1.4ms to authorize a token when we have 1000 service-to-service
  35. # keys to verify.
  36. puts Benchmark.measure { gatekeeper.keys.each { |key| gatekeeper.authorized?(key) } }
Add Comment
Please, Sign In to add comment