mirror of
https://github.com/jixunmoe/tc_tea_rust
synced 2026-03-07 20:19:49 +00:00
fix: off by one bit
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
[package]
|
||||
name = "tc_tea"
|
||||
version = "0.1.1"
|
||||
version = "0.1.2"
|
||||
authors = ["Jixun Wu <jixun.moe@gmail.com>"]
|
||||
edition = "2021"
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ Code implemented according to the spec described in
|
||||
Add the following to `[dependencies]` section in your `Cargo.toml` file:
|
||||
|
||||
```toml
|
||||
tc_tea = "0.1.1"
|
||||
tc_tea = "0.1.2"
|
||||
```
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
@@ -60,7 +60,7 @@ pub fn encrypt<T: AsRef<[u8]>, K: AsRef<[u8]>>(plaintext: T, key: K) -> Option<B
|
||||
.unwrap()
|
||||
.fill_bytes(&mut encrypted[0..header_len]);
|
||||
|
||||
encrypted[0] = (encrypted[0] & 0b1111_1100) | ((pad_len as u8) & 0b0000_0111);
|
||||
encrypted[0] = (encrypted[0] & 0b1111_1000) | ((pad_len as u8) & 0b0000_0111);
|
||||
|
||||
// Copy input to destination buffer.
|
||||
encrypted[header_len..header_len + plaintext.len()]
|
||||
@@ -169,6 +169,22 @@ mod tests {
|
||||
assert_eq!(decrypted, GOOD_DECRYPTED_DATA.into());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tc_tea_test_long_encryption() {
|
||||
let input = b"...test data by Jixun";
|
||||
for i in 0..255 {
|
||||
let encrypted = encrypt(input, ENCRYPTION_KEY).unwrap();
|
||||
assert_eq!(encrypted.len() % 8, 0);
|
||||
assert!(encrypted.len() > input.len());
|
||||
eprintln!(":2 encrypted.len(): {}", encrypted.len());
|
||||
|
||||
// Since encryption utilises random numbers, we are just going to
|
||||
let decrypted = decrypt(encrypted, ENCRYPTION_KEY).unwrap();
|
||||
assert_eq!(&*decrypted, input);
|
||||
eprintln!("run {} ok", i)
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_calc_encrypted_size() {
|
||||
assert_eq!(calc_encrypted_size(0), 16);
|
||||
|
||||
Reference in New Issue
Block a user