Skip to content

@sentrie/encoding

The @sentrie/encoding module provides various encoding and decoding utilities. Supports Base64, Hex, and URL encoding/decoding operations.

use { base64Encode, base64Decode, hexEncode, urlEncode } from @sentrie/encoding

Encodes a string using standard Base64 encoding.

Parameters:

  • str - The string to encode

Returns: Base64-encoded string

Example:

use { base64Encode } from @sentrie/encoding
let encoded = encoding.base64Encode("Hello, World!") // "SGVsbG8sIFdvcmxkIQ=="

Decodes a Base64-encoded string.

Parameters:

  • str - The Base64-encoded string to decode

Returns: Decoded string

Throws: Error if the input is not valid Base64

Example:

use { base64Decode } from @sentrie/encoding
let decoded = encoding.base64Decode("SGVsbG8sIFdvcmxkIQ==") // "Hello, World!"

Encodes a string using URL-safe Base64 encoding. Uses - and _ instead of + and /, and omits padding.

Parameters:

  • str - The string to encode

Returns: URL-safe Base64-encoded string

Example:

use { base64UrlEncode } from @sentrie/encoding
let encoded = encoding.base64UrlEncode("Hello, World!")

Decodes a URL-safe Base64-encoded string.

Parameters:

  • str - The URL-safe Base64-encoded string to decode

Returns: Decoded string

Throws: Error if the input is not valid URL-safe Base64

Encodes a string to hexadecimal representation.

Parameters:

  • str - The string to encode

Returns: Hexadecimal-encoded string (e.g., "48656c6c6f")

Example:

use { hexEncode } from @sentrie/encoding
let encoded = encoding.hexEncode("Hello") // "48656c6c6f"

Decodes a hexadecimal string.

Parameters:

  • str - The hexadecimal string to decode (e.g., "48656c6c6f")

Returns: Decoded string

Throws: Error if the input is not valid hexadecimal

Example:

use { hexDecode } from @sentrie/encoding
let decoded = encoding.hexDecode("48656c6c6f") // "Hello"

URL-encodes a string using percent encoding (query string encoding). Encodes special characters as %XX hexadecimal sequences.

Parameters:

  • str - The string to encode

Returns: URL-encoded string

Example:

use { urlEncode } from @sentrie/encoding
let encoded = encoding.urlEncode("Hello, World!") // "Hello%2C%20World%21"

Decodes a URL-encoded string.

Parameters:

  • str - The URL-encoded string to decode

Returns: Decoded string

Throws: Error if the input contains invalid encoding sequences

Example:

use { urlDecode } from @sentrie/encoding
let decoded = encoding.urlDecode("Hello%2C%20World%21") // "Hello, World!"
namespace com/example/mypolicy
policy mypolicy {
use { base64Encode, base64Decode, urlEncode, urlDecode } from @sentrie/encoding
fact data!: string
rule encodeData = default false {
let encoded = encoding.base64Encode(data)
let decoded = encoding.base64Decode(encoded)
let urlEncoded = encoding.urlEncode(data)
yield decoded == data
}
export decision of encodeData
}