use std::fmt;
use generic_array::{ArrayLength, GenericArray};
#[cfg(feature = "v3")]
use rusty_paseto::core::V3;
#[cfg(feature = "v4")]
use rusty_paseto::core::V4;
pub trait Version {
type Local: ArrayLength<u8>;
type Public: ArrayLength<u8>;
type Secret: ArrayLength<u8>;
const TOKEN_HEADER: &'static str;
const KEY_HEADER: &'static str;
}
#[cfg(feature = "v3")]
impl Version for V3 {
type Local = generic_array::typenum::U32;
type Public = generic_array::typenum::U49;
type Secret = generic_array::typenum::U48;
const TOKEN_HEADER: &'static str = "v3.";
const KEY_HEADER: &'static str = "k3.";
}
#[cfg(feature = "v4")]
impl Version for V4 {
type Local = generic_array::typenum::U32;
type Public = generic_array::typenum::U32;
type Secret = generic_array::typenum::U64;
const TOKEN_HEADER: &'static str = "v4.";
const KEY_HEADER: &'static str = "k4.";
}
#[derive(Debug)]
pub struct Public;
#[derive(Debug)]
pub struct Secret;
#[derive(Debug)]
pub struct Local;
pub trait KeyType<V: Version> {
type KeyLen: ArrayLength<u8>;
const HEADER: &'static str;
const ID: &'static str;
}
impl<V: Version> KeyType<V> for Public {
type KeyLen = V::Public;
const HEADER: &'static str = "public.";
const ID: &'static str = "pid.";
}
impl<V: Version> KeyType<V> for Secret {
type KeyLen = V::Secret;
const HEADER: &'static str = "secret.";
const ID: &'static str = "sid.";
}
impl<V: Version> KeyType<V> for Local {
type KeyLen = V::Local;
const HEADER: &'static str = "local.";
const ID: &'static str = "lid.";
}
pub struct Key<V: Version, K: KeyType<V>> {
pub(crate) key: GenericArray<u8, K::KeyLen>,
}
impl<V: Version, K: KeyType<V>> core::cmp::PartialEq for Key<V, K> {
fn eq(&self, other: &Self) -> bool {
self.key.eq(&other.key)
}
}
impl<V: Version, K: KeyType<V>> core::cmp::Eq for Key<V, K> {}
impl<V: Version, K: KeyType<V>> Clone for Key<V, K> {
fn clone(&self) -> Self {
Self {
key: self.key.clone(),
}
}
}
impl<V: Version, K: KeyType<V>> Copy for Key<V, K> where GenericArray<u8, K::KeyLen>: Copy {}
impl<V: Version, K: KeyType<V>> fmt::Debug for Key<V, K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let mut f = f.debug_struct("Key");
if cfg!(fuzzing_repro) {
f.field("key", &self.key).finish()
} else {
f.finish_non_exhaustive()
}
}
}
mod convert;
#[cfg(feature = "arbitrary")]
mod arbitrary;
pub mod plaintext;