Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

8 Commits

Repository files navigation

Byte Sequences

This crate provides some utility to create byte sequences like ApiKeys or SessionIds. It contains a macro that lets you specify the name and length in bytes of the sequence. The macro than creates a struct that has following methods

fngenerate_new() -> Selffnto_string() -> Stringfncheck(key:&str) -> std::result::Result<Self, failure::Error>;

It also implements Display, Debug, serde::Serialize, serde::Deserialize, PartialOrd, Ord, PartialEq, Eq, Clone, Copy and Hash;

Complete example:

#[macro_use]externcrate byte_sequence;externcrate rand;use serde;use std;use byte_sequence::Checkable;byte_seq!(ApiKey;32);#[test]fnexample(){// Creates a new ApiKey containing 32 random bytes using a thread_rnglet key = ApiKey::generate_new();// The to_string method creates a hex encoded string:// i.e. 'BBC47F308F3D02C3C6C3D6C9555296A64407FE72AD92DE8C7344D610CFFABF67'assert_eq!(key.to_string().len(),64);// you can also do it the other way around: Parse a string into an ApiKeylet key = ApiKey::check("BBC47F308F3D02C3C6C3D6C9555296A64407FE72AD92DE8C7344D610CFFABF67").unwrap();assert_eq!(key.to_string(),"BBC47F308F3D02C3C6C3D6C9555296A64407FE72AD92DE8C7344D610CFFABF67");}

You can also extend the generated structs. For example add a public part of the ApiKey:

implApiKey{pubfnpublic_part(&self) -> PublicApiKey{letmut pub_data = [0u8;8];
pub_data.copy_from_slice(&self.0[0..8]);PublicApiKey(pub_data)}}

About

Utilities for things like ApiKeys or SessionIds

Resources

Stars

13 stars

Watchers

2 watching

Forks

Releases

Packages

Used by

Contributors

Languages