aboutsummaryrefslogtreecommitdiffstats
path: root/tests/unit/Lib/KeyutilsTest.php
blob: 1f3fa2295e955d49f81eddf17e2dbc5d4cb06fa7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
<?php
/*
 * Copyright (c) 2016-2017 Hubzilla
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

namespace Zotlabs\Tests\Unit\Lib;

use phpseclib3\Crypt\PublicKeyLoader;
use phpseclib3\Math\BigInteger;

use Zotlabs\Tests\Unit\UnitTestCase;
use Zotlabs\Lib\Keyutils;

/**
 * @brief Unit Test case for Keyutils class.
 *
 * @covers Zotlabs\Lib\Keyutils
 */

class KeyutilsTest extends UnitTestCase {

	protected function getPubPKCS1() {
		$key = '-----BEGIN RSA PUBLIC KEY-----
MIIBCgKCAQEAsSBBV5khOWvMAOIV2AhxQukBgWc1pfYqUM/9OIFfL+KaRDOFAT0y
jMbtTCbHEjxZbasS3CNnHtAgJ+4BWjtFnZl7zY4pXb2RDe4IWNnK8BjqsWDVuPks
sc+yRBTepR/50FG/xB4HfI4GRc/8EDmynyAdLDD/r6oPxmz1gMICqSGDX8yXYG4o
DliNT3bWDH+uf1+6pWsN6IFQYmUoRLly1xsfc8AV4H1GmSFrbAvQpHA95GOlxnPY
IwVth1m8O+D9SH2+0e8ourdXH3/9ccJnh6FVvFvkyeC1e2vY+J+uC0uwu+6IvHFV
pSnS8lTmwG/BBh2dA5eqV3s+REdLdS/tsQIDAQAB
-----END RSA PUBLIC KEY-----';
		return str_replace(["\r", "\n"], "\r\n", $key);
	}

	protected function getPubPKCS8() {
		$key = '-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAsSBBV5khOWvMAOIV2Ahx
QukBgWc1pfYqUM/9OIFfL+KaRDOFAT0yjMbtTCbHEjxZbasS3CNnHtAgJ+4BWjtF
nZl7zY4pXb2RDe4IWNnK8BjqsWDVuPkssc+yRBTepR/50FG/xB4HfI4GRc/8EDmy
nyAdLDD/r6oPxmz1gMICqSGDX8yXYG4oDliNT3bWDH+uf1+6pWsN6IFQYmUoRLly
1xsfc8AV4H1GmSFrbAvQpHA95GOlxnPYIwVth1m8O+D9SH2+0e8ourdXH3/9ccJn
h6FVvFvkyeC1e2vY+J+uC0uwu+6IvHFVpSnS8lTmwG/BBh2dA5eqV3s+REdLdS/t
sQIDAQAB
-----END PUBLIC KEY-----';
		return str_replace(["\r", "\n"], "\r\n", $key);
	}

	public function testMeToPem() {
		[$m, $e] = Keyutils::pemToMe($this->getPubPKCS8());
		$gen_key = Keyutils::meToPem($m, $e);
		self::assertEquals($this->getPubPKCS8(), $gen_key);
	}

	public function testRsaToPem() {
		$gen_key = Keyutils::rsaToPem($this->getPubPKCS1());
		self::assertEquals($gen_key, $this->getPubPKCS8());
	}

	public function testPemToRsa() {
		$gen_key = Keyutils::pemToRsa($this->getPubPKCS8());
		self::assertEquals($gen_key, $this->getPubPKCS1());
	}

	public function testPemToMe() {
		[$m, $e] = Keyutils::pemToMe($this->getPubPKCS8());

		$parsedKey = PublicKeyLoader::load([
			'e' => new BigInteger($e, 256),
			'n' => new BigInteger($m, 256)
		]);

		self::assertEquals($parsedKey->toString('PKCS8'), $this->getPubPKCS8());
	}

}