aboutsummaryrefslogtreecommitdiff
path: root/src/zip/encryption.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/zip/encryption.rs')
-rw-r--r--src/zip/encryption.rs89
1 files changed, 0 insertions, 89 deletions
diff --git a/src/zip/encryption.rs b/src/zip/encryption.rs
deleted file mode 100644
index f317245..0000000
--- a/src/zip/encryption.rs
+++ /dev/null
@@ -1,89 +0,0 @@
1use crate::utils::ReadUtils;
2use crate::zip::{ZipError, ZipResult};
3use std::io::{Read, Result as IoResult};
4
5const TABLE: [u32; 256] = generate_table();
6
7const fn generate_table() -> [u32; 256] {
8 let mut table = [0; 256];
9
10 let mut i = 0;
11 while i <= 255 {
12 let mut t = i as u32;
13
14 let mut j = 0;
15 while j < 8 {
16 if (t & 1) > 0 {
17 t = (t >> 1) ^ 0xEDB88320
18 } else {
19 t >>= 1
20 }
21 j += 1;
22 }
23
24 table[i] = t;
25 i += 1
26 }
27
28 table
29}
30
31fn crc32(byte: u8, crc: u32) -> u32 {
32 (crc >> 8) ^ TABLE[((crc & 0xFF) as u8 ^ byte) as usize]
33}
34
35pub struct WeakDecoder<Io: Read> {
36 key0: u32,
37 key1: u32,
38 key2: u32,
39 io: Io,
40}
41
42impl<Io: Read> WeakDecoder<Io> {
43 pub fn new(io: Io, check: u8, password: &[u8]) -> ZipResult<Self> {
44 let mut decoder = Self {
45 key0: 305419896,
46 key1: 591751049,
47 key2: 878082192,
48 io,
49 };
50
51 for c in password {
52 decoder.update_keys(*c)
53 }
54
55 let buf = decoder.read_arr::<12>()?;
56 if check != buf[11] {
57 return Err(ZipError::IncorrectPassword.into());
58 }
59
60 Ok(decoder)
61 }
62
63 fn update_keys(&mut self, byte: u8) {
64 self.key0 = crc32(byte, self.key0);
65 self.key1 = self
66 .key1
67 .wrapping_add(self.key0 & 0xFF)
68 .wrapping_mul(134775813)
69 .wrapping_add(1);
70 self.key2 = crc32((self.key1 >> 24) as u8, self.key2);
71 }
72
73 fn decode_byte(&mut self, byte: u8) -> u8 {
74 let key = self.key2 | 2;
75 let byte = byte ^ ((key.wrapping_mul(key ^ 1)) >> 8) as u8;
76 self.update_keys(byte);
77 byte
78 }
79}
80
81impl<Io: Read> Read for WeakDecoder<Io> {
82 fn read(&mut self, buf: &mut [u8]) -> IoResult<usize> {
83 let bytes = self.io.read(buf)?;
84 for i in 0..bytes {
85 buf[i] = self.decode_byte(buf[i]);
86 }
87 Ok(bytes)
88 }
89}