Skip to content

Commit 24681ff

Browse files
authored
Implement base64url (#37)
* Implement base64url * Refactor base64.c
1 parent 787efd4 commit 24681ff

10 files changed

Lines changed: 163 additions & 69 deletions

File tree

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# secretbase (development version)
22

3+
* `base64enc()` and `base64dec()` gain a `url` argument for the URL- and filename-safe base64 variant (RFC 4648 section 5), using the `-` and `_` alphabet without padding.
4+
35
# secretbase 1.2.2
46

57
* Speeds up hex string conversion for hash output (`convert = TRUE`).

R/base.R

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,15 @@
88
#' whilst all other objects are first serialized (using R serialisation version
99
#' 3, big-endian representation).
1010
#'
11+
#' Set `url = TRUE` for the URL- and filename-safe variant (RFC 4648 section 5),
12+
#' which substitutes `-` and `_` for `+` and `/` and omits padding (`=`), making
13+
#' the output safe to use within URLs and filenames without further escaping.
14+
#'
1115
#' @param x an object.
1216
#' @param convert logical `TRUE` to encode to a character string or `FALSE` to a
1317
#' raw vector.
18+
#' @param url logical `TRUE` to use the URL- and filename-safe base64url alphabet
19+
#' without padding, or `FALSE` (the default) for standard base64.
1420
#'
1521
#' @return A character string or raw vector depending on the value of `convert`.
1622
#'
@@ -25,10 +31,12 @@
2531
#' base64enc("secret base")
2632
#' base64enc(as.raw(c(1L, 2L, 4L)), convert = FALSE)
2733
#' base64enc(data.frame())
34+
#' base64enc("secret base", url = TRUE)
2835
#'
2936
#' @export
3037
#'
31-
base64enc <- function(x, convert = TRUE) .Call(secretbase_base64enc, x, convert)
38+
base64enc <- function(x, convert = TRUE, url = FALSE)
39+
.Call(secretbase_base64enc, x, convert, url)
3240

3341
#' Base64 Decode
3442
#'
@@ -38,10 +46,16 @@ base64enc <- function(x, convert = TRUE) .Call(secretbase_base64enc, x, convert)
3846
#' reverse of the 3 encoding operations (for strings, raw vectors and arbitrary
3947
#' objects), in order to return the original object.
4048
#'
49+
#' Set `url = TRUE` to decode the URL- and filename-safe variant (RFC 4648
50+
#' section 5), which expects the `-` and `_` alphabet without padding; inputs
51+
#' containing `+`, `/` or `=` are then rejected. The value of `url` must match
52+
#' the variant used to encode.
53+
#'
4154
#' @param x an object.
4255
#' @param convert logical `TRUE` to convert back to a character string, `FALSE`
4356
#' to convert back to a raw vector or `NA` to decode and then unserialize back
4457
#' to the original object.
58+
#' @inheritParams base64enc
4559
#'
4660
#' @return A character string, raw vector, or other object depending on the
4761
#' value of `convert`.
@@ -57,10 +71,12 @@ base64enc <- function(x, convert = TRUE) .Call(secretbase_base64enc, x, convert)
5771
#' base64dec(base64enc("secret base"))
5872
#' base64dec(base64enc(as.raw(c(1L, 2L, 4L))), convert = FALSE)
5973
#' base64dec(base64enc(data.frame()), convert = NA)
74+
#' base64dec(base64enc("secret base", url = TRUE), url = TRUE)
6075
#'
6176
#' @export
6277
#'
63-
base64dec <- function(x, convert = TRUE) .Call(secretbase_base64dec, x, convert)
78+
base64dec <- function(x, convert = TRUE, url = FALSE)
79+
.Call(secretbase_base64dec, x, convert, url)
6480

6581
#' Base58 Encode
6682
#'

README.Rmd

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Implements the SHA-256, SHA-3 and 'Keccak' cryptographic hash functions, SHAKE25
4343
| `sha3()` `sha256()` `keccak()` | Cryptographic hashes |
4444
| `shake256()` | Extendable-output function (XOF) |
4545
| `siphash13()` | Keyed, fast pseudo-random function |
46-
| `base64enc()` `base64dec()` | Base64 encoding |
46+
| `base64enc()` `base64dec()` | Base64 encoding (incl. URL-safe variant) |
4747
| `base58enc()` `base58dec()` | Base58 encoding with checksum |
4848
| `cborenc()` `cbordec()` | CBOR serialization |
4949
| `jsonenc()` `jsondec()` | JSON serialization |
@@ -154,6 +154,13 @@ base64enc(as.raw(c(1L, 2L, 4L)), convert = FALSE)
154154
base64dec(base64enc(data.frame()), convert = NA)
155155
```
156156

157+
Set `url = TRUE` for the URL- and filename-safe variant (RFC 4648 section 5), which uses the `-` and `_` alphabet without padding:
158+
```{r}
159+
#| label: base64url
160+
base64enc("secret base", url = TRUE)
161+
base64dec(base64enc("secret base", url = TRUE), url = TRUE)
162+
```
163+
157164
#### Base58
158165

159166
Includes a 4-byte checksum (double SHA-256), verified on decode:

README.md

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@ Implements the SHA-256, SHA-3 and ‘Keccak’ cryptographic hash functions,
3434
SHAKE256 extendable-output function (XOF), ‘SipHash’ pseudo-random
3535
function, base64 and base58 encoding, ‘CBOR’ and ‘JSON’ serialization.
3636

37-
| Function | Purpose |
38-
|--------------------------------|------------------------------------|
39-
| `sha3()` `sha256()` `keccak()` | Cryptographic hashes |
40-
| `shake256()` | Extendable-output function (XOF) |
41-
| `siphash13()` | Keyed, fast pseudo-random function |
42-
| `base64enc()` `base64dec()` | Base64 encoding |
43-
| `base58enc()` `base58dec()` | Base58 encoding with checksum |
44-
| `cborenc()` `cbordec()` | CBOR serialization |
45-
| `jsonenc()` `jsondec()` | JSON serialization |
37+
| Function | Purpose |
38+
|--------------------------------|------------------------------------------|
39+
| `sha3()` `sha256()` `keccak()` | Cryptographic hashes |
40+
| `shake256()` | Extendable-output function (XOF) |
41+
| `siphash13()` | Keyed, fast pseudo-random function |
42+
| `base64enc()` `base64dec()` | Base64 encoding (incl. URL-safe variant) |
43+
| `base58enc()` `base58dec()` | Base58 encoding with checksum |
44+
| `cborenc()` `cbordec()` | CBOR serialization |
45+
| `jsonenc()` `jsondec()` | JSON serialization |
4646

4747
### Installation
4848

@@ -167,6 +167,16 @@ base64dec(base64enc(data.frame()), convert = NA)
167167
#> data frame with 0 columns and 0 rows
168168
```
169169

170+
Set `url = TRUE` for the URL- and filename-safe variant (RFC 4648
171+
section 5), which uses the `-` and `_` alphabet without padding:
172+
173+
``` r
174+
base64enc("secret base", url = TRUE)
175+
#> [1] "c2VjcmV0IGJhc2U"
176+
base64dec(base64enc("secret base", url = TRUE), url = TRUE)
177+
#> [1] "secret base"
178+
```
179+
170180
#### Base58
171181

172182
Includes a 4-byte checksum (double SHA-256), verified on decode:

man/base64dec.Rd

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

man/base64enc.Rd

Lines changed: 9 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)