encode/decode for multiple alphabets (#13428)

Based on the discussion in #13419.


## Description

Reworks the `decode`/`encode` commands by adding/changing the following
bases:

- `base32`
- `base32hex`
- `hex`
- `new-base64`

The `hex` base is compatible with the previous version of `hex` out of
the box (it only adds more flags). `base64` isn't, so the PR adds a new
version and deprecates the old one.

All commands have `string -> binary` signature for decoding and `string
| binary -> string` signature for encoding. A few `base64` encodings,
which are not a part of the
[RFC4648](https://datatracker.ietf.org/doc/html/rfc4648#section-6), have
been dropped.


## Example usage

```Nushell
~/fork/nushell> "string" | encode base32 | decode base32 | decode
string
```

```Nushell
~/fork/nushell> "ORSXG5A=" | decode base32
# `decode` always returns a binary value
Length: 4 (0x4) bytes | printable whitespace ascii_other non_ascii
00000000:   74 65 73 74                                          test
```


## User-Facing Changes

- New commands: `encode/decode base32/base32hex`.
- `encode hex` gets a `--lower` flag.
- `encode/decode base64` deprecated in favor of `encode/decode
new-base64`.
This commit is contained in:
Andrej Kolčin
2024-08-23 19:18:51 +03:00
committed by GitHub
parent 39b0f3bdda
commit 0560826414
23 changed files with 1122 additions and 210 deletions

View File

@@ -0,0 +1,36 @@
use nu_test_support::nu;
#[test]
fn canonical() {
for value in super::random_bytes() {
let outcome = nu!("{} | encode hex | decode hex | to nuon", value);
assert_eq!(outcome.out, value);
}
}
#[test]
fn encode() {
let text = "Ș̗͙̂̏o̲̲̗͗̌͊m̝̊̓́͂ë̡̦̞̤́̌̈́̀ ̥̝̪̎̿ͅf̧̪̻͉͗̈́̍̆u̮̝͌̈́ͅn̹̞̈́̊k̮͇̟͎̂͘y̧̲̠̾̆̕ͅ ̙͖̭͔̂̐t̞́́͘e̢̨͕̽x̥͋t͍̑̔͝";
let encoded = "53CC82CC8FCCA6CC97CD996FCD97CC8CCD8ACCB2CCB2CC976DCC8ACC93CC81CD82CC9D65CD84CC8CCD84CC80CCA6CCA1CC9ECCA420CC8ECCBFCD85CCA5CC9DCCAA66CD97CD84CC8DCC86CCAACCBBCCA7CD8975CD8CCD84CCAECC9DCD856ECD84CC8ACCB9CC9E6BCD98CC82CCAECD87CC9FCD8E79CCBECC95CC86CD85CCB2CCA7CCA020CC82CC90CC99CD96CCADCD9474CC81CD98CC81CC9E65CCBDCCA2CD95CCA878CD8BCCA574CC91CC94CD9DCD8D";
let outcome = nu!("'{}' | encode hex", text);
assert_eq!(outcome.out, encoded);
}
#[test]
fn decode_string() {
let text = "Very important data";
let encoded = "5665727920696D706F7274616E742064617461";
let outcome = nu!("'{}' | decode hex | decode", encoded);
assert_eq!(outcome.out, text);
}
#[test]
fn decode_case_mixing() {
let text = "®lnnE¾ˆë";
let mixed_encoded = "c2aE6c6e6E45C2BeCB86c3ab";
let outcome = nu!("'{}' | decode hex | decode", mixed_encoded);
assert_eq!(outcome.out, text);
}