From 3fc82c61bbc3656a8e323fb00cb1a11c25508dfe Mon Sep 17 00:00:00 2001 From: grjte Date: Wed, 30 Oct 2024 09:42:04 +0000 Subject: [PATCH] chore: shorten encoder/decoder config names --- README.md | 24 ++++++++++++------------ src/lib.nr | 16 ++++++++-------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index e91d104..9913bdc 100644 --- a/README.md +++ b/README.md @@ -9,23 +9,23 @@ 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` @@ -33,7 +33,7 @@ Takes a utf-8 byte array that encodes a Base64 string and attempts to decoded it ``` // base64: [u8; N] -let bytes = BASE64_DECODER_STANDARD.decode(base64); +let bytes = DECODER_STANDARD.decode(base64); ``` ## Example usage @@ -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()); } ``` diff --git a/src/lib.nr b/src/lib.nr index 52c830f..da8952d 100644 --- a/src/lib.nr +++ b/src/lib.nr @@ -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 { @@ -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()); } @@ -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()); }