pub fn decode(data: &str, key: &[u8], ttl: u32) -> Result<Vec<u8>, BrancaError> {
    let sk: SecretKey = match SecretKey::from_slice(key) {
        Ok(key) => key,
        Err(UnknownCryptoError) => return Err(BrancaError::BadKeyLength),
    };

    if data.len() < 61 {
        return Err(BrancaError::InvalidBase62Token);
    }

    let decoded_data = match b62_decode(BASE62, data) {
        Ok(decoded) => decoded,
        Err(_) => return Err(BrancaError::InvalidBase62Token),
    };

    // After we have decoded the data, the branca token is now represented
    // by the following layout below:

    // Branca( header[0..29] + ciphertext[29..] )
    // Version (&u8) || Timestamp (u32) || Nonce ([u8;24]) || Ciphertext (&[u8]) || Tag ([u8:16])

    // We then obtain the header, ciphertext, version and timestamp with this layout.

    // Check if there is a version mismatch.
    if decoded_data[0] != VERSION {
        return Err(BrancaError::InvalidTokenVersion);
    }

    let header = &decoded_data[0..29];
    let n: Nonce = Nonce::from_slice(decoded_data[5..29].as_ref()).unwrap();
    let mut buf_crypt = vec![0u8; decoded_data.len() - 16 - 29];

    match open(
        &sk,
        &n,
        decoded_data[29..].as_ref(),
        Some(header),
        &mut buf_crypt,
    ) {
        Ok(()) => (),
        Err(orion::errors::UnknownCryptoError) => return Err(BrancaError::DecryptFailed),
    };

    let timestamp: u32 = BigEndian::read_u32(&decoded_data[1..5]);

    // TTL check to determine if the token has expired.
    if ttl != 0 {
        let future = match timestamp.checked_add(ttl) {
            Some(value) => value as u64,
            None => return Err(BrancaError::OverflowingOperation),
        };

        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Failed to obtain timestamp from system clock.")
            .as_secs();
        if future < now {
            return Err(BrancaError::ExpiredToken);
        }
    }

    // Return the plaintext.
    Ok(buf_crypt)
}
/// Decodes a Branca token and returns the plaintext as a String.
///
/// `data` - The input which is to be decrypted with the user-supplied key.
///
/// `key` - The user-supplied key to use for decryption.
///
/// `ttl` - The time-to-live upon creation of the token with its timestamp in seconds.
///
/// If the supplied ttl is set to 0, then the expiration check is omitted. It is recommended to
/// set this if you want to check the timestamp of an incoming token generated by the client.
///
/// Note:
///
/// * The key must be 32 bytes in length, otherwise it returns a `BrancaError::BadKeyLength` Result.
///
/// * If the decryption fails, it returns a `BrancaError::DecryptFailed` Result.
///
/// * If the TTL is non-zero and the timestamp of the token is in the past, it is considered to be expired; returning a `BrancaError::ExpiredToken` Result.
///
/// * If the input is not in Base62 format, it returns a `BrancaError::InvalidBase62Token` Result.
///
/// * If adding TTL to the token timestamp results in an overflow, it returns a `BrancaError::OverflowingOperation` Result.
///
/// * This panics if the current system time cannot be obtained.
Decodes a Branca token and returns the plaintext as a String.
`data` - The input which is to be decrypted with the user-supplied key.

`key` - The user-supplied key to use for decryption.

`ttl` - The time-to-live upon creation of the token with its timestamp in seconds.

If the supplied ttl is set to 0, then the expiration check is omitted. It is recommended to
set this if you want to check the timestamp of an incoming token generated by the client.



The key must be 32 bytes in length, otherwise it returns a `BrancaError::BadKeyLength` Result.

If the decryption fails, it returns a `BrancaError::DecryptFailed` Result.

If the TTL is non-zero and the timestamp of the token is in the past, it is considered to be expired; returning a `BrancaError::ExpiredToken` Result.

If the input is not in Base62 format, it returns a `BrancaError::InvalidBase62Token` Result.

If adding TTL to the token timestamp results in an overflow, it returns a `BrancaError::OverflowingOperation` Result.

This panics if the current system time cannot be obtained.
Decodes a Branca token and returns the plaintext as a String . ` data ` - The input which is to be decrypted with the user - supplied key . ` key ` - The user - supplied key to use for decryption . ` ttl ` - The time - to - live upon creation of the token with its timestamp in seconds . If the supplied ttl is set to 0 then the expiration check is omitted . It is recommended to set this if you want to check the timestamp of an incoming token generated by the client . The key must be 32 bytes in length otherwise it returns a ` BrancaError :: BadKeyLength ` Result . If the decryption fails it returns a ` BrancaError :: DecryptFailed ` Result . If the TTL is non - zero and the timestamp of the token is in the past it is considered to be expired ; returning a ` BrancaError :: ExpiredToken ` Result . If the input is not in Base62 format it returns a ` BrancaError :: InvalidBase62Token ` Result . If adding TTL to the token timestamp results in an overflow it returns a ` BrancaError :: OverflowingOperation ` Result . This panics if the current system time cannot be obtained .
pub fn decode(data: &str, key: &[u8], ttl: u32) -> Result<Vec<u8>, BrancaError> {
    let sk: SecretKey = match SecretKey::from_slice(key) {
        Ok(key) => key,
        Err(UnknownCryptoError) => return Err(BrancaError::BadKeyLength),
    };
    if data.len() < 61 {
        return Err(BrancaError::InvalidBase62Token);
    }
    let decoded_data = match b62_decode(BASE62, data) {
        Ok(decoded) => decoded,
        Err(_) => return Err(BrancaError::InvalidBase62Token),
    };
    // After we have decoded the data, the branca token is now represented
    // by the following layout below:
    // Branca( header[0..29] + ciphertext[29..] )
    // Version (&u8) || Timestamp (u32) || Nonce ([u8;24]) || Ciphertext (&[u8]) || Tag ([u8:16])
    // We then obtain the header, ciphertext, version and timestamp with this layout.
    // Check if there is a version mismatch.
    if decoded_data[0] != VERSION {
        return Err(BrancaError::InvalidTokenVersion);
    }
    let header = &decoded_data[0..29];
    let n: Nonce = Nonce::from_slice(decoded_data[5..29].as_ref()).unwrap();
    let mut buf_crypt = vec![0u8; decoded_data.len() - 16 - 29];
    match open(
        &sk,
        &n,
        decoded_data[29..].as_ref(),
        Some(header),
        &mut buf_crypt,
    ) {
        Ok(()) => (),
        Err(orion::errors::UnknownCryptoError) => return Err(BrancaError::DecryptFailed),
    };
    let timestamp: u32 = BigEndian::read_u32(&decoded_data[1..5]);
    // TTL check to determine if the token has expired.
    if ttl != 0 {
        let future = match timestamp.checked_add(ttl) {
            Some(value) => value as u64,
            None => return Err(BrancaError::OverflowingOperation),
        };
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Failed to obtain timestamp from system clock.")
            .as_secs();
        if future < now {
            return Err(BrancaError::ExpiredToken);
        }
    }
    // Return the plaintext.
    Ok(buf_crypt)
}
pub fn decode ( data : & str , key : & [ u8 ] , ttl : u32 ) -> Result < Vec < u8 > , BrancaError > { let sk : SecretKey = match SecretKey :: from_slice ( key ) { Ok ( key ) => key , Err ( UnknownCryptoError ) => return Err ( BrancaError :: BadKeyLength ) , } ; if data . len ( ) < 61 { return Err ( BrancaError :: InvalidBase62Token ) ; } let decoded_data = match b62_decode ( BASE62 , data ) { Ok ( decoded ) => decoded , Err ( _ ) => return Err ( BrancaError :: InvalidBase62Token ) , } ; // After we have decoded the data, the branca token is now represented // by the following layout below: // Branca( header[0..29] + ciphertext[29..] ) // Version (&u8) || Timestamp (u32) || Nonce ([u8;24]) || Ciphertext (&[u8]) || Tag ([u8:16]) // We then obtain the header, ciphertext, version and timestamp with this layout. // Check if there is a version mismatch. if decoded_data [ 0 ] != VERSION { return Err ( BrancaError :: InvalidTokenVersion ) ; } let header = & decoded_data [ 0 .. 29 ] ; let n : Nonce = Nonce :: from_slice ( decoded_data [ 5 .. 29 ] . as_ref ( ) ) . unwrap ( ) ; let mut buf_crypt = vec ! [ 0u8 decoded_data len ( ) 16 29 ] ; match open ( & sk , & n , decoded_data [ 29 .. ] . as_ref ( ) , Some ( header ) , & mut buf_crypt , ) { Ok ( ( ) ) => ( ) , Err ( orion :: errors :: UnknownCryptoError ) => return Err ( BrancaError :: DecryptFailed ) , } ; let timestamp : u32 = BigEndian :: read_u32 ( & decoded_data [ 1 .. 5 ] ) ; // TTL check to determine if the token has expired. if ttl != 0 { let future = match timestamp . checked_add ( ttl ) { Some ( value ) => value as u64 , None => return Err ( BrancaError :: OverflowingOperation ) , } ; let now = SystemTime :: now ( ) . duration_since ( UNIX_EPOCH ) . expect ( " " ) . as_secs ( ) ; if future < now { return Err ( BrancaError :: ExpiredToken ) ; } } // Return the plaintext. Ok ( buf_crypt ) }
Decodes a Branca token and returns the plaintext as a String.
Decodes a Branca token and returns the plaintext as a String .
data
&str
key
&[u8]
ttl
u32