Compare commits
No commits in common. "02c01ffa2c174cdd560cbc3cc9793b777c58a1a3" and "5283830c3ce0e537ad8653e150c8bd2cab271068" have entirely different histories.
02c01ffa2c
...
5283830c3c
3 changed files with 93 additions and 242 deletions
72
Cargo.lock
generated
72
Cargo.lock
generated
|
@ -132,15 +132,15 @@ checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
|||
|
||||
[[package]]
|
||||
name = "bytes"
|
||||
version = "1.4.0"
|
||||
version = "1.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "89b2fd2a0dcf38d7971e2194b6b6eebab45ae01067456a7fd93d5547a61b70be"
|
||||
checksum = "dfb24e866b15a1af2a1b663f10c6b6b8f397a84aadb828f12e5b289ec23a3a3c"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.79"
|
||||
version = "1.0.78"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "50d30906286121d95be3d479533b458f87493b30a4b5f79a607db8f5d11aa91f"
|
||||
checksum = "a20104e2335ce8a659d6dd92a51a767a0c062599c73b343fd152cb401e828c3d"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
|
@ -241,9 +241,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "encoding_rs"
|
||||
version = "0.8.32"
|
||||
version = "0.8.31"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "071a31f4ee85403370b58aca746f01041ede6f0da2730960ad001edc2b71b394"
|
||||
checksum = "9852635589dc9f9ea1b6fe9f05b50ef208c85c834a562f0c6abb1c475736ec2b"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
]
|
||||
|
@ -289,36 +289,36 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "futures-channel"
|
||||
version = "0.3.26"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2e5317663a9089767a1ec00a487df42e0ca174b61b4483213ac24448e4664df5"
|
||||
checksum = "52ba265a92256105f45b719605a571ffe2d1f0fea3807304b522c1d778f79eed"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "futures-core"
|
||||
version = "0.3.26"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ec90ff4d0fe1f57d600049061dc6bb68ed03c7d2fbd697274c41805dcb3f8608"
|
||||
checksum = "04909a7a7e4633ae6c4a9ab280aeb86da1236243a77b694a49eacd659a4bd3ac"
|
||||
|
||||
[[package]]
|
||||
name = "futures-sink"
|
||||
version = "0.3.26"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f310820bb3e8cfd46c80db4d7fb8353e15dfff853a127158425f31e0be6c8364"
|
||||
checksum = "39c15cf1a4aa79df40f1bb462fb39676d0ad9e366c2a33b590d7c66f4f81fcf9"
|
||||
|
||||
[[package]]
|
||||
name = "futures-task"
|
||||
version = "0.3.26"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dcf79a1bf610b10f42aea489289c5a2c478a786509693b80cd39c44ccd936366"
|
||||
checksum = "2ffb393ac5d9a6eaa9d3fdf37ae2776656b706e200c8e16b1bdb227f5198e6ea"
|
||||
|
||||
[[package]]
|
||||
name = "futures-util"
|
||||
version = "0.3.26"
|
||||
version = "0.3.25"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "9c1d6de3acfef38d2be4b1f543f553131788603495be83da675e180c8d6b7bd1"
|
||||
checksum = "197676987abd2f9cadff84926f410af1c183608d36641465df73ae8211dc65d6"
|
||||
dependencies = [
|
||||
"futures-core",
|
||||
"futures-task",
|
||||
|
@ -514,9 +514,9 @@ checksum = "fad582f4b9e86b6caa621cabeb0963332d92eea04729ab12892c2533951e6440"
|
|||
|
||||
[[package]]
|
||||
name = "js-sys"
|
||||
version = "0.3.61"
|
||||
version = "0.3.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "445dde2150c55e483f3d8416706b97ec8e8237c307e5b7b4b8dd15e6af2a0730"
|
||||
checksum = "49409df3e3bf0856b916e2ceaca09ee28e6871cf7d9ce97a692cacfdb2a25a47"
|
||||
dependencies = [
|
||||
"wasm-bindgen",
|
||||
]
|
||||
|
@ -920,9 +920,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "security-framework"
|
||||
version = "2.8.2"
|
||||
version = "2.8.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a332be01508d814fed64bf28f798a146d73792121129962fdf335bb3c49a4254"
|
||||
checksum = "645926f31b250a2dca3c232496c2d898d91036e45ca0e97e0e2390c54e11be36"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"core-foundation",
|
||||
|
@ -1058,9 +1058,9 @@ checksum = "cda74da7e1a664f795bb1f8a87ec406fb89a02522cf6e50620d016add6dbbf5c"
|
|||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.25.0"
|
||||
version = "1.24.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c8e00990ebabbe4c14c08aca901caed183ecd5c09562a12c824bb53d3c3fd3af"
|
||||
checksum = "597a12a59981d9e3c38d216785b0c37399f6e415e8d0712047620f189371b0bb"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"bytes",
|
||||
|
@ -1206,9 +1206,9 @@ checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen"
|
||||
version = "0.2.84"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "31f8dcbc21f30d9b8f2ea926ecb58f6b91192c17e9d33594b3df58b2007ca53b"
|
||||
checksum = "eaf9f5aceeec8be17c128b2e93e031fb8a4d469bb9c4ae2d7dc1888b26887268"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"wasm-bindgen-macro",
|
||||
|
@ -1216,9 +1216,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-backend"
|
||||
version = "0.2.84"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "95ce90fd5bcc06af55a641a86428ee4229e44e07033963a2290a8e241607ccb9"
|
||||
checksum = "4c8ffb332579b0557b52d268b91feab8df3615f265d5270fec2a8c95b17c1142"
|
||||
dependencies = [
|
||||
"bumpalo",
|
||||
"log",
|
||||
|
@ -1231,9 +1231,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-futures"
|
||||
version = "0.4.34"
|
||||
version = "0.4.33"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f219e0d211ba40266969f6dbdd90636da12f75bee4fc9d6c23d1260dadb51454"
|
||||
checksum = "23639446165ca5a5de86ae1d8896b737ae80319560fbaa4c2887b7da6e7ebd7d"
|
||||
dependencies = [
|
||||
"cfg-if",
|
||||
"js-sys",
|
||||
|
@ -1243,9 +1243,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro"
|
||||
version = "0.2.84"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "4c21f77c0bedc37fd5dc21f897894a5ca01e7bb159884559461862ae90c0b4c5"
|
||||
checksum = "052be0f94026e6cbc75cdefc9bae13fd6052cdcaf532fa6c45e7ae33a1e6c810"
|
||||
dependencies = [
|
||||
"quote",
|
||||
"wasm-bindgen-macro-support",
|
||||
|
@ -1253,9 +1253,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-macro-support"
|
||||
version = "0.2.84"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "2aff81306fcac3c7515ad4e177f521b5c9a15f2b08f4e32d823066102f35a5f6"
|
||||
checksum = "07bc0c051dc5f23e307b13285f9d75df86bfdf816c5721e573dec1f9b8aa193c"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1266,15 +1266,15 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "wasm-bindgen-shared"
|
||||
version = "0.2.84"
|
||||
version = "0.2.83"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "0046fef7e28c3804e5e38bfa31ea2a0f73905319b677e57ebe37e49358989b5d"
|
||||
checksum = "1c38c045535d93ec4f0b4defec448e4291638ee608530863b1e2ba115d4fff7f"
|
||||
|
||||
[[package]]
|
||||
name = "web-sys"
|
||||
version = "0.3.61"
|
||||
version = "0.3.60"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e33b99f4b23ba3eec1a53ac264e35a755f00e966e0065077d6027c0f575b0b97"
|
||||
checksum = "bcda906d8be16e728fd5adc5b729afad4e444e106ab28cd1c7256e54fa61510f"
|
||||
dependencies = [
|
||||
"js-sys",
|
||||
"wasm-bindgen",
|
||||
|
|
|
@ -4,8 +4,7 @@ use std::{
|
|||
fs::File,
|
||||
hash::{Hash, Hasher},
|
||||
io::{BufReader, Read, Seek, SeekFrom, Write},
|
||||
iter::TakeWhile,
|
||||
path::{Iter, Path},
|
||||
path::Path,
|
||||
};
|
||||
|
||||
use aes_gcm::{aead::Aead, Aes256Gcm, KeyInit, Nonce};
|
||||
|
@ -13,7 +12,7 @@ use argon2::PasswordHasher;
|
|||
use password_hash::SaltString;
|
||||
use rand::{rngs::StdRng, RngCore, SeedableRng};
|
||||
|
||||
use super::{EncryptionStream, EncryptionType, HashType, RecordType, Store, StoreRecord};
|
||||
use super::{EncryptionType, HashType, RecordType, Store, StoreRecord};
|
||||
|
||||
pub struct LocalStore {
|
||||
path: String,
|
||||
|
@ -159,11 +158,11 @@ fn try_deserialize_superblock(
|
|||
.decrypt_block_from_file(header.enc_sup_block_size as usize, 0, file)
|
||||
.unwrap();
|
||||
|
||||
if data.data[0..enc.key.len()].ne(&enc.key) {
|
||||
if data[0..enc.key.len()].ne(&enc.key) {
|
||||
return Err("Key mismatch.");
|
||||
}
|
||||
|
||||
let meta = deserialize_store_meta(&&data.data[32..]);
|
||||
let meta = deserialize_store_meta(&data[31..]);
|
||||
|
||||
Ok(meta)
|
||||
}
|
||||
|
@ -253,15 +252,12 @@ fn try_create_store(path: &String, passphrase: &Vec<u8>) -> bool {
|
|||
|
||||
header.chksum_crc = header.get_checksum();
|
||||
|
||||
let mut index_block: Vec<u8> = vec![0; meta.data_block_size as usize];
|
||||
|
||||
let mut buf = serialize_header(&header);
|
||||
|
||||
buf.extend_from_slice(&salt_buf);
|
||||
buf.extend_from_slice(&iv);
|
||||
buf.append([b'\0'].repeat(16 - iv.len()).as_mut());
|
||||
buf.extend_from_slice(&enc_buf.data);
|
||||
buf.extend_from_slice(&enc.encrypt_block(&index_block, 0x90).unwrap().data);
|
||||
buf.extend_from_slice(&enc_buf);
|
||||
|
||||
match std::fs::File::create(path).unwrap().write(&buf) {
|
||||
Ok(x) => return x == buf.len(),
|
||||
|
@ -388,7 +384,7 @@ pub fn load(path: String, passphrase: Vec<u8>) -> Result<LocalStore, &'static st
|
|||
};
|
||||
|
||||
let store = LocalStore {
|
||||
path,
|
||||
path: path,
|
||||
header,
|
||||
enc_ctx,
|
||||
meta,
|
||||
|
@ -489,7 +485,7 @@ impl LocalRecord {
|
|||
key: key.to_owned(),
|
||||
r#type: RecordType::from(r#type),
|
||||
value: value.to_vec(),
|
||||
meta,
|
||||
meta: meta,
|
||||
},
|
||||
position: 0,
|
||||
size: val_offset + val_len + meta_len,
|
||||
|
@ -521,7 +517,6 @@ impl Store for LocalStore {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
||||
enum IndexNodeType {
|
||||
Internal,
|
||||
Leaf,
|
||||
|
@ -539,62 +534,12 @@ impl From<u8> for IndexNodeType {
|
|||
}
|
||||
}
|
||||
|
||||
struct IndexNodeEntry {
|
||||
key: u64,
|
||||
pointer: u64,
|
||||
}
|
||||
|
||||
struct IndexNode {
|
||||
node_type: IndexNodeType,
|
||||
children: Vec<IndexNodeEntry>,
|
||||
}
|
||||
|
||||
impl IndexNode {
|
||||
fn children(&self) -> &Vec<IndexNodeEntry> {
|
||||
&self.children
|
||||
}
|
||||
|
||||
fn len(&self) -> usize {
|
||||
self.children.len()
|
||||
}
|
||||
children: [u64],
|
||||
}
|
||||
|
||||
impl LocalStore {
|
||||
fn read_node(&self, data: &mut EncryptionStream) -> IndexNode {
|
||||
let node_type = {
|
||||
let mut buf: [u8; 2] = [0; 2];
|
||||
data.read_exact(&mut buf).expect("Cannot read node type.");
|
||||
IndexNodeType::from(buf[0])
|
||||
};
|
||||
|
||||
let mut children = Vec::with_capacity(self.meta.index_node_arity as usize);
|
||||
for _ in 0..self.meta.index_node_arity {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
|
||||
data.read_exact(&mut buf)
|
||||
.expect("Cannot read child node content.");
|
||||
|
||||
let key = u64::from_be_bytes(buf[0..8].try_into().unwrap());
|
||||
|
||||
if key == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
let pointer = u64::from_be_bytes(buf[0..8].try_into().unwrap());
|
||||
|
||||
let entry = IndexNodeEntry { key, pointer };
|
||||
|
||||
children.push(entry);
|
||||
}
|
||||
|
||||
let node = IndexNode {
|
||||
node_type,
|
||||
children,
|
||||
};
|
||||
|
||||
node
|
||||
}
|
||||
|
||||
fn btree_find_key(&self, key: &String) -> Option<u64> {
|
||||
let mut file: BufReader<File> =
|
||||
BufReader::new(File::open(&self.path).expect("Cannot open file"));
|
||||
|
@ -606,36 +551,33 @@ impl LocalStore {
|
|||
};
|
||||
|
||||
let pos = file.seek(SeekFrom::Start(0x90)).unwrap();
|
||||
let mut block = self
|
||||
.enc_ctx
|
||||
.decrypt_block_from_file(self.meta.data_block_size as usize, 0x90, &mut file)
|
||||
.unwrap();
|
||||
|
||||
let mut read_node_type = || {
|
||||
let mut buf: [u8; 2] = [0; 2];
|
||||
file.read_exact(&mut buf).expect("Cannot read node type.");
|
||||
IndexNodeType::from(buf[0])
|
||||
};
|
||||
|
||||
let mut read_key_pos = || {
|
||||
let mut buf: [u8; 16] = [0; 16];
|
||||
file.read_exact(&mut buf).expect("Read error");
|
||||
|
||||
(
|
||||
u64::from_be_bytes(buf[0..8].try_into().unwrap()),
|
||||
u64::from_be_bytes(buf[8..16].try_into().unwrap()),
|
||||
)
|
||||
};
|
||||
|
||||
let mut subnode_pos = 0;
|
||||
loop {
|
||||
let node = self.read_node(&mut block);
|
||||
let (key, offset) = read_key_pos();
|
||||
|
||||
if node.node_type == IndexNodeType::Invalid {
|
||||
panic!(
|
||||
"Invalid node detected: {}",
|
||||
block.stream_position().unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
if node.node_type == IndexNodeType::Leaf {
|
||||
for child in node.children() {
|
||||
if child.key == hash {
|
||||
return Some(child.pointer);
|
||||
}
|
||||
if hash > key {
|
||||
if subnode_pos > self.meta.index_node_arity {
|
||||
return None;
|
||||
}
|
||||
return None;
|
||||
}
|
||||
|
||||
for child in node.children {
|
||||
if child.key < hash {
|
||||
continue;
|
||||
}
|
||||
|
||||
block.seek(SeekFrom::Start(child.pointer)).unwrap();
|
||||
subnode_pos += 1;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -646,24 +588,19 @@ fn nonce_offset(nonce_root: &Vec<u8>, offset: usize) -> Vec<u8> {
|
|||
if len < (usize::BITS / 8) as usize {
|
||||
panic!("Nonce length less than {}", usize::BITS / 8);
|
||||
}
|
||||
let start = len - (usize::BITS / 8) as usize;
|
||||
|
||||
let mut nonce_copy = vec![0; len];
|
||||
nonce_copy.clone_from_slice(&nonce_root);
|
||||
|
||||
let mut num = u64::from_be_bytes(nonce_copy[start..len].try_into().unwrap());
|
||||
num += offset as u64;
|
||||
nonce_copy[start..len].copy_from_slice(&num.to_be_bytes());
|
||||
for i in 0..(usize::BITS / 8) {
|
||||
nonce_copy[len - (i as usize + 1)] += ((offset & (0xff << (8 * i))) >> (8 * i)) as u8;
|
||||
}
|
||||
|
||||
nonce_copy
|
||||
}
|
||||
|
||||
impl EncryptionContext {
|
||||
fn decrypt_block(
|
||||
&self,
|
||||
ciphertext: &Vec<u8>,
|
||||
offset: usize,
|
||||
) -> Result<EncryptionStream, String> {
|
||||
fn decrypt_block(&self, ciphertext: &Vec<u8>, offset: usize) -> Result<Vec<u8>, &'static str> {
|
||||
let plaintext = match self.enc_type {
|
||||
EncryptionType::Invalid => unreachable!(),
|
||||
EncryptionType::AesGcm => {
|
||||
|
@ -679,20 +616,13 @@ impl EncryptionContext {
|
|||
};
|
||||
|
||||
if plaintext.is_err() {
|
||||
return Err(format!("Decryption error: {}", plaintext.unwrap_err()));
|
||||
return Err("Decryption error");
|
||||
}
|
||||
|
||||
Ok(EncryptionStream {
|
||||
data: plaintext.unwrap(),
|
||||
pos: 0,
|
||||
})
|
||||
Ok(plaintext.unwrap())
|
||||
}
|
||||
|
||||
fn encrypt_block(
|
||||
&self,
|
||||
plaintext: &Vec<u8>,
|
||||
offset: usize,
|
||||
) -> Result<EncryptionStream, &'static str> {
|
||||
fn encrypt_block(&self, plaintext: &Vec<u8>, offset: usize) -> Result<Vec<u8>, &'static str> {
|
||||
let ciphertext = match self.enc_type {
|
||||
EncryptionType::Invalid => unreachable!(),
|
||||
EncryptionType::AesGcm => {
|
||||
|
@ -711,10 +641,7 @@ impl EncryptionContext {
|
|||
return Err("Encryption error");
|
||||
}
|
||||
|
||||
Ok(EncryptionStream {
|
||||
data: ciphertext.unwrap(),
|
||||
pos: 0,
|
||||
})
|
||||
Ok(ciphertext.unwrap())
|
||||
}
|
||||
|
||||
fn decrypt_block_from_file(
|
||||
|
@ -722,12 +649,13 @@ impl EncryptionContext {
|
|||
len: usize,
|
||||
offset: usize,
|
||||
file: &mut BufReader<File>,
|
||||
) -> Result<EncryptionStream, String> {
|
||||
) -> Result<Vec<u8>, &'static str> {
|
||||
let mut buf: Vec<u8> = vec![0; len + 16];
|
||||
match file.read_exact(&mut buf) {
|
||||
Err(err) => Err(format!("Read error: {}", err.to_string())),
|
||||
Ok(_) => self.decrypt_block(&buf, offset),
|
||||
if file.read_exact(&mut buf).is_err() {
|
||||
return Err("Read error.");
|
||||
}
|
||||
|
||||
self.decrypt_block(&buf, offset)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -752,42 +680,21 @@ mod tests {
|
|||
|
||||
#[test]
|
||||
fn nonce_offset_increment() {
|
||||
{
|
||||
let nonce: Vec<u8> = vec![0; 12];
|
||||
let nonce: Vec<u8> = vec![0; 12];
|
||||
|
||||
let nonce_off = nonce_offset(&nonce, 0xfeeddeadbeef);
|
||||
let nonce_off = nonce_offset(&nonce, 0xfeeddeadbeef);
|
||||
|
||||
assert!(
|
||||
nonce_off[11] == 0xef
|
||||
&& nonce_off[10] == 0xbe
|
||||
&& nonce_off[9] == 0xad
|
||||
&& nonce_off[8] == 0xde
|
||||
&& nonce_off[7] == 0xed
|
||||
&& nonce_off[6] == 0xfe,
|
||||
"\ninitial = {:x?}\nnew = {:x?}",
|
||||
nonce,
|
||||
nonce_off
|
||||
);
|
||||
}
|
||||
|
||||
//Carry test
|
||||
{
|
||||
let nonce: Vec<u8> = vec![0xfe; 12];
|
||||
|
||||
let nonce_off = nonce_offset(&nonce, 0x2);
|
||||
|
||||
assert!(
|
||||
nonce_off[11] == 0x00
|
||||
&& nonce_off[10] == 0xff
|
||||
&& nonce_off[9] == 0xfe
|
||||
&& nonce_off[8] == 0xfe
|
||||
&& nonce_off[7] == 0xfe
|
||||
&& nonce_off[6] == 0xfe,
|
||||
"\ninitial = {:x?}\nnew = {:x?}",
|
||||
nonce,
|
||||
nonce_off
|
||||
);
|
||||
}
|
||||
assert!(
|
||||
nonce_off[11] == 0xef
|
||||
&& nonce_off[10] == 0xbe
|
||||
&& nonce_off[9] == 0xad
|
||||
&& nonce_off[8] == 0xde
|
||||
&& nonce_off[7] == 0xed
|
||||
&& nonce_off[6] == 0xfe,
|
||||
"\nn1 = {:x?}\nn2 = {:x?}",
|
||||
nonce,
|
||||
nonce_off
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
|
|
@ -99,7 +99,6 @@ impl Read for EncryptionStream {
|
|||
let range = self.pos..(self.pos + buf.len()).clamp(0, self.data.len() - buf.len());
|
||||
let size = range.len();
|
||||
buf.copy_from_slice(&self.data[range]);
|
||||
self.pos += size;
|
||||
Ok(size)
|
||||
}
|
||||
}
|
||||
|
@ -147,58 +146,3 @@ impl Seek for EncryptionStream {
|
|||
Ok(cur as u64)
|
||||
}
|
||||
}
|
||||
|
||||
impl EncryptionStream {
|
||||
pub fn len(&self) -> usize {
|
||||
self.data.len()
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use std::io::{Read, Seek, SeekFrom};
|
||||
|
||||
use super::EncryptionStream;
|
||||
|
||||
fn make_enc_stream() -> EncryptionStream {
|
||||
EncryptionStream {
|
||||
data: vec![0; 128],
|
||||
pos: 0,
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encryption_stream_seek() {
|
||||
let mut stream = make_enc_stream();
|
||||
|
||||
{
|
||||
{
|
||||
let pos = stream.seek(SeekFrom::Start(64));
|
||||
assert!(pos.is_ok());
|
||||
assert_eq!(pos.unwrap(), 0);
|
||||
}
|
||||
|
||||
{
|
||||
let pos = stream.seek(SeekFrom::Start(128));
|
||||
assert!(pos.is_ok());
|
||||
assert_eq!(pos.unwrap(), 64);
|
||||
}
|
||||
|
||||
{
|
||||
let pos = stream.seek(SeekFrom::Start(256));
|
||||
assert!(pos.is_err());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn encryption_stream_read() {
|
||||
let mut stream = make_enc_stream();
|
||||
let mut buf: [u8; 8] = [1; 8];
|
||||
|
||||
assert!(stream.read_exact(&mut buf).is_ok());
|
||||
|
||||
assert_eq!(&buf, &[0; 8]);
|
||||
assert_eq!(stream.stream_position().unwrap(), 8);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue