@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/encodingFunctions
Section titled “Functions”Base64 Encoding
Section titled “Base64 Encoding”base64Encode(str: string): string
Section titled “base64Encode(str: string): string”Encodes a string using standard Base64 encoding.
Parameters:
str- The string to encode
Returns: Base64-encoded string
Example:
use { base64Encode } from @sentrie/encodinglet encoded = encoding.base64Encode("Hello, World!") // "SGVsbG8sIFdvcmxkIQ=="base64Decode(str: string): string
Section titled “base64Decode(str: string): string”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/encodinglet decoded = encoding.base64Decode("SGVsbG8sIFdvcmxkIQ==") // "Hello, World!"URL-Safe Base64 Encoding
Section titled “URL-Safe Base64 Encoding”base64UrlEncode(str: string): string
Section titled “base64UrlEncode(str: string): string”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/encodinglet encoded = encoding.base64UrlEncode("Hello, World!")base64UrlDecode(str: string): string
Section titled “base64UrlDecode(str: string): string”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
Hex Encoding
Section titled “Hex Encoding”hexEncode(str: string): string
Section titled “hexEncode(str: string): string”Encodes a string to hexadecimal representation.
Parameters:
str- The string to encode
Returns: Hexadecimal-encoded string (e.g., "48656c6c6f")
Example:
use { hexEncode } from @sentrie/encodinglet encoded = encoding.hexEncode("Hello") // "48656c6c6f"hexDecode(str: string): string
Section titled “hexDecode(str: string): string”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/encodinglet decoded = encoding.hexDecode("48656c6c6f") // "Hello"URL Encoding
Section titled “URL Encoding”urlEncode(str: string): string
Section titled “urlEncode(str: string): string”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/encodinglet encoded = encoding.urlEncode("Hello, World!") // "Hello%2C%20World%21"urlDecode(str: string): string
Section titled “urlDecode(str: string): string”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/encodinglet decoded = encoding.urlDecode("Hello%2C%20World%21") // "Hello, World!"Complete Example
Section titled “Complete Example”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}