fix: off by one bit

This commit is contained in:
Jixun Wu
2021-12-29 00:20:00 +00:00
parent ab6bca0ba6
commit 76ae128d68
3 changed files with 19 additions and 3 deletions

View File

@@ -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"

View File

@@ -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

View File

@@ -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);