shape-runtime 0.3.1

Bytecode compiler, builtins, and runtime infrastructure for Shape
Documentation
/// @module std::core::crypto
/// Cryptographic Hashing and Encoding
///
/// Hash functions (SHA-256, SHA-512, SHA-1, MD5), HMAC, Base64/hex
/// encoding and decoding, secure random bytes, and Ed25519 digital signatures.
///
/// # Example
///
/// ```shape
/// use std::core::crypto
///
/// let hash = crypto.sha256("hello")
/// let encoded = crypto.base64_encode("secret data")
/// ```

/// Compute the SHA-256 hash of a string, returning a hex-encoded digest.
///
/// # Arguments
///
/// * `data` - Data to hash
///
/// # Returns
///
/// Hex-encoded 64-character SHA-256 digest string.
///
/// # Example
///
/// ```shape
/// crypto.sha256("hello")  // "2cf24dba5fb0a30e..."
/// ```
pub builtin fn sha256(data: string) -> string;

/// Compute the SHA-512 hash of a string, returning a hex-encoded digest.
///
/// # Arguments
///
/// * `data` - Data to hash
///
/// # Returns
///
/// Hex-encoded 128-character SHA-512 digest string.
///
/// # Example
///
/// ```shape
/// crypto.sha512("hello")
/// ```
pub builtin fn sha512(data: string) -> string;

/// Compute the SHA-1 hash of a string, returning a hex-encoded digest (legacy).
///
/// # Arguments
///
/// * `data` - Data to hash
///
/// # Returns
///
/// Hex-encoded 40-character SHA-1 digest string.
///
/// # Example
///
/// ```shape
/// crypto.sha1("hello")  // "aaf4c61ddcc5e8a2..."
/// ```
pub builtin fn sha1(data: string) -> string;

/// Compute the MD5 hash of a string, returning a hex-encoded digest (legacy).
///
/// # Arguments
///
/// * `data` - Data to hash
///
/// # Returns
///
/// Hex-encoded 32-character MD5 digest string.
///
/// # Example
///
/// ```shape
/// crypto.md5("hello")  // "5d41402abc4b2a76..."
/// ```
pub builtin fn md5(data: string) -> string;

/// Compute HMAC-SHA256 of data with the given key, returning a hex digest.
///
/// # Arguments
///
/// * `data` - Data to authenticate
/// * `key` - HMAC key
///
/// # Returns
///
/// Hex-encoded 64-character HMAC-SHA256 digest string.
///
/// # Example
///
/// ```shape
/// crypto.hmac_sha256("message", "secret-key")
/// ```
pub builtin fn hmac_sha256(data: string, key: string) -> string;

/// Encode a string to Base64.
///
/// # Arguments
///
/// * `data` - Data to encode
///
/// # Returns
///
/// Base64-encoded string.
///
/// # Example
///
/// ```shape
/// crypto.base64_encode("Hello, World!")  // "SGVsbG8sIFdvcmxkIQ=="
/// ```
pub builtin fn base64_encode(data: string) -> string;

/// Decode a Base64 string.
///
/// # Arguments
///
/// * `encoded` - Base64-encoded string to decode
///
/// # Returns
///
/// `Ok(decoded)` with the decoded UTF-8 string, or `Err(message)` on failure.
///
/// # Example
///
/// ```shape
/// crypto.base64_decode("SGVsbG8sIFdvcmxkIQ==")  // Ok("Hello, World!")
/// ```
pub builtin fn base64_decode(encoded: string) -> Result<string, string>;

/// Encode a string as hexadecimal.
///
/// # Arguments
///
/// * `data` - Data to hex-encode
///
/// # Returns
///
/// Hex-encoded string.
///
/// # Example
///
/// ```shape
/// crypto.hex_encode("hello")  // "68656c6c6f"
/// ```
pub builtin fn hex_encode(data: string) -> string;

/// Decode a hexadecimal string.
///
/// # Arguments
///
/// * `hex` - Hex-encoded string to decode
///
/// # Returns
///
/// `Ok(decoded)` with the decoded UTF-8 string, or `Err(message)` on failure.
///
/// # Example
///
/// ```shape
/// crypto.hex_decode("68656c6c6f")  // Ok("hello")
/// ```
pub builtin fn hex_decode(hex: string) -> Result<string, string>;

/// Generate n random bytes, returned as a hex-encoded string.
///
/// # Arguments
///
/// * `n` - Number of random bytes to generate (0..65536)
///
/// # Returns
///
/// Hex-encoded string of random bytes.
///
/// # Example
///
/// ```shape
/// let token = crypto.random_bytes(32)  // 64 hex characters
/// ```
pub builtin fn random_bytes(n: int) -> string;

/// Generate an Ed25519 keypair.
///
/// # Returns
///
/// An object with hex-encoded `public_key` (64 chars) and `secret_key` (64 chars).
///
/// # Example
///
/// ```shape
/// let keypair = crypto.ed25519_generate_keypair()
/// print(keypair["public_key"])
/// ```
pub builtin fn ed25519_generate_keypair() -> _;

/// Sign a message with an Ed25519 secret key.
///
/// # Arguments
///
/// * `message` - Message to sign
/// * `secret_key` - Hex-encoded 32-byte Ed25519 secret key
///
/// # Returns
///
/// Hex-encoded 128-character Ed25519 signature.
///
/// # Example
///
/// ```shape
/// let sig = crypto.ed25519_sign("hello", keypair["secret_key"])
/// ```
pub builtin fn ed25519_sign(message: string, secret_key: string) -> string;

/// Verify an Ed25519 signature against a message and public key.
///
/// # Arguments
///
/// * `message` - Message that was signed
/// * `signature` - Hex-encoded 64-byte Ed25519 signature
/// * `public_key` - Hex-encoded 32-byte Ed25519 public key
///
/// # Returns
///
/// `true` if the signature is valid, `false` otherwise.
///
/// # Example
///
/// ```shape
/// let valid = crypto.ed25519_verify("hello", sig, keypair["public_key"])
/// ```
pub builtin fn ed25519_verify(message: string, signature: string, public_key: string) -> bool;