@@ -4,11 +4,11 @@ defmodule Base do
4
4
@ moduledoc """
5
5
This module provides data encoding and decoding functions
6
6
according to [RFC 4648](http://tools.ietf.org/html/rfc4648).
7
-
7
+
8
8
This document defines the commonly used base 64, base 32, and base
9
9
16 encoding schemes.
10
10
"""
11
-
11
+
12
12
b16_alphabet = Enum . with_index '0123456789ABCDEF'
13
13
b64_alphabet = Enum . with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
14
14
b64url_alphabet = Enum . with_index 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_'
@@ -28,7 +28,7 @@ defmodule Base do
28
28
raise ArgumentError , message: "non-alphabet digit found: #{ << c >> } "
29
29
end
30
30
end
31
-
31
+
32
32
@ doc """
33
33
Encodes a binary string into a base 16 encoded string.
34
34
@@ -45,16 +45,16 @@ defmodule Base do
45
45
46
46
@ doc """
47
47
Decodes a base 16 encoded string into a binary string.
48
-
48
+
49
49
The following alphabet is used both for encoding and decoding:
50
-
50
+
51
51
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
52
52
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
53
53
| 0| 0| 4| 4| 8| 8| 12| C|
54
54
| 1| 1| 5| 5| 9| 9| 13| D|
55
55
| 2| 2| 6| 6| 10| A| 14| E|
56
56
| 3| 3| 7| 7| 11| B| 15| F|
57
-
57
+
58
58
## Examples
59
59
60
60
iex> Base.decode16("666F6F626172")
@@ -65,15 +65,15 @@ defmodule Base do
65
65
def decode16 ( string ) when is_binary ( string ) do
66
66
{ :ok , decode16! ( string ) }
67
67
rescue
68
- ArgumentError -> :error
68
+ ArgumentError -> :error
69
69
end
70
-
70
+
71
71
@ doc """
72
72
Decodes a base 16 encoded string into a binary string.
73
-
73
+
74
74
An `ArgumentError` exception is raised if the padding is incorrect or
75
75
a non-alphabet character is present in the string.
76
-
76
+
77
77
## Examples
78
78
79
79
iex> Base.decode16!("666F6F626172")
@@ -101,9 +101,9 @@ defmodule Base do
101
101
102
102
@ doc """
103
103
Decodes a base 64 encoded string into a binary string.
104
-
104
+
105
105
The following alphabet is used both for encoding and decoding:
106
-
106
+
107
107
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
108
108
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
109
109
| 0| A| 17| R| 34| i| 51| z|
@@ -123,7 +123,7 @@ defmodule Base do
123
123
| 14| O| 31| f| 48| w| (pad)| =|
124
124
| 15| P| 32| g| 49| x| | |
125
125
| 16| Q| 33| h| 50| y| | |
126
-
126
+
127
127
## Examples
128
128
129
129
iex> Base.decode64("Zm9vYmFy")
@@ -136,15 +136,15 @@ defmodule Base do
136
136
rescue
137
137
ArgumentError -> :error
138
138
end
139
-
139
+
140
140
@ doc """
141
141
Decodes a base 64 encoded string into a binary string.
142
-
142
+
143
143
The following alphabet is used both for encoding and decoding:
144
-
144
+
145
145
An `ArgumentError` exception is raised if the padding is incorrect or
146
146
a non-alphabet character is present in the string.
147
-
147
+
148
148
## Examples
149
149
150
150
iex> Base.decode64!("Zm9vYmFy")
@@ -164,19 +164,19 @@ defmodule Base do
164
164
165
165
iex> Base.url_encode64(<<255,127,254,252>>)
166
166
"_3_-_A=="
167
-
167
+
168
168
"""
169
169
@ spec url_encode64 ( binary ) :: binary
170
170
def url_encode64 ( data ) when is_binary ( data ) do
171
171
do_encode64 ( data , & enc64url / 1 )
172
172
end
173
-
173
+
174
174
@ doc """
175
175
Decodes a base 64 encoded string with URL and filename safe alphabet
176
176
into a binary string.
177
-
177
+
178
178
The following alphabet is used both for encoding and decoding:
179
-
179
+
180
180
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
181
181
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
182
182
| 0| A| 17| R| 34| i| 51| z|
@@ -196,7 +196,7 @@ defmodule Base do
196
196
| 14| O| 31| f| 48| w| (pad)| =|
197
197
| 15| P| 32| g| 49| x| | |
198
198
| 16| Q| 33| h| 50| y| | |
199
-
199
+
200
200
## Examples
201
201
202
202
iex> Base.url_decode64("_3_-_A==")
@@ -209,14 +209,14 @@ defmodule Base do
209
209
rescue
210
210
ArgumentError -> :error
211
211
end
212
-
212
+
213
213
@ doc """
214
214
Decodes a base 64 encoded string with URL and filename safe alphabet
215
215
into a binary string.
216
-
216
+
217
217
An `ArgumentError` exception is raised if the padding is incorrect or
218
218
a non-alphabet character is present in the string.
219
-
219
+
220
220
## Examples
221
221
222
222
iex> Base.url_decode64!("_3_-_A==")
@@ -227,7 +227,7 @@ defmodule Base do
227
227
def url_decode64! ( string ) when is_binary ( string ) do
228
228
do_decode64 ( string , & dec64url / 1 )
229
229
end
230
-
230
+
231
231
@ doc """
232
232
Encodes a binary string into a base 32 encoded string.
233
233
@@ -244,9 +244,9 @@ defmodule Base do
244
244
245
245
@ doc """
246
246
Decodes a base 32 encoded string into a binary string.
247
-
247
+
248
248
The following alphabet is used both for encoding and decoding:
249
-
249
+
250
250
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
251
251
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
252
252
| 0| A| 9| J| 18| S| 27| 3|
@@ -258,7 +258,7 @@ defmodule Base do
258
258
| 6| G| 15| P| 24| Y| (pad)| =|
259
259
| 7| H| 16| Q| 25| Z| | |
260
260
| 8| I| 17| R| 26| 2| | |
261
-
261
+
262
262
## Examples
263
263
264
264
iex> Base.decode32("MZXW6YTBOI======")
@@ -271,13 +271,13 @@ defmodule Base do
271
271
rescue
272
272
ArgumentError -> :error
273
273
end
274
-
274
+
275
275
@ doc """
276
276
Decodes a base 32 encoded string into a binary string.
277
-
277
+
278
278
An `ArgumentError` exception is raised if the padding is incorrect or
279
279
a non-alphabet character is present in the string.
280
-
280
+
281
281
## Examples
282
282
283
283
iex> Base.decode32!("MZXW6YTBOI======")
@@ -288,7 +288,7 @@ defmodule Base do
288
288
def decode32! ( string ) do
289
289
do_decode32 ( string , & dec32 / 1 )
290
290
end
291
-
291
+
292
292
@ doc """
293
293
Encodes a binary string into a base 32 encoded string with an
294
294
extended hexadecimal alphabet.
@@ -305,11 +305,11 @@ defmodule Base do
305
305
end
306
306
307
307
@ doc """
308
- Decodes a base 32 encoded string with extended hexadecimal alphabet
308
+ Decodes a base 32 encoded string with extended hexadecimal alphabet
309
309
into a binary string.
310
-
310
+
311
311
The following alphabet is used both for encoding and decoding:
312
-
312
+
313
313
| Value | Encoding | Value | Encoding | Value | Encoding | Value | Encoding |
314
314
|------:|---------:|------:|---------:|------:|---------:|------:|---------:|
315
315
| 0| 0| 9| 9| 18| I| 27| R|
@@ -321,7 +321,7 @@ defmodule Base do
321
321
| 6| 6| 15| F| 24| O| (pad)| =|
322
322
| 7| 7| 16| G| 25| P| | |
323
323
| 8| 8| 17| H| 26| Q| | |
324
-
324
+
325
325
## Examples
326
326
327
327
iex> Base.hex_decode32("CPNMUOJ1E8======")
@@ -334,14 +334,14 @@ defmodule Base do
334
334
rescue
335
335
ArgumentError -> :error
336
336
end
337
-
337
+
338
338
@ doc """
339
- Decodes a base 32 encoded string with extended hexadecimal alphabet
339
+ Decodes a base 32 encoded string with extended hexadecimal alphabet
340
340
into a binary string.
341
-
341
+
342
342
An `ArgumentError` exception is raised if the padding is incorrect or
343
343
a non-alphabet character is present in the string.
344
-
344
+
345
345
## Examples
346
346
347
347
iex> Base.hex_decode32!("CPNMUOJ1E8======")
@@ -460,5 +460,5 @@ defmodule Base do
460
460
defp do_decode32 ( _ , _ ) do
461
461
raise ArgumentError , message: "incorrect padding"
462
462
end
463
-
463
+
464
464
end
0 commit comments