Skip to content

Commit

Permalink
update to php 8.3 - refactoring, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Eroslaev committed Sep 12, 2024
1 parent fa75c01 commit 1ab1d48
Show file tree
Hide file tree
Showing 29 changed files with 808 additions and 659 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
v6.3.0
v6.3.1
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
}
],
"require": {
"php": "^8.2",
"php": "^8.3",
"myclabs/php-enum": "^1.8",
"virgil/crypto-wrapper": "^0.17",
"ext-json": "*"
Expand Down
10 changes: 5 additions & 5 deletions src/Core/Enum/HashAlgorithms.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
*
* All rights reserved.
*
Expand Down Expand Up @@ -53,8 +53,8 @@
*/
class HashAlgorithms extends Enum
{
private const SHA224 = "SHA224";
private const SHA256 = "SHA256";
private const SHA384 = "SHA384";
private const SHA512 = "SHA512";
private const string SHA224 = "SHA224";
private const string SHA256 = "SHA256";
private const string SHA384 = "SHA384";
private const string SHA512 = "SHA512";
}
111 changes: 36 additions & 75 deletions src/Core/Enum/KeyPairType.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
*
* All rights reserved.
*
Expand Down Expand Up @@ -57,12 +57,12 @@
*/
class KeyPairType extends Enum
{
private const ED25519 = "ED25519";
private const CURVE25519 = "CURVE25519";
private const SECP256R1 = "SECP256R1";
private const RSA2048 = "RSA2048";
private const RSA4096 = "RSA4096";
private const RSA8192 = "RSA8192";
private const string ED25519 = "ED25519";
private const string CURVE25519 = "CURVE25519";
private const string SECP256R1 = "SECP256R1";
private const string RSA2048 = "RSA2048";
private const string RSA4096 = "RSA4096";
private const string RSA8192 = "RSA8192";

/**
* @param KeyPairType $keyPairType
Expand All @@ -71,21 +71,12 @@ class KeyPairType extends Enum
*/
public function getRsaBitLen(KeyPairType $keyPairType): ?int
{
switch ($keyPairType) {
case $keyPairType::RSA2048():
$res = 2048;
break;
case $keyPairType::RSA4096():
$res = 4096;
break;
case $keyPairType::RSA8192():
$res = 8192;
break;
default:
$res = null;
}

return $res;
return match ((string)$keyPairType) {
(string)$keyPairType::RSA2048() => 2048,
(string)$keyPairType::RSA4096() => 4096,
(string)$keyPairType::RSA8192() => 8192,
default => null,
};
}

/**
Expand All @@ -96,21 +87,12 @@ public function getRsaBitLen(KeyPairType $keyPairType): ?int
*/
public static function getRsaKeyType(int $bitLen): KeyPairType
{
switch ($bitLen) {
case 2048:
$res = KeyPairType::RSA2048();
break;
case 4096:
$res = KeyPairType::RSA4096();
break;
case 8192:
$res = KeyPairType::RSA8192();
break;
default:
throw new VirgilCryptoException(VirgilCryptoError::UNSUPPORTED_RSA_LENGTH());
}

return $res;
return match ($bitLen) {
2048 => KeyPairType::RSA2048(),
4096 => KeyPairType::RSA4096(),
8192 => KeyPairType::RSA8192(),
default => throw new VirgilCryptoException(VirgilCryptoError::UNSUPPORTED_RSA_LENGTH()),
};
}

/**
Expand All @@ -121,23 +103,15 @@ public static function getRsaKeyType(int $bitLen): KeyPairType
*/
public static function getFromAlgId(AlgId $algId): KeyPairType
{
switch ($algId) {
case $algId::ED25519():
$res = KeyPairType::ED25519();
break;
case $algId::CURVE25519():
$res = KeyPairType::CURVE25519();
break;
case $algId::SECP256R1():
$res = KeyPairType::SECP256R1();
break;
case $algId::RSA():
throw new VirgilCryptoException(VirgilCryptoError::RSA_SHOULD_BE_CONSTRUCTED_DIRECTLY());
default:
throw new VirgilCryptoException(VirgilCryptoError::UNKNOWN_ALG_ID());
}

return $res;
return match ((string)$algId) {
(string)$algId::ED25519() => KeyPairType::ED25519(),
(string)$algId::CURVE25519() => KeyPairType::CURVE25519(),
(string)$algId::SECP256R1() => KeyPairType::SECP256R1(),
(string)$algId::RSA() => throw new VirgilCryptoException(
VirgilCryptoError::RSA_SHOULD_BE_CONSTRUCTED_DIRECTLY()
),
default => throw new VirgilCryptoException(VirgilCryptoError::UNKNOWN_ALG_ID()),
};
}

/**
Expand All @@ -148,26 +122,13 @@ public static function getFromAlgId(AlgId $algId): KeyPairType
*/
public function getAlgId(KeyPairType $keyPairType): AlgId
{
switch ($keyPairType) {
case $keyPairType::ED25519():
$res = AlgId::ED25519();
break;
case $keyPairType::CURVE25519():
$res = AlgId::CURVE25519();
break;
case $keyPairType::SECP256R1():
$res = AlgId::SECP256R1();
break;
case $keyPairType::RSA2048():
case $keyPairType::RSA4096():
case $keyPairType::RSA8192():
$res = AlgId::RSA();
break;
default:
throw new VirgilCryptoException(VirgilCryptoError::UNKNOWN_ALG_ID());
}

return $res;
return match ((string)$keyPairType) {
(string)$keyPairType::ED25519() => AlgId::ED25519(),
(string)$keyPairType::CURVE25519() => AlgId::CURVE25519(),
(string)$keyPairType::SECP256R1() => AlgId::SECP256R1(),
(string)$keyPairType::RSA2048(), (string)$keyPairType::RSA4096(), (string)$keyPairType::RSA8192(
) => AlgId::RSA(),
default => throw new VirgilCryptoException(VirgilCryptoError::UNKNOWN_ALG_ID()),
};
}

}
6 changes: 3 additions & 3 deletions src/Core/Enum/SigningMode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -42,6 +42,6 @@
*/
class SigningMode extends Enum
{
private const SIGN_AND_ENCRYPT = "SIGN_AND_ENCRYPT";
private const SIGN_THEN_ENCRYPT = "SIGN_THEN_ENCRYPT";
private const string SIGN_AND_ENCRYPT = "SIGN_AND_ENCRYPT";
private const string SIGN_THEN_ENCRYPT = "SIGN_THEN_ENCRYPT";
}
8 changes: 4 additions & 4 deletions src/Core/Enum/VerifyingMode.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -43,7 +43,7 @@
*/
class VerifyingMode extends Enum
{
private const DECRYPT_AND_VERIFY = "DECRYPT_AND_VERIFY";
private const DECRYPT_THEN_VERIFY = "DECRYPT_THEN_VERIFY";
private const ANY = "ANY";
private const string DECRYPT_AND_VERIFY = "DECRYPT_AND_VERIFY";
private const string DECRYPT_THEN_VERIFY = "DECRYPT_THEN_VERIFY";
private const string ANY = "ANY";
}
26 changes: 13 additions & 13 deletions src/Core/Enum/VirgilCryptoError.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -54,18 +54,18 @@
*/
class VirgilCryptoError extends Enum
{
private const SIGNER_NOT_FOUND = [1, "Signer not found"];
private const SIGNATURE_NOT_FOUND = [2, "Signature not found"];
private const SIGNATURE_NOT_VERIFIED = [3, "Signature not verified"];
private const UNKNOWN_ALG_ID = [4, "Unknown alg id"];
private const RSA_SHOULD_BE_CONSTRUCTED_DIRECTLY = [5, "Rsa should be constructed directly"];
private const UNSUPPORTED_RSA_LENGTH = [6, "Unsupported rsa length"];
private const PASSED_KEY_IS_NOT_VIRGIL = [8, "Passed key is not virgil"];
private const OUTPUT_STREAM_ERROR = [9, "Output stream has no space left"];
private const INPUT_STREAM_ERROR = [10, "Input stream has no space left"];
private const INVALID_SEED_SIZE = [11, "Invalid seed size"];
private const DATA_IS_NOT_SIGNED = [12, "Data has no signature to verify"];
private const INVALID_STREAM_SIZE = [13, "Actual stream size doesn't match with given value"];
private const array SIGNER_NOT_FOUND = [1, "Signer not found"];
private const array SIGNATURE_NOT_FOUND = [2, "Signature not found"];
private const array SIGNATURE_NOT_VERIFIED = [3, "Signature not verified"];
private const array UNKNOWN_ALG_ID = [4, "Unknown alg id"];
private const array RSA_SHOULD_BE_CONSTRUCTED_DIRECTLY = [5, "Rsa should be constructed directly"];
private const array UNSUPPORTED_RSA_LENGTH = [6, "Unsupported rsa length"];
private const array PASSED_KEY_IS_NOT_VIRGIL = [8, "Passed key is not virgil"];
private const array OUTPUT_STREAM_ERROR = [9, "Output stream has no space left"];
private const array INPUT_STREAM_ERROR = [10, "Input stream has no space left"];
private const array INVALID_SEED_SIZE = [11, "Invalid seed size"];
private const array DATA_IS_NOT_SIGNED = [12, "Data has no signature to verify"];
private const array INVALID_STREAM_SIZE = [13, "Actual stream size doesn't match with given value"];

/**
* @return int
Expand Down
14 changes: 4 additions & 10 deletions src/Core/IO/FileInputStream.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -40,11 +40,6 @@
*/
class FileInputStream implements InputStream
{
/**
* @var string
*/
private $input;

/**
* @var resource
*/
Expand All @@ -53,7 +48,7 @@ class FileInputStream implements InputStream
/**
* @var int
*/
private $offset;
private int $offset;

/**
* FileInputStream constructor.
Expand All @@ -62,14 +57,13 @@ class FileInputStream implements InputStream
*
* @throws VirgilCryptoException
*/
public function __construct(string $input)
public function __construct(private readonly string $input)
{
$resource = fopen($input, "rb");
if (!$resource) {
throw new VirgilCryptoException(VirgilCryptoError::INPUT_STREAM_ERROR());
}

$this->input = $input;
$this->resource = $resource;
$this->offset = 0;
}
Expand All @@ -94,4 +88,4 @@ public function __destruct()
{
fclose($this->resource);
}
}
}
15 changes: 5 additions & 10 deletions src/Core/IO/FileOutputStream.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -40,11 +40,6 @@
*/
class FileOutputStream implements OutputStream
{
/**
* @var string
*/
private $output;

/**
* @var resource
*/
Expand All @@ -57,13 +52,13 @@ class FileOutputStream implements OutputStream
*
* @throws VirgilCryptoException
*/
public function __construct(string $output)
public function __construct(private string $output)
{
$resource = fopen($output, "a");
if (!$resource)
if (!$resource) {
throw new VirgilCryptoException(VirgilCryptoError::OUTPUT_STREAM_ERROR());
}

$this->output = $output;
$this->resource = $resource;
}

Expand All @@ -81,4 +76,4 @@ public function __destruct()
{
fclose($this->resource);
}
}
}
4 changes: 2 additions & 2 deletions src/Core/IO/InputStream.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -44,4 +44,4 @@ interface InputStream
* @return int
*/
public function read(string &$buffer, int $size): int;
}
}
5 changes: 2 additions & 3 deletions src/Core/IO/OutputStream.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/**
* Copyright (C) 2015-2020 Virgil Security Inc.
* Copyright (C) 2015-2024 Virgil Security Inc.
* All rights reserved.
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
Expand Down Expand Up @@ -43,5 +43,4 @@ interface OutputStream
* @return int
*/
public function write(string $chunk): int;

}
}
Loading

0 comments on commit 1ab1d48

Please sign in to comment.