Module: JWK::Utils
- Defined in:
- lib/jwk/utils.rb
Class Method Summary collapse
- .binary_to_int(s) ⇒ Object
- .decode_ub64(data) ⇒ Object
- .decode_ub64_int(data) ⇒ Object
- .encode_ub64_int(n) ⇒ Object
- .hex_string_to_binary(s) ⇒ Object
- .int_to_binary(n) ⇒ Object
Class Method Details
.binary_to_int(s) ⇒ Object
17 18 19 20 21 |
# File 'lib/jwk/utils.rb', line 17 def binary_to_int(s) s.chars.inject(0) do |val, char| (val << 8) | char[0].ord end end |
.decode_ub64(data) ⇒ Object
23 24 25 26 27 28 29 30 |
# File 'lib/jwk/utils.rb', line 23 def decode_ub64(data) clean = data.gsub(/[[:space:]]/, '') len = clean.length padded = (len % 4).zero? ? clean : clean + '=' * (4 - len % 4) Base64.urlsafe_decode64(padded) end |
.decode_ub64_int(data) ⇒ Object
32 33 34 |
# File 'lib/jwk/utils.rb', line 32 def decode_ub64_int(data) Utils.binary_to_int(decode_ub64(data)) end |
.encode_ub64_int(n) ⇒ Object
36 37 38 |
# File 'lib/jwk/utils.rb', line 36 def encode_ub64_int(n) Base64.urlsafe_encode64(Utils.int_to_binary(n)) end |
.hex_string_to_binary(s) ⇒ Object
4 5 6 |
# File 'lib/jwk/utils.rb', line 4 def hex_string_to_binary(s) s.scan(/.{2}/).map { |n| n.to_i(16).chr }.join end |
.int_to_binary(n) ⇒ Object
8 9 10 11 12 13 14 15 |
# File 'lib/jwk/utils.rb', line 8 def int_to_binary(n) num_octets = (n.to_s(16).length / 2.0).ceil shifted = n << 8 Array.new(num_octets) do ((shifted >>= 8) & 0xFF).chr end.join.reverse end |