Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Nguyen Van Nguyen <[email protected]>
  • Loading branch information
nguyennv committed Oct 1, 2024
1 parent c476746 commit ebd0cec
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 18 deletions.
50 changes: 32 additions & 18 deletions src/Common/Armor.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@

namespace OpenPGP\Common;

use phpseclib3\Common\Functions\Strings;
use OpenPGP\Enum\ArmorType;
use phpseclib3\Common\Functions\Strings;

/**
* Armor class
Expand Down Expand Up @@ -127,13 +127,10 @@ public function assert(ArmorType $type): self
* Verify the checksum and return the encoded bytes
*
* @param string $armoredText
* @param bool $checksumRequired
* @return self
*/
public static function decode(
string $armoredText,
bool $checksumRequired = false
): self {
public static function decode(string $armoredText): self
{
$textDone = false;
$checksum = "";
$type = null;
Expand Down Expand Up @@ -182,7 +179,7 @@ public static function decode(
$data = Strings::base64_decode(implode($dataLines));
if (
strcmp($checksum, self::crc24Checksum($data)) !== 0 &&
(!empty($checksum) || $checksumRequired)
(!empty($checksum) || Config::checksumRequired())
) {
throw new \RuntimeException("Ascii armor integrity check failed!");
}
Expand Down Expand Up @@ -233,7 +230,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
sprintf(
self::MULTIPART_SECTION_MESSAGE_END,
$partIndex,
Expand All @@ -248,7 +247,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
sprintf(self::MULTIPART_LAST_MESSAGE_END, $partIndex),
],
ArmorType::SignedMessage => [
Expand All @@ -272,7 +273,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
self::SIGNATURE_END,
],
ArmorType::Message => [
Expand All @@ -283,7 +286,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
self::MESSAGE_END,
],
ArmorType::PublicKey => [
Expand All @@ -294,7 +299,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
self::PUBLIC_KEY_BLOCK_END,
],
ArmorType::PrivateKey => [
Expand All @@ -305,7 +312,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
self::PRIVATE_KEY_BLOCK_END,
],
ArmorType::Signature => [
Expand All @@ -316,7 +325,9 @@ public static function encode(
self::TRUNK_SIZE,
Helper::EOL
),
"=" . self::crc24Checksum($data) . Helper::EOL,
Config::checksumRequired()
? "=" . self::crc24Checksum($data) . Helper::EOL
: "",
self::SIGNATURE_END,
],
};
Expand All @@ -331,10 +342,13 @@ public static function encode(
*/
private static function addHeader(string $customComment = ""): string
{
$headers = [
"Version: " . Config::VERSION . Helper::EOL,
"Comment: " . Config::COMMENT . Helper::EOL,
];
$headers = [];
if (Config::showVersion()) {
$headers[] = "Version: " . Config::VERSION . Helper::EOL;
}
if (Config::showComment()) {
$headers[] = "Comment: " . Config::COMMENT . Helper::EOL;
}
if (!empty($customComment)) {
$headers[] = "Comment: " . $customComment . Helper::EOL;
}
Expand Down
66 changes: 66 additions & 0 deletions src/Common/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ final class Config

private static bool $allowUnauthenticated = false;

private static bool $showVersion = true;

private static bool $showComment = false;

private static bool $checksumRequired = false;

/**
* Get preferred hash algorithm.
*
Expand Down Expand Up @@ -316,4 +322,64 @@ public static function setAllowUnauthenticated(bool $allow): void
{
self::$allowUnauthenticated = $allow;
}

/**
* Whether to include version header in armored messages.
*
* @return bool
*/
public static function showVersion(): bool
{
return self::$showVersion;
}

/**
* Set show version.
*
* @param bool $showVersion
*/
public static function setShowVersion(bool $showVersion): void
{
self::$showVersion = $showVersion;
}

/**
* Whether to include comment header in armored messages.
*
* @return bool
*/
public static function showComment(): bool
{
return self::$showComment;
}

/**
* Set show comment.
*
* @param bool $showComment
*/
public static function setShowComment(bool $showComment): void
{
self::$showComment = $showComment;
}

/**
* Whether checksum required in armored messages.
*
* @return bool
*/
public static function checksumRequired(): bool
{
return self::$checksumRequired;
}

/**
* Set checksum required.
*
* @param bool $checksumRequired
*/
public static function setChecksumRequired(bool $checksumRequired): void
{
self::$checksumRequired = $checksumRequired;
}
}
5 changes: 5 additions & 0 deletions src/Common/Helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ public static function bytesToShort(
public static function stringToKey(
S2kType $type = S2kType::Iterated
): S2KInterface {
if ($type === S2kType::Simple) {
throw new \RuntimeException(
"S2k type {$type->name} is unsupported."
);
}
return $type === S2kType::Argon2
? new Argon2S2K(
self::generatePassword(Argon2S2K::SALT_LENGTH),
Expand Down

0 comments on commit ebd0cec

Please sign in to comment.