-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathob64.rb
96 lines (86 loc) · 2.83 KB
/
ob64.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# frozen_string_literal: true
require_relative "ob64/version"
require_relative "ob64_ext"
# Methods for base64-encoding and -decoding strings.
module Ob64
# The glue between Ruby and libbase64. See +../ext/libbase64/ob64/ob64_ext.c+.
module LibBase64; end
include LibBase64
extend LibBase64
module_function
# Returns the Base64-encoded version of +bin+.
# This method complies with RFC 4648.
# No line feeds are added.
#
# @param bin [String]
# @return [String]
def encode(bin)
__encode(bin)
end
# Returns the Base64-decoded version of +string+.
# This method complies with RFC 4648.
# ArgumentError is raised if +string+ is incorrectly padded or contains
# non-alphabet characters. Note that CR or LF are also rejected.
#
# @param string [String]
# @return [String]
# @raise [ArgumentError] if +string+ cannot be decoded
def decode(string)
__decode(string)
end
# Returns the Base64-encoded version of +bin+.
# This method complies with ``Base 64 Encoding with URL and Filename Safe
# Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
# Note that the result can still contain '='.
# You can remove the padding by setting +padding+ as false.
#
# @param bin [String]
# @param padding [Boolean] - if the output must be padded
# @return [String]
def urlsafe_encode(bin, padding: true)
string = __encode(bin)
string.chomp!("==") || string.chomp!("=") unless padding
string.tr!("+/", "-_")
string
end
# Returns the Base64-decoded version of +string+.
# This method complies with ``Base 64 Encoding with URL and Filename Safe
# Alphabet'' in RFC 4648.
# The alphabet uses '-' instead of '+' and '_' instead of '/'.
#
# The padding character is optional.
# This method accepts both correctly-padded and unpadded input.
# Note that it still rejects incorrectly-padded input.
#
# @param string [String]
# @return [String]
# @raise [ArgumentError] if +string+ cannot be decoded
def urlsafe_decode(string)
if !string.end_with?("=") && string.length % 4 != 0
string = string.ljust((string.length + 3) & ~3, "=")
string.tr!("-_", "+/")
else
string = string.tr("-_", "+/")
end
__decode(string)
end
# Returns the length of the Base64-encoded version of +bin+.
#
# @param bin [String]
# @param padding [Boolean] - if the Base64-encoded version of +bin+ will be padded
# @return [Integer]
def encoded_length_of(bin, padding: true)
__encoded_length_of(bin, padding)
end
# Returns the length of the Base64-decoded version of +string+.
#
# ArgumentError is raised if +string+ has an invalid length.
#
# @param string [String]
# @return [Integer]
# @raise [ArgumentError] if +string+ has an invalid length
def decoded_length_of(string)
__decoded_length_of(string)
end
end