Crate vach[−][src]
Expand description
A simple archiving format, designed for storing assets in compact secure containers
vach, pronounced like “puck” but with a “v”, is a archiving and resource transmission format. It was built to be secure, contained and protected. It was, in fact, designed by the SCP to keep your anomalous assets compact and secure during transmission. vach also has in-built support for compression, data signing, leaf bitflags, encryption and archive customization and archive customization. Check out the vach specification at spec.txt. Any and all help will be much appreciated, especially proof reading the docs and code review.
👄 Terminologies
- Archive: Any source of data, for example a file or TCP stream, that is a valid
vachdata source. - Leaf: Any actual data endpoint within an archive, what
tarcalls archive members, for examplefootstep1.wavinsounds.vach. - Entry: Some data in the registry section of a
vachsource on an correspondingleaf. For example,{ id: footstep.wav, location: 45, offset: 2345, flags: 0b0000_0000_0000_0000u16 }.
🀄 Show me some code dang it!
> Building a basic unsigned .vach file
use std::{io::Cursor, fs::File};
use vach::prelude::{Builder, BuilderConfig};
let config = BuilderConfig::default();
let mut builder = Builder::default();
// Use `Builder::add( reader, ID )` to add data to the write queue
// builder.add(File::open("test_data/background.wav")?, "ambient").unwrap();
// builder.add(File::open("test_data/footstep.wav")?, "ftstep").unwrap();
builder.add(Cursor::new(b"Hello, Cassandra!"), "hello").unwrap();
// let mut target = File::create("sounds.vach")?;
let mut target = Cursor::new(Vec::new());
// The number of bytes written to the file
let size = builder.dump(&mut target, &config).unwrap();> Loading resources from an unsigned .vach file
use std::fs::File;
use vach::prelude::{Archive, Resource, Flags};
let target = File::open("sounds.vach")?;
let mut archive = Archive::from_handle(target)?;
let resource: Resource = archive.fetch("ambient")?;
// By default all resources are flagged as NOT secured
println!("{}", Sound::new(&resource.data)?);
assert!(!resource.secured);
let mut buffer = Vec::new();
let (flags, content_version, is_secure) = archive.fetch_write("ftstep", &mut buffer)?;> Build a signed .vach file
use std::{io::Cursor, fs::File};
use vach::prelude::{Builder, BuilderConfig, Keypair};
use vach::utils::gen_keypair;
let keypair: Keypair = gen_keypair();
let config: BuilderConfig = BuilderConfig::default().keypair(keypair);
let mut builder = Builder::default();
// Use `Builder::add( reader, ID )` to add data to the write queue
// builder.add(File::open("test_data/background.wav")?, "ambient").unwrap();
// builder.add(File::open("test_data/footstep.wav")?, "ftstep").unwrap();
builder.add(Cursor::new(b"Hello, Cassandra!"), "hello").unwrap();
// let mut target = File::create("sounds.vach")?;
let mut target = Cursor::new(Vec::new());
builder.dump(&mut target, &config).unwrap();> Serialize and de-serialize a Keypair, SecretKey and PublicKey
As Keypair, SecretKey and PublicKey are reflected from ed25519_dalek, you could refer to their docs to read further about them.
use vach;
use vach::prelude::{Keypair, SecretKey, PublicKey};
use vach::utils::gen_keypair;
// Generate keys
let keypair : Keypair = gen_keypair();
let secret : SecretKey = keypair.secret;
let public : PublicKey = keypair.public;
// Serialize
let public_key_bytes : [u8; vach::PUBLIC_KEY_LENGTH] = public.to_bytes();
let secret_key_bytes : [u8; vach::SECRET_KEY_LENGTH] = secret.to_bytes();
// let keypair_bytes : [u8; vach::KEYPAIR_LENGTH] = keypair.to_bytes();
// Deserialize
let public_key : PublicKey = PublicKey::from_bytes(&public_key_bytes).unwrap();
let secret_key : SecretKey = SecretKey::from_bytes(&secret_key_bytes).unwrap();
// let keypair : Keypair = Keypair::from_bytes(&keypair_bytes).unwrap();> Load resources from a signed .vach source
// Load public_key
let mut public_key = File::open(PUBLIC_KEY)?;
let mut public_key_bytes: [u8; crate::PUBLIC_KEY_LENGTH];
public_key.read_exact(&mut public_key_bytes)?;
// Build the Loader config
let mut config = HeaderConfig::default().key(PublicKey::from_bytes(&public_key_bytes)?);
let target = File::open("sounds.vach")?;
let mut archive = Archive::with_config(target, &config)?;
// Resources are marked as secure (=true) if the signatures match the data
let resource = archive.fetch("ambient")?;
println!("{}", Sound::new(&resource.data)?);
assert!(resource.secured);Re-exports
pub use rand;Modules
Loader-based logic and data-structures
Builder related data structures and logic
Import keypairs and signatures from here, mirrors from ed25519_dalek
Consolidated import for crate logic; This module stores all structs associated with this crate. Constants can be accesses directly with crate::<CONSTANT>
Some utility functions to keep you happy
Constants
The default MAGIC used by vach
Size of a keypair: (secret + public)
The standard size of any MAGIC entry in bytes
Maximum size for any ID
Size of a public key
Size of a secret key
Size of a signature
Current file spec version, both Loader and Builder