Skip to content

Commit ebd0cec

Browse files
committed
WIP
Signed-off-by: Nguyen Van Nguyen <[email protected]>
1 parent c476746 commit ebd0cec

File tree

3 files changed

+103
-18
lines changed

3 files changed

+103
-18
lines changed

src/Common/Armor.php

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88

99
namespace OpenPGP\Common;
1010

11-
use phpseclib3\Common\Functions\Strings;
1211
use OpenPGP\Enum\ArmorType;
12+
use phpseclib3\Common\Functions\Strings;
1313

1414
/**
1515
* Armor class
@@ -127,13 +127,10 @@ public function assert(ArmorType $type): self
127127
* Verify the checksum and return the encoded bytes
128128
*
129129
* @param string $armoredText
130-
* @param bool $checksumRequired
131130
* @return self
132131
*/
133-
public static function decode(
134-
string $armoredText,
135-
bool $checksumRequired = false
136-
): self {
132+
public static function decode(string $armoredText): self
133+
{
137134
$textDone = false;
138135
$checksum = "";
139136
$type = null;
@@ -182,7 +179,7 @@ public static function decode(
182179
$data = Strings::base64_decode(implode($dataLines));
183180
if (
184181
strcmp($checksum, self::crc24Checksum($data)) !== 0 &&
185-
(!empty($checksum) || $checksumRequired)
182+
(!empty($checksum) || Config::checksumRequired())
186183
) {
187184
throw new \RuntimeException("Ascii armor integrity check failed!");
188185
}
@@ -233,7 +230,9 @@ public static function encode(
233230
self::TRUNK_SIZE,
234231
Helper::EOL
235232
),
236-
"=" . self::crc24Checksum($data) . Helper::EOL,
233+
Config::checksumRequired()
234+
? "=" . self::crc24Checksum($data) . Helper::EOL
235+
: "",
237236
sprintf(
238237
self::MULTIPART_SECTION_MESSAGE_END,
239238
$partIndex,
@@ -248,7 +247,9 @@ public static function encode(
248247
self::TRUNK_SIZE,
249248
Helper::EOL
250249
),
251-
"=" . self::crc24Checksum($data) . Helper::EOL,
250+
Config::checksumRequired()
251+
? "=" . self::crc24Checksum($data) . Helper::EOL
252+
: "",
252253
sprintf(self::MULTIPART_LAST_MESSAGE_END, $partIndex),
253254
],
254255
ArmorType::SignedMessage => [
@@ -272,7 +273,9 @@ public static function encode(
272273
self::TRUNK_SIZE,
273274
Helper::EOL
274275
),
275-
"=" . self::crc24Checksum($data) . Helper::EOL,
276+
Config::checksumRequired()
277+
? "=" . self::crc24Checksum($data) . Helper::EOL
278+
: "",
276279
self::SIGNATURE_END,
277280
],
278281
ArmorType::Message => [
@@ -283,7 +286,9 @@ public static function encode(
283286
self::TRUNK_SIZE,
284287
Helper::EOL
285288
),
286-
"=" . self::crc24Checksum($data) . Helper::EOL,
289+
Config::checksumRequired()
290+
? "=" . self::crc24Checksum($data) . Helper::EOL
291+
: "",
287292
self::MESSAGE_END,
288293
],
289294
ArmorType::PublicKey => [
@@ -294,7 +299,9 @@ public static function encode(
294299
self::TRUNK_SIZE,
295300
Helper::EOL
296301
),
297-
"=" . self::crc24Checksum($data) . Helper::EOL,
302+
Config::checksumRequired()
303+
? "=" . self::crc24Checksum($data) . Helper::EOL
304+
: "",
298305
self::PUBLIC_KEY_BLOCK_END,
299306
],
300307
ArmorType::PrivateKey => [
@@ -305,7 +312,9 @@ public static function encode(
305312
self::TRUNK_SIZE,
306313
Helper::EOL
307314
),
308-
"=" . self::crc24Checksum($data) . Helper::EOL,
315+
Config::checksumRequired()
316+
? "=" . self::crc24Checksum($data) . Helper::EOL
317+
: "",
309318
self::PRIVATE_KEY_BLOCK_END,
310319
],
311320
ArmorType::Signature => [
@@ -316,7 +325,9 @@ public static function encode(
316325
self::TRUNK_SIZE,
317326
Helper::EOL
318327
),
319-
"=" . self::crc24Checksum($data) . Helper::EOL,
328+
Config::checksumRequired()
329+
? "=" . self::crc24Checksum($data) . Helper::EOL
330+
: "",
320331
self::SIGNATURE_END,
321332
],
322333
};
@@ -331,10 +342,13 @@ public static function encode(
331342
*/
332343
private static function addHeader(string $customComment = ""): string
333344
{
334-
$headers = [
335-
"Version: " . Config::VERSION . Helper::EOL,
336-
"Comment: " . Config::COMMENT . Helper::EOL,
337-
];
345+
$headers = [];
346+
if (Config::showVersion()) {
347+
$headers[] = "Version: " . Config::VERSION . Helper::EOL;
348+
}
349+
if (Config::showComment()) {
350+
$headers[] = "Comment: " . Config::COMMENT . Helper::EOL;
351+
}
338352
if (!empty($customComment)) {
339353
$headers[] = "Comment: " . $customComment . Helper::EOL;
340354
}

src/Common/Config.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,12 @@ final class Config
6363

6464
private static bool $allowUnauthenticated = false;
6565

66+
private static bool $showVersion = true;
67+
68+
private static bool $showComment = false;
69+
70+
private static bool $checksumRequired = false;
71+
6672
/**
6773
* Get preferred hash algorithm.
6874
*
@@ -316,4 +322,64 @@ public static function setAllowUnauthenticated(bool $allow): void
316322
{
317323
self::$allowUnauthenticated = $allow;
318324
}
325+
326+
/**
327+
* Whether to include version header in armored messages.
328+
*
329+
* @return bool
330+
*/
331+
public static function showVersion(): bool
332+
{
333+
return self::$showVersion;
334+
}
335+
336+
/**
337+
* Set show version.
338+
*
339+
* @param bool $showVersion
340+
*/
341+
public static function setShowVersion(bool $showVersion): void
342+
{
343+
self::$showVersion = $showVersion;
344+
}
345+
346+
/**
347+
* Whether to include comment header in armored messages.
348+
*
349+
* @return bool
350+
*/
351+
public static function showComment(): bool
352+
{
353+
return self::$showComment;
354+
}
355+
356+
/**
357+
* Set show comment.
358+
*
359+
* @param bool $showComment
360+
*/
361+
public static function setShowComment(bool $showComment): void
362+
{
363+
self::$showComment = $showComment;
364+
}
365+
366+
/**
367+
* Whether checksum required in armored messages.
368+
*
369+
* @return bool
370+
*/
371+
public static function checksumRequired(): bool
372+
{
373+
return self::$checksumRequired;
374+
}
375+
376+
/**
377+
* Set checksum required.
378+
*
379+
* @param bool $checksumRequired
380+
*/
381+
public static function setChecksumRequired(bool $checksumRequired): void
382+
{
383+
self::$checksumRequired = $checksumRequired;
384+
}
319385
}

src/Common/Helper.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,11 @@ public static function bytesToShort(
125125
public static function stringToKey(
126126
S2kType $type = S2kType::Iterated
127127
): S2KInterface {
128+
if ($type === S2kType::Simple) {
129+
throw new \RuntimeException(
130+
"S2k type {$type->name} is unsupported."
131+
);
132+
}
128133
return $type === S2kType::Argon2
129134
? new Argon2S2K(
130135
self::generatePassword(Argon2S2K::SALT_LENGTH),

0 commit comments

Comments
 (0)