You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
44 lines
1.2 KiB
44 lines
1.2 KiB
//! Encodings
|
|
use super::*;
|
|
use ext::*;
|
|
use std::{fmt, error};
|
|
use bytes::{
|
|
Buf,
|
|
Bytes,
|
|
};
|
|
use std::io;
|
|
use tokio::io::{
|
|
AsyncRead, AsyncWrite,
|
|
AsyncReadExt, AsyncWriteExt,
|
|
};
|
|
use serde::{
|
|
Serialize,
|
|
de::DeserializeOwned
|
|
};
|
|
use cryptohelpers::{
|
|
sha256,
|
|
rsa,
|
|
};
|
|
|
|
/// Size of buffer to use when copying a stream.
|
|
pub const STREAMING_BUFFER_SIZE: usize = 4096;
|
|
|
|
pub mod ser;
|
|
|
|
/// Copy `from` to `to`, transforming the data with the provided key and IV.
|
|
///
|
|
/// # Stream cipher usage
|
|
/// The copy is buffered by `STREAMING_BUFFER_SIZE` bytes, and the cipher applied to each read buffer.
|
|
/// If the buffer cannot be filled (because the stream reached EOF before filling it), then only the full portion of the buffer is transformed and written.
|
|
#[inline] pub async fn cc20_copy_stream<F, T, K>(from: &mut F, to: &mut T, keys: K, decrypt: bool) -> io::Result<(usize, usize)>
|
|
where K: key::CC20Key,
|
|
F: AsyncRead + Unpin + ?Sized,
|
|
T: AsyncWrite + Unpin + ?Sized
|
|
{
|
|
if decrypt {
|
|
ser::cha_copy::<F, T, STREAMING_BUFFER_SIZE, true>(from, to, keys.key(), keys.iv()).await
|
|
} else {
|
|
ser::cha_copy::<F, T, STREAMING_BUFFER_SIZE, false>(from, to, keys.key(), keys.iv()).await
|
|
}
|
|
}
|