Skip to content

Commit 87f0334

Browse files
committed
fix: use already instantiated username and use salt
1 parent f9f97c7 commit 87f0334

File tree

2 files changed

+80
-13
lines changed

2 files changed

+80
-13
lines changed

src/Wowemu/SRP/UserClient.php

+7-13
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,14 @@ public function setHostPublicEphemeralValue(string $value): void
3939
/**
4040
* Generate verifier using username, password and existing salt
4141
*
42-
* @param string $I User's identity (username)
4342
* @param string $p User's password in plaintext
44-
* @param string $s User's salt
4543
*
4644
* @return string
4745
* @throws Exception
4846
*/
49-
public function generateVerifier(string $I, string $p, string $s): string
47+
public function generateVerifier(string $p): string
5048
{
51-
$privateKey = $this->computePrivateKey($s, $I, $p);
49+
$privateKey = $this->computePrivateKey($p);
5250
$verifier = $this->computeVerifier($privateKey);
5351

5452
return $verifier->toHex();
@@ -58,26 +56,22 @@ public function generateVerifier(string $I, string $p, string $s): string
5856
* Computes private key using salt and identity which is derived from username and password
5957
*
6058
* @param string $p User's password in plaintext
61-
* @param string $I User's identity (username)
62-
* @param string $s User's salt
6359
*
6460
* @return BigInteger
6561
*/
66-
public function computePrivateKey(string $p, string $I = null, string $s = null): BigInteger
62+
public function computePrivateKey(string $p): BigInteger
6763
{
68-
$salt = $s ?? $this->salt;
69-
if (empty($salt)) {
64+
if (empty($this->salt)) {
7065
throw new RuntimeException('Received empty salt.');
7166
}
7267

73-
$username = $I ?? $this->username;
74-
if (empty($username)) {
68+
if (empty($this->username)) {
7569
throw new RuntimeException('Received empty username.');
7670
}
7771

78-
$salt = $this->reverseHex($salt);
72+
$salt = $this->reverseHex($this->salt);
7973
$salt = hex2bin($salt);
80-
$identity = hash('sha1', strtoupper($username.':'.$p), true);
74+
$identity = hash('sha1', strtoupper($this->username.':'.$p), true);
8175

8276
$sha = sha1($salt.$identity);
8377
$sha = $this->reverseHex($sha);

tests/UserClientTest.php

+73
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
/*
3+
* (c) Dmitri Petmanson <[email protected]>
4+
*
5+
* For the full copyright and license information, please view the LICENSE
6+
* file that was distributed with this source code.
7+
*/
8+
9+
namespace Tests;
10+
11+
use Exception;
12+
use Laizerox\Wowemu\SRP\UserClient;
13+
use PHPUnit\Framework\TestCase;
14+
use RuntimeException;
15+
16+
class UserClientTest extends TestCase
17+
{
18+
public function dataProvider(): array
19+
{
20+
return [
21+
[
22+
'admin',
23+
'admin',
24+
'12ee32e201835ebc6a00c7056f08e18651633ab9cec6cfd5a1bdda413747c74c',
25+
'2b25415d6fd90435b9506f64c15e0670bef49a9905d62f21eb573dc4ff2bbaf0',
26+
],
27+
[
28+
'player',
29+
'player',
30+
'50b39832882cc3174f4b566d377775ecc33af5f21fa71bcac58290595101d4e9',
31+
'59f9d68f247ff723c46677847e042923184307f652c297726da2868670c607bf',
32+
],
33+
];
34+
}
35+
36+
/**
37+
* @param $username
38+
* @param $password
39+
* @param $salt
40+
* @param $expectedVerifier
41+
*
42+
* @dataProvider dataProvider
43+
* @throws Exception
44+
*/
45+
public function testGenerateVerifierAgainstExistingData($username, $password, $salt, $expectedVerifier): void
46+
{
47+
$client = new UserClient($username, $salt);
48+
49+
$this->assertEquals($expectedVerifier, $client->generateVerifier($password));
50+
}
51+
52+
/**
53+
* @throws Exception
54+
*/
55+
public function testGenerateVerifierWithEmptyUsername(): void
56+
{
57+
$this->expectException(RuntimeException::class);
58+
59+
$client = new UserClient('');
60+
$client->generateVerifier('');
61+
}
62+
63+
/**
64+
* @throws Exception
65+
*/
66+
public function testGenerateVerifierWithEmptySalt(): void
67+
{
68+
$this->expectException(RuntimeException::class);
69+
70+
$client = new UserClient('admin');
71+
$client->generateVerifier('admin');
72+
}
73+
}

0 commit comments

Comments
 (0)