Skip to content

Commit b861dd2

Browse files
committed
Make ZipArchiveWriter::usingPassword() accept util.Secret instances
1 parent 88a84a4 commit b861dd2

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

src/main/php/io/archive/zip/ZipArchiveWriter.class.php

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use io\streams\OutputStream;
44
use lang\{Closeable, IllegalArgumentException};
5-
use util\Date;
5+
use util\{Date, Secret};
66

77
/**
88
* Writes to a ZIP archive
@@ -52,15 +52,19 @@ public function usingUnicodeNames($unicode= true) {
5252
/**
5353
* Set password to use when adding entries
5454
*
55-
* @param string password
55+
* @param string|util.Secret $password
5656
* @return io.archive.zip.ZipArchiveWriter this
5757
*/
5858
public function usingPassword($password) {
5959
if (null === $password) {
6060
$this->password= null;
6161
} else {
6262
$this->password= new ZipCipher();
63-
$this->password->initialize(iconv(\xp::ENCODING, 'cp437', $password));
63+
$this->password->initialize(iconv(
64+
\xp::ENCODING,
65+
'cp437',
66+
$password instanceof Secret ? $password->reveal() : $password)
67+
);
6468
}
6569
return $this;
6670
}

src/test/php/io/archive/zip/unittest/ZipArchiveWriterTest.class.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@
33
use io\archive\zip\{ZipArchiveWriter, ZipDirEntry, ZipFile, ZipFileEntry};
44
use io\streams\{MemoryInputStream, MemoryOutputStream, StreamTransfer};
55
use lang\IllegalArgumentException;
6-
use test\{Assert, Expect, Test};
6+
use test\{Assert, Expect, Test, Values};
7+
use util\Secret;
78

89
class ZipArchiveWriterTest extends AbstractZipFileTest {
910

@@ -83,11 +84,11 @@ public function adding_files_and_dir() {
8384
);
8485
}
8586

86-
#[Test]
87-
public function using_password_protection() {
87+
#[Test, Values(['secret', new Secret('secret')])]
88+
public function using_password_protection($password) {
8889
$out= new MemoryOutputStream();
8990

90-
$fixture= ZipFile::create($out)->usingPassword('secret');
91+
$fixture= ZipFile::create($out)->usingPassword($password);
9192
$fixture->addFile(new ZipFileEntry('test.txt'))->out()->write('File contents');
9293
$fixture->close();
9394

0 commit comments

Comments
 (0)