Skip to content

Commit

Permalink
chore: shorten encoder/decoder config names
Browse files Browse the repository at this point in the history
  • Loading branch information
grjte committed Oct 30, 2024
1 parent 9d3d9f0 commit 3fc82c6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,31 +9,31 @@ Start by selecting the encoder or decoder for your configuration. These are defi
RFC 4648 specifies multiple alphabets, including the [standard Base 64 Alphabet](https://datatracker.ietf.org/doc/html/rfc4648#section-4) known as `base64` and the ["URL and Filename Safe Alphabet"](https://datatracker.ietf.org/doc/html/rfc4648#section-5) known as `base64url`. It also specifies that [padding](https://datatracker.ietf.org/doc/html/rfc4648#section-3.2) should be required in the general case but can be explicitly omitted as an option.

Available encoder configurations:
- `BASE64_ENCODER_STANDARD`: uses the standard alphabet (base64) and adds padding.
- `BASE64_ENCODER_STANDARD_NO_PAD`: uses the standard alphabet (base64), but omits padding.
- `BASE64_ENCODER_URL_SAFE`: uses the "URL and Filename Safe Alphabet" (base64url) and adds padding.
- `BASE64_ENCODER_URL_SAFE_NO_PAD`: uses the "URL and Filename Safe Alphabet" (base64url), but omits padding.
- `ENCODER_STANDARD`: uses the standard alphabet (base64) and adds padding.
- `ENCODER_STANDARD_NO_PAD`: uses the standard alphabet (base64), but omits padding.
- `ENCODER_URL_SAFE`: uses the "URL and Filename Safe Alphabet" (base64url) and adds padding.
- `ENCODER_URL_SAFE_NO_PAD`: uses the "URL and Filename Safe Alphabet" (base64url), but omits padding.

Available decoder configurations:
- `BASE64_DECODER_STANDARD`: uses the standard alphabet (base64) and expects correct padding.
- `BASE64_DECODER_STANDARD_NO_PAD`: uses the standard alphabet (base64), but expects all padding characters to have been stripped. A padding character encoutered during decoding will trigger an error.
- `BASE64_DECODER_URL_SAFE`: uses the "URL and Filename Safe Alphabet" (base64url) and expects correct padding.
- `BASE64_DECODER_URL_SAFE_NO_PAD`: uses the "URL and Filename Safe Alphabet" (base64url), but expects all padding characters to have been stripped. A padding character encoutered during decoding will trigger an error.
- `DECODER_STANDARD`: uses the standard alphabet (base64) and expects correct padding.
- `DECODER_STANDARD_NO_PAD`: uses the standard alphabet (base64), but expects all padding characters to have been stripped. A padding character encoutered during decoding will trigger an error.
- `DECODER_URL_SAFE`: uses the "URL and Filename Safe Alphabet" (base64url) and expects correct padding.
- `DECODER_URL_SAFE_NO_PAD`: uses the "URL and Filename Safe Alphabet" (base64url), but expects all padding characters to have been stripped. A padding character encoutered during decoding will trigger an error.

### `fn encode`
Takes an arbitrary byte array as input, encodes it in Base64 according to the alphabet and padding rules specified by the configuration, then encodes each Base64 character into UTF-8 to return a byte array representing the Base64 encoding.

```
// bytes: [u8; N]
let base64 = BASE64_ENCODER_STANDARD.encode(bytes);
let base64 = ENCODER_STANDARD.encode(bytes);
```

### `fn decode`
Takes a utf-8 byte array that encodes a Base64 string and attempts to decoded it into bytes according to the provided configuration specifying the alphabet and padding rules.

```
// base64: [u8; N]
let bytes = BASE64_DECODER_STANDARD.decode(base64);
let bytes = DECODER_STANDARD.decode(base64);
```

## Example usage
Expand All @@ -44,10 +44,10 @@ fn encode_and_decode() {
let input: str<88> = "The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox.";
let base64_encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg==";
let encoded:[u8; 120] = BASE64_ENCODER_STANDARD.encode(input.as_bytes());
let encoded:[u8; 120] = noir_base64::ENCODER_STANDARD.encode(input.as_bytes());
assert(encoded == base64_encoded.as_bytes());
let decoded: [u8; 88] = BASE64_DECODER_STANDARD.decode(encoded);
let decoded: [u8; 88] = noir_base64::DECODER_STANDARD.decode(encoded);
assert(decoded == input.as_bytes());
}
```
Expand Down
16 changes: 8 additions & 8 deletions src/lib.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
// output string.
mod encoder;
pub use encoder::{
STANDARD as BASE64_ENCODER_STANDARD, STANDARD_NO_PAD as BASE64_ENCODER_STANDARD_NO_PAD,
URL_SAFE as BASE64_ENCODER_URL_SAFE, URL_SAFE_NO_PAD as BASE64_ENCODER_URL_SAFE_NO_PAD,
STANDARD as ENCODER_STANDARD, STANDARD_NO_PAD as ENCODER_STANDARD_NO_PAD,
URL_SAFE as ENCODER_URL_SAFE, URL_SAFE_NO_PAD as ENCODER_URL_SAFE_NO_PAD,
};

mod decoder;
pub use decoder::{
STANDARD as BASE64_DECODER_STANDARD, STANDARD_NO_PAD as BASE64_DECODER_STANDARD_NO_PAD,
URL_SAFE as BASE64_DECODER_URL_SAFE, URL_SAFE_NO_PAD as BASE64_DECODER_URL_SAFE_NO_PAD,
STANDARD as DECODER_STANDARD, STANDARD_NO_PAD as DECODER_STANDARD_NO_PAD,
URL_SAFE as DECODER_URL_SAFE, URL_SAFE_NO_PAD as DECODER_URL_SAFE_NO_PAD,
};

pub(crate) mod defaults {
Expand All @@ -37,10 +37,10 @@ fn encode_and_decode() {
"The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox.";
let base64_encoded = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg==";

let encoded: [u8; 120] = BASE64_ENCODER_STANDARD.encode(input.as_bytes());
let encoded: [u8; 120] = ENCODER_STANDARD.encode(input.as_bytes());
assert(encoded == base64_encoded.as_bytes());

let decoded: [u8; 88] = BASE64_DECODER_STANDARD.decode(encoded);
let decoded: [u8; 88] = DECODER_STANDARD.decode(encoded);
assert(decoded == input.as_bytes());
}

Expand All @@ -50,9 +50,9 @@ fn encode_and_decode_no_pad() {
"The quick brown fox jumps over the lazy dog, while 42 ravens perch atop a rusty mailbox.";
let base64_encoded: str<118> = "VGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIHRoZSBsYXp5IGRvZywgd2hpbGUgNDIgcmF2ZW5zIHBlcmNoIGF0b3AgYSBydXN0eSBtYWlsYm94Lg";

let encoded: [u8; 118] = BASE64_ENCODER_STANDARD_NO_PAD.encode(input.as_bytes());
let encoded: [u8; 118] = ENCODER_STANDARD_NO_PAD.encode(input.as_bytes());
assert(encoded == base64_encoded.as_bytes());

let decoded: [u8; 88] = BASE64_DECODER_STANDARD_NO_PAD.decode(encoded);
let decoded: [u8; 88] = DECODER_STANDARD_NO_PAD.decode(encoded);
assert(decoded == input.as_bytes());
}

0 comments on commit 3fc82c6

Please sign in to comment.