• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // The following is a hexdump of a zip file containing the following
2 // ZipCrypto encrypted file:
3 // test.txt: 35 bytes, contents: `abcdefghijklmnopqrstuvwxyz123456789`, password: `test`
4 //
5 // 00000000  50 4b 03 04 14 00 01 00  00 00 54 bd b5 50 2f 20  |PK........T..P/ |
6 // 00000010  79 55 2f 00 00 00 23 00  00 00 08 00 00 00 74 65  |yU/...#.......te|
7 // 00000020  73 74 2e 74 78 74 ca 2d  1d 27 19 19 63 43 77 9a  |st.txt.-.'..cCw.|
8 // 00000030  71 76 c9 ec d1 6f d9 f5  22 67 b3 8f 52 b5 41 bc  |qv...o.."g..R.A.|
9 // 00000040  5c 36 f2 1d 84 c3 c0 28  3b fd e1 70 c2 cc 0c 11  |\6.....(;..p....|
10 // 00000050  0c c5 95 2f a4 50 4b 01  02 3f 00 14 00 01 00 00  |.../.PK..?......|
11 // 00000060  00 54 bd b5 50 2f 20 79  55 2f 00 00 00 23 00 00  |.T..P/ yU/...#..|
12 // 00000070  00 08 00 24 00 00 00 00  00 00 00 20 00 00 00 00  |...$....... ....|
13 // 00000080  00 00 00 74 65 73 74 2e  74 78 74 0a 00 20 00 00  |...test.txt.. ..|
14 // 00000090  00 00 00 01 00 18 00 31  b2 3b bf b8 2f d6 01 31  |.......1.;../..1|
15 // 000000a0  b2 3b bf b8 2f d6 01 a8  c4 45 bd b8 2f d6 01 50  |.;../....E../..P|
16 // 000000b0  4b 05 06 00 00 00 00 01  00 01 00 5a 00 00 00 55  |K..........Z...U|
17 // 000000c0  00 00 00 00 00                                    |.....|
18 // 000000c5
19 
20 use std::io::Cursor;
21 use std::io::Read;
22 
23 #[test]
encrypted_file()24 fn encrypted_file() {
25     let zip_file_bytes = &mut Cursor::new(vec![
26         0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0xbd, 0xb5, 0x50, 0x2f,
27         0x20, 0x79, 0x55, 0x2f, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00,
28         0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78, 0x74, 0xca, 0x2d, 0x1d, 0x27, 0x19, 0x19, 0x63,
29         0x43, 0x77, 0x9a, 0x71, 0x76, 0xc9, 0xec, 0xd1, 0x6f, 0xd9, 0xf5, 0x22, 0x67, 0xb3, 0x8f,
30         0x52, 0xb5, 0x41, 0xbc, 0x5c, 0x36, 0xf2, 0x1d, 0x84, 0xc3, 0xc0, 0x28, 0x3b, 0xfd, 0xe1,
31         0x70, 0xc2, 0xcc, 0x0c, 0x11, 0x0c, 0xc5, 0x95, 0x2f, 0xa4, 0x50, 0x4b, 0x01, 0x02, 0x3f,
32         0x00, 0x14, 0x00, 0x01, 0x00, 0x00, 0x00, 0x54, 0xbd, 0xb5, 0x50, 0x2f, 0x20, 0x79, 0x55,
33         0x2f, 0x00, 0x00, 0x00, 0x23, 0x00, 0x00, 0x00, 0x08, 0x00, 0x24, 0x00, 0x00, 0x00, 0x00,
34         0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x74, 0x65, 0x73, 0x74,
35         0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18,
36         0x00, 0x31, 0xb2, 0x3b, 0xbf, 0xb8, 0x2f, 0xd6, 0x01, 0x31, 0xb2, 0x3b, 0xbf, 0xb8, 0x2f,
37         0xd6, 0x01, 0xa8, 0xc4, 0x45, 0xbd, 0xb8, 0x2f, 0xd6, 0x01, 0x50, 0x4b, 0x05, 0x06, 0x00,
38         0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x5a, 0x00, 0x00, 0x00, 0x55, 0x00, 0x00, 0x00,
39         0x00, 0x00,
40     ]);
41 
42     let mut archive = zip::ZipArchive::new(zip_file_bytes).unwrap();
43 
44     assert_eq!(archive.len(), 1); //Only one file inside archive: `test.txt`
45 
46     {
47         // No password
48         let file = archive.by_index(0);
49         match file {
50             Err(zip::result::ZipError::UnsupportedArchive("Password required to decrypt file")) => {
51                 ()
52             }
53             Err(_) => panic!(
54                 "Expected PasswordRequired error when opening encrypted file without password"
55             ),
56             Ok(_) => panic!("Error: Successfully opened encrypted file without password?!"),
57         }
58     }
59 
60     {
61         // Wrong password
62         let file = archive.by_index_decrypt(0, b"wrong password");
63         match file {
64             Ok(Err(zip::result::InvalidPassword)) => (),
65             Err(_) => panic!(
66                 "Expected InvalidPassword error when opening encrypted file with wrong password"
67             ),
68             Ok(Ok(_)) => panic!("Error: Successfully opened encrypted file with wrong password?!"),
69         }
70     }
71 
72     {
73         // Correct password, read contents
74         let mut file = archive
75             .by_index_decrypt(0, "test".as_bytes())
76             .unwrap()
77             .unwrap();
78         let file_name = file.enclosed_name().unwrap();
79         assert_eq!(file_name, std::path::PathBuf::from("test.txt"));
80 
81         let mut data = Vec::new();
82         file.read_to_end(&mut data).unwrap();
83         assert_eq!(data, "abcdefghijklmnopqrstuvwxyz123456789".as_bytes());
84     }
85 }
86