Replace library/asn1.php with phpseclib

This commit is contained in:
Philipp 2020-09-12 11:57:37 +02:00
parent 0af08ac1d4
commit 11ef3895f5
No known key found for this signature in database
GPG key ID: 9A28B7D4FF5667BD
9 changed files with 197 additions and 455 deletions

View file

@ -21,6 +21,8 @@
*/
namespace Friendica\Util;
use phpseclib\Crypt\RSA;
use phpseclib\Math\BigInteger;
use PHPUnit\Framework\TestCase;
class CryptoTest extends TestCase
@ -32,7 +34,7 @@ class CryptoTest extends TestCase
private function assertRandomInt($min, $max)
{
global $phpMock;
$phpMock['random_int'] = function($mMin, $mMax) use ($min, $max) {
$phpMock['random_int'] = function ($mMin, $mMax) use ($min, $max) {
$this->assertEquals($min, $mMin);
$this->assertEquals($max, $mMax);
return 1;
@ -51,6 +53,50 @@ class CryptoTest extends TestCase
$this->assertEquals(8, strlen($test));
$this->assertEquals(11111111, $test);
}
public function dataRsa()
{
return [
'diaspora' => [
'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-rsa-base64'),
'expected' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
],
];
}
/**
* @dataProvider dataRsa
*/
public function testPubRsaToMe(string $key, string $expected)
{
$this->assertEquals($expected, Crypto::rsaToPem(base64_decode($key)));
}
public function datePem()
{
return [
'diaspora' => [
'key' => file_get_contents(__DIR__ . '/../../datasets/crypto/rsa/diaspora-public-pem'),
],
];
}
/**
* @dataProvider datePem
*/
public function testPemToMe(string $key)
{
Crypto::pemToMe($key, $m, $e);
$expectedRSA = new RSA();
$expectedRSA->loadKey([
'e' => new BigInteger($e, 256),
'n' => new BigInteger($m, 256)
]);
$this->assertEquals($expectedRSA->getPublicKey(), $key);
}
}
/**