-
Notifications
You must be signed in to change notification settings - Fork 469
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a387b67
commit 3e3096f
Showing
10 changed files
with
535 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Eric Biedert | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining | ||
a copy of this software and associated documentation files (the | ||
"Software"), to deal in the Software without restriction, including | ||
without limitation the rights to use, copy, modify, merge, publish, | ||
distribute, sublicense, and/or sell copies of the Software, and to | ||
permit persons to whom the Software is furnished to do so, subject to | ||
the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be | ||
included in all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND | ||
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE | ||
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION | ||
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# based | ||
|
||
A package for encoding and decoding in base64, base32, and base16. | ||
|
||
## Usage | ||
|
||
The package comes with three submodules: `base64`, `base32`, and `base16`. All of them have an `encode` and `decode` function. The package also provides the function aliases | ||
|
||
- `encode64` / `decode64`, | ||
- `encode32` / `decode32`, and | ||
- `encode16` / `decode16`. | ||
|
||
Both base64 and base32 allow you to choose whether to use padding for encoding via the `pad` parameter, which is enabled by default. Base64 also allows you to encode with the URL-safe alphabet by enabling the `url` parameter, while base32 allows you to encode or decode with the "extended hex" alphabet via the `hex` parameter. Both options are disabled by default. The base16 encoder uses lowercase letters, the decoder is case-insensitive. | ||
|
||
You can encode strings, arrays and bytes. The `encode` function will return a string, while the `decode` function will return bytes. | ||
|
||
## Example | ||
|
||
```typ | ||
#import "@preview/based:0.2.0": base64, base32, base16 | ||
#table( | ||
columns: 3, | ||
table.header[*Base64*][*Base32*][*Base16*], | ||
raw(base64.encode("Hello world!")), | ||
raw(base32.encode("Hello world!")), | ||
raw(base16.encode("Hello world!")), | ||
str(base64.decode("SGVsbG8gd29ybGQh")), | ||
str(base32.decode("JBSWY3DPEB3W64TMMQQQ====")), | ||
str(base16.decode("48656C6C6F20776F726C6421")) | ||
) | ||
``` | ||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
#import "@preview/based:0.2.0": base64, base32, base16 | ||
|
||
#set text(size: 14pt) | ||
#set page( | ||
width: auto, | ||
height: auto, | ||
margin: 1em, | ||
fill: none, | ||
background: pad(0.5pt, box( | ||
width: 100%, | ||
height: 100%, | ||
radius: 4pt, | ||
fill: white, | ||
stroke: white.darken(10%), | ||
)), | ||
) | ||
|
||
#table( | ||
columns: 3, | ||
inset: 0.5em, | ||
|
||
table.header[*Base64*][*Base32*][*Base16*], | ||
|
||
raw(base64.encode("Hello world!")), | ||
raw(base32.encode("Hello world!")), | ||
raw(base16.encode("Hello world!")), | ||
|
||
str(base64.decode("SGVsbG8gd29ybGQh")), | ||
str(base32.decode("JBSWY3DPEB3W64TMMQQQ====")), | ||
str(base16.decode("48656C6C6F20776F726C6421")) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#let plugin = plugin("based.wasm") | ||
|
||
/// Encodes the given data as a hex string. | ||
/// | ||
/// Arguments: | ||
/// - data: The data to encode. Must be of type array, bytes, or string. | ||
/// | ||
/// Returns: The encoded string (lowercase). | ||
#let encode(data) = { | ||
str(plugin.encode16(bytes(data))) | ||
} | ||
|
||
/// Decodes the given hex string. | ||
/// | ||
/// Arguments: | ||
/// - string: The string to decode (case-insensitive). | ||
/// | ||
/// Returns: The decoded bytes. | ||
#let decode(string) = { | ||
plugin.decode16(bytes(string)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#let plugin = plugin("based.wasm") | ||
|
||
/// Encodes the given data in base32 format. | ||
/// | ||
/// Arguments: | ||
/// - data: The data to encode. Must be of type array, bytes, or string. | ||
/// - pad: Whether to pad the output with "=" characters. | ||
/// - hex: Whether to use the extended base32hex alphabet. | ||
/// | ||
/// Returns: The encoded string. | ||
#let encode(data, pad: true, hex: false) = { | ||
let flags = bytes((if pad { 1 } else { 0 }, if hex { 1 } else { 0 })) | ||
str(plugin.encode32(bytes(data), flags)) | ||
} | ||
|
||
/// Decodes the given base32 string. | ||
/// | ||
/// Arguments: | ||
/// - string: The string to decode. | ||
/// - hex: Whether to use the extended base32hex alphabet. | ||
/// | ||
/// Returns: The decoded bytes. | ||
#let decode(string, hex: false) = { | ||
let flags = bytes((0, if hex { 1 } else { 0 })) | ||
let string = string.trim("=", at: end) | ||
plugin.decode32(bytes(string), flags) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#let plugin = plugin("based.wasm") | ||
|
||
/// Encodes the given data in base64 format. | ||
/// | ||
/// Arguments: | ||
/// - data: The data to encode. Must be of type array, bytes, or string. | ||
/// - pad: Whether to pad the output with "=" characters. | ||
/// - url: Whether to use the URL safe alphabet. | ||
/// | ||
/// Returns: The encoded string. | ||
#let encode(data, pad: true, url: false) = { | ||
let flags = bytes((if pad { 1 } else { 0 }, if url { 1 } else { 0 })) | ||
str(plugin.encode64(bytes(data), flags)) | ||
} | ||
|
||
/// Decodes the given base64 string. | ||
/// | ||
/// URL safe characters are automatically converted to their standard | ||
/// counterparts. | ||
/// | ||
/// Arguments: | ||
/// - string: The string to decode. | ||
/// | ||
/// Returns: The decoded bytes. | ||
#let decode(string) = { | ||
let flags = bytes((0, 0)) | ||
let string = string.replace("-", "+").replace("_", "/").trim("=", at: end) | ||
plugin.decode64(bytes(string), flags) | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#import "base64.typ" | ||
#import "base32.typ" | ||
#import "base16.typ" | ||
|
||
#let encode64 = base64.encode | ||
#let decode64 = base64.decode | ||
|
||
#let encode32 = base32.encode | ||
#let decode32 = base32.decode | ||
|
||
#let encode16 = base16.encode | ||
#let decode16 = base16.decode |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
[package] | ||
name = "based" | ||
version = "0.2.0" | ||
entrypoint = "src/lib.typ" | ||
authors = ["Eric Biedert"] | ||
license = "MIT" | ||
description = "Encoder and decoder for base64, base32, and base16." | ||
repository = "https://github.com/EpicEricEE/typst-based" | ||
exclude = ["assets", "tests"] | ||
categories = ["scripting"] |