Module: HTTP2::Base64

Defined in:
lib/http/2/base64.rb

Overview

require "base64" will not be a default gem after ruby 3.4.0

Class Method Summary collapse

Class Method Details

.decode64(str) ⇒ Object



15
16
17
# File 'lib/http/2/base64.rb', line 15

def decode64(str)
  str.unpack1("m")
end

.encode64(bin) ⇒ Object



11
12
13
# File 'lib/http/2/base64.rb', line 11

def encode64(bin)
  [bin].pack("m")
end

.strict_decode64(str) ⇒ Object



23
24
25
# File 'lib/http/2/base64.rb', line 23

def strict_decode64(str)
  str.unpack1("m0")
end

.strict_encode64(bin) ⇒ Object



19
20
21
# File 'lib/http/2/base64.rb', line 19

def strict_encode64(bin)
  [bin].pack("m0")
end

.urlsafe_decode64(str) ⇒ Object



34
35
36
37
38
39
40
41
42
# File 'lib/http/2/base64.rb', line 34

def urlsafe_decode64(str)
  if !str.end_with?("=") && str.length % 4 != 0
    str = str.ljust((str.length + 3) & ~3, "=")
    str.tr!("-_", "+/")
  else
    str = str.tr("-_", "+/")
  end
  strict_decode64(str)
end

.urlsafe_encode64(bin, padding: true) ⇒ Object



27
28
29
30
31
32
# File 'lib/http/2/base64.rb', line 27

def urlsafe_encode64(bin, padding: true)
  str = strict_encode64(bin)
  str.chomp!("==") or str.chomp!("=") unless padding
  str.tr!("+/", "-_")
  str
end