Skip to content

Commit

Permalink
Merge pull request #18 from SignpostMarv/update-implementation/hidden…
Browse files Browse the repository at this point in the history
…-string

adding HiddenString support
  • Loading branch information
paragonie-scott authored May 13, 2019
2 parents 169bd6a + 1e65215 commit c062ccb
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 14 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
"require": {
"php": "^7",
"bacon/bacon-qr-code": "^1",
"paragonie/constant_time_encoding": "^2"
"paragonie/constant_time_encoding": "^2",
"paragonie/hidden-string": "^1"
},
"require-dev": {
"phpunit/phpunit": "^6",
Expand Down
7 changes: 4 additions & 3 deletions src/OTP/HOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Binary,
Hex
};
use ParagonIE\HiddenString\HiddenString;

/**
* Class HOTP
Expand Down Expand Up @@ -41,13 +42,13 @@ public function __construct(
* Generate a HOTP secret in accordance with RFC 4226
*
* @ref https://tools.ietf.org/html/rfc4226
* @param string $sharedSecret The key to use for determining the HOTP
* @param string|HiddenString $sharedSecret The key to use for determining the HOTP
* @param int $counterValue Current time or HOTP counter
* @return string
* @throws \OutOfRangeException
*/
public function getCode(
string $sharedSecret,
$sharedSecret,
int $counterValue
): string {
if ($this->length < 1 || $this->length > 10) {
Expand All @@ -56,7 +57,7 @@ public function getCode(
);
}
$msg = $this->getTValue($counterValue, true);
$bytes = \hash_hmac($this->algo, $msg, $sharedSecret, true);
$bytes = \hash_hmac($this->algo, $msg, is_string($sharedSecret) ? $sharedSecret : $sharedSecret->getString(), true);

$byteLen = Binary::safeStrlen($bytes);

Expand Down
6 changes: 4 additions & 2 deletions src/OTP/OTPInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
declare(strict_types=1);
namespace ParagonIE\MultiFactor\OTP;

use ParagonIE\HiddenString\HiddenString;

/**
* Interface OTPInterface
* @package ParagonIE\MultiFactor\OTP
Expand All @@ -11,13 +13,13 @@ interface OTPInterface
/**
* Get the code we need
*
* @param string $sharedSecret The key to use for determining the TOTP
* @param string|HiddenString $sharedSecret The key to use for determining the TOTP
* @param int $counterValue Current time or HOTP counter
* @return string
* @throws \OutOfRangeException
*/
public function getCode(
string $sharedSecret,
$sharedSecret,
int $counterValue
): string;

Expand Down
7 changes: 4 additions & 3 deletions src/OTP/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Binary,
Hex
};
use ParagonIE\HiddenString\HiddenString;

/**
* Class TOTP
Expand Down Expand Up @@ -57,13 +58,13 @@ public function __construct(
* Generate a TOTP secret in accordance with RFC 6238
*
* @ref https://tools.ietf.org/html/rfc6238
* @param string $sharedSecret The key to use for determining the TOTP
* @param string|HiddenString $sharedSecret The key to use for determining the TOTP
* @param int $counterValue Current time or HOTP counter
* @return string
* @throws \OutOfRangeException
*/
public function getCode(
string $sharedSecret,
$sharedSecret,
int $counterValue
): string {
if ($this->length < 1 || $this->length > 10) {
Expand All @@ -72,7 +73,7 @@ public function getCode(
);
}
$msg = $this->getTValue($counterValue, true);
$bytes = \hash_hmac($this->algo, $msg, $sharedSecret, true);
$bytes = \hash_hmac($this->algo, $msg, is_string($sharedSecret) ? $sharedSecret : $sharedSecret->getString(), true);

$byteLen = Binary::safeStrlen($bytes);

Expand Down
9 changes: 5 additions & 4 deletions src/OneTime.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
OTPInterface,
TOTP
};
use ParagonIE\HiddenString\HiddenString;

/**
* Class OneTime
Expand All @@ -20,21 +21,21 @@ class OneTime implements MultiFactorInterface
protected $otp;

/**
* @var string
* @var HiddenString
*/
protected $secretKey;

/**
* FIDOU2F constructor.
*
* @param string $secretKey
* @param string|HiddenString $secretKey
* @param OTPInterface $otp
*/
public function __construct(
string $secretKey = '',
$secretKey = '',
OTPInterface $otp = null
) {
$this->secretKey = $secretKey;
$this->secretKey = ($secretKey instanceof HiddenString) ? $secretKey : new HiddenString($secretKey);
if (!$otp) {
$otp = new TOTP();
}
Expand Down
2 changes: 1 addition & 1 deletion src/Vendor/GoogleAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function makeQRCode(
}
$message .= \urlencode($username);
$args = [
'secret' => Base32::encode($this->secretKey)
'secret' => Base32::encode($this->secretKey->getString())
];
if ($issuer) {
$args['issuer'] = $issuer;
Expand Down

0 comments on commit c062ccb

Please sign in to comment.