OpenViSTAI · Agents · Blockchain · Crypto
Menu
OPENVIST RECIPES

Classify lowercase SHA-256 hexadecimal spellings with Rust

Rust

Check digest text shape without computing hashes or asserting provenance and integrity.

Rust code excerpt: Classify lowercase SHA-256 hexadecimal spellings with Rust. Version 0.1.0. Read the full recipe for code, sources and recorded test scope.

Check digest text shape without computing hashes or asserting provenance and integrity. Work only with supplied input; this does not contact external services or modify input.

Before you start

  • Save as recipe.rs and compile with Rust 1.99.0 using: rustc --edition=2021 recipe.rs -o recipe
  • Run recipe without arguments, supplying input through stdin. All arguments, including help flags, are rejected.
  • The reader consumes at most 65537 bytes and rejects input exceeding 65536 bytes. Input must be strict UTF-8, must not begin with a UTF-8 BOM, and must contain no CR characters.
  • Only LF splits lines. Exactly empty segments are discarded, including those caused by consecutive or trailing LF characters. No trimming or normalization occurs.
  • All retained lines are validated before classification: at most 100 nonempty lines, each at most 256 UTF-8 bytes. Duplicate lines remain separate and in input order.
  • Each retained line produces true exactly when its bytes are 64 ASCII characters from 0-9 and a-f. Other spellings produce false rather than rejecting the input, provided the input-level limits are satisfied.
  • Success exits 0 and writes exactly one JSON object followed by LF, with no stderr. Empty input or input containing only LF characters produces {"valid":[]}.
  • Invalid arguments, invalid input, or stdin-read failures exit 2 with no stdout and exactly error: invalid input followed by LF on stderr, subject to the allocation and output-failure limitations below.
  • Uses only the standard library, with explicit imports from std::io. The only environment and process calls are argument inspection through std::env::args_os and exit-status selection through std::process::exit.

Build and check

  1. Save the example as recipe.rs.
  2. Compile with Rust 1.99.0 using rustc --edition=2021 recipe.rs -o recipe.
  3. Prepare UTF-8 input.txt following the documented operation-specific conventions.
  4. Run ./recipe < input.txt.
  5. Inspect the JSON result. Exit 2 with exactly error: invalid input followed by a newline on stderr and no stdout identifies rejected input or stdin-read failure. Output-write failures may also exit nonzero, without that diagnostic and potentially after partial stdout; these are outside the input-error guarantee.

Limits to understand

  • Stdin may block until EOF or until the 65537-byte read bound is reached. There is no timeout.
  • Memory use is bounded by the input and line limits: a 65537-byte input buffer, at most 100 borrowed line references, a small output string, and standard-library buffering and allocator overhead. This is not an exact process-memory ceiling.
  • Successful output can be sensitive: it reveals the number and order of retained lines and which satisfy the spelling rule. Input values are not included in error diagnostics.
  • Memory-allocation failures and output-write failures are outside the input-error guarantee. A stdout write or flush failure exits 2 without an added diagnostic and may leave partial stdout. A stderr failure can prevent delivery of the required error message.
  • This checks textual shape only. It does not compute hashes, verify provenance or integrity, establish uniqueness, or assert provider compatibility or production suitability.
  • Text shape does not establish that a hash was computed correctly or that data is trustworthy.

Run it

rustc --edition=2021 recipe.rs -o recipe
./recipe < input.txt

Example input

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
0000000000000000000000000000000000000000000000000000000000000000
abc

Expected output

{"valid":[true,true,false]}
INSPECT THE SCRIPT

Inspect, copy and run the documented checks.

This browser preview builds code locally. It does not contact an API or run the code.

Default example ready.

Rust example
use std::io::{self, Read, Write};

fn prepare_output() -> Result<String, ()> {
    if std::env::args_os().skip(1).next().is_some() {
        return Err(());
    }

    let mut input = Vec::with_capacity(65_537);
    {
        let stdin = io::stdin();
        let mut reader = stdin.lock().take(65_537);
        reader.read_to_end(&mut input).map_err(|_| ())?;
    }
    if input.len() > 65_536 {
        return Err(());
    }

    let text = std::str::from_utf8(&input).map_err(|_| ())?;
    if text.starts_with('\u{feff}') || input.contains(&b'\r') {
        return Err(());
    }

    // Validate and retain all lines before classifying any spelling.
    let mut lines = Vec::with_capacity(100);
    for line in text.split('\n') {
        if line.is_empty() {
            continue;
        }
        if line.len() > 256 || lines.len() == 100 {
            return Err(());
        }
        lines.push(line);
    }

    let mut output = String::with_capacity(640);
    output.push_str("{\"valid\":[");
    for (index, line) in lines.iter().enumerate() {
        if index != 0 {
            output.push(',');
        }
        let valid = line.len() == 64
            && line.bytes().all(|b| matches!(b, b'0'..=b'9' | b'a'..=b'f'));
        output.push_str(if valid { "true" } else { "false" });
    }
    output.push_str("]}\n");
    Ok(output)
}

fn main() {
    match prepare_output() {
        Ok(output) => {
            let stdout = io::stdout();
            let mut writer = stdout.lock();
            if writer.write_all(output.as_bytes()).is_err() || writer.flush().is_err() {
                std::process::exit(2);
            }
        }
        Err(()) => {
            let stderr = io::stderr();
            let mut writer = stderr.lock();
            let _ = writer.write_all(b"error: invalid input\n");
            let _ = writer.flush();
            std::process::exit(2);
        }
    }
}
What to verify after running
  • The example must produce the documented JSON result.
  • Empty stdin must produce the documented empty result.
  • Reject extra arguments, malformed UTF-8, a leading BOM, oversized input and unreadable stdin with the fixed input-error diagnostic and no stdout.

Request or format preview

Illustrative preview. No provider response, price or balance is fetched.

Change a setting to inspect the preview.

Official sources and related profiles

    Code license: MIT. Keep the license notice with your copy.

    Example version 0.1.0 · Documentation reviewed 2026-10-14.

    Six independent CLI fixtures and 17 supplementary cases are executed in pinned, offline Rust 1.99.0 containers. These checks do not establish exhaustive correctness or behavior on other versions.

    Related browser tools

    Inspect related data locally in your browser. These tools have their own input rules and limits; they do not execute this recipe.