Skip to content

Commit

Permalink
Update Readme, fixing minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
ekojs committed May 9, 2022
1 parent 66aeb7b commit e98a721
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 10 deletions.
105 changes: 103 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,103 @@
# ejsotp
TOTP dan HOTP Library with backup codes, compatible with google authenticator
OTP Library
=============

TOTP dan HOTP Library with backup codes, compatible with google authenticator. Compatible with PHP >= 7.3.

[![Latest Stable Version](http://poser.pugx.org/ekojs/otp/v)](https://packagist.org/packages/ekojs/otp) [![Total Downloads](http://poser.pugx.org/ekojs/otp/downloads)](https://packagist.org/packages/ekojs/otp) [![Latest Unstable Version](http://poser.pugx.org/ekojs/otp/v/unstable)](https://packagist.org/packages/ekojs/otp) [![License](http://poser.pugx.org/ekojs/otp/license)](https://packagist.org/packages/ekojs/otp) [![PHP Version Require](http://poser.pugx.org/ekojs/otp/require/php)](https://packagist.org/packages/ekojs/otp)

## Install

For PHP version **`>= 7.3`**:

```
composer require ekojs/otp
```

## How to use
### Generate TOTP (Time-Based One-Time Password) based on [RFC 6238](https://datatracker.ietf.org/doc/html/rfc6238)
```php
<?php
require_once "vendor/autoload.php";

use Ekojs\Otp\TOTP;
use Ekojs\Otp\HOTP;

$ejsotp = TOTP::getInstance();
$ejsotp->otp->setLabel("[email protected]");
$ejsotp->otp->setIssuer("My Service");

echo "The TOTP secret is: {$ejsotp->otp->getSecret()}".PHP_EOL;
echo "The current TOTP is: {$ejsotp->otp->now()}".PHP_EOL;
echo "The Uri is: {$ejsotp->otp->getProvisioningUri()}".PHP_EOL;
```

### Verify TOTP
```php
$ejsotp = TOTP::getInstance(["secret" => "VE7RDW7LC45QHKVZI6SPHDQK254TKO7CPG6KHPQ4RYN4MGBBA6EAAHVYHRVAGO5LPF6XNDPAOLE3KYQHBBHPB62VFVNZURWRZUDER4A"]);
$ejsotp->otp->setLabel('[email protected]');
$ejsotp->otp->setIssuer("My Service");

echo 'Current TOTP: ' . $ejsotp->otp->now() . PHP_EOL;
var_dump($ejsotp->otp->verify("988942"));
```

### Generate HOTP (HMAC-Based One-Time Password) based on [RFC 4226](http://tools.ietf.org/html/rfc4226)
```php
$ejshotp = HOTP::getInstance(["counter" => 1000]);
$ejshotp->otp->setLabel("[email protected]");
$ejshotp->otp->setIssuer("My Service HOTP");

echo "The HOTP secret is: {$ejshotp->otp->getSecret()}".PHP_EOL;
echo "The current HOTP is: {$ejshotp->otp->at($ejshotp->otp->getCounter())}".PHP_EOL;
echo "The Uri is: {$ejshotp->otp->getProvisioningUri()}".PHP_EOL;
```

### Verify HOTP
```php
$ejshotp = HOTP::getInstance([
"secret" => "HZHL2VE2RWMT2KHDQCYCLPXJRJC7T63SZFNDTLEEEJISHLQS5Y6CRDTW4D7D3GA35VMSA32NAGLXEEFDSRT63E332JQOCTDAVK4HZHI",
"counter" => 1000
]);
$ejshotp->otp->setLabel("[email protected]");
$ejshotp->otp->setIssuer("My Service HOTP");

echo 'Current OTP: ' . $ejshotp->otp->at(1001) . PHP_EOL;
var_dump($ejshotp->otp->verify("598162",1001));
```

### Generate Backup Codes (Mnemonic) based on [BIP 39](https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki)
```php
echo "Hash secret : ". hash("md5","mysecret").PHP_EOL; // 06c219e5bc8378f3a8a3f83b4b7e4649
echo "Backup codes: ". implode(" ",$ejsotp->generateBackupCodes("mysecret")).PHP_EOL;
```

### Reverse Mnemonic / Backup Codes
```php
echo "Reverse Mnemonic: ".$ejsotp->reverseMnemonic("almost awkward just jungle daring keep penalty lecture deputy fossil muscle nasty").PHP_EOL; // 06c219e5bc8378f3a8a3f83b4b7e4649
```

### Generate QrCode compatible with [Google Authenticator](https://play.google.com/store/apps/details?id=com.google.android.apps.authenticator2)
```php
$result = $ejsotp->generateQr();
echo "<img src='".$result->getDataUri()."' />";
```

### Verify Using Google Authenticator
Scan this Qr using Google Authenticator

![Imgur](https://i.imgur.com/FexUnMJ.png)

Create test.php, and execute it from terminal
```php
<?php
require_once "vendor/autoload.php";

use Ekojs\Otp\TOTP;

$ejsotp = TOTP::getInstance(["secret" => "VZCKGWRLS7CINEYALENYPH5T442LJUAFGSNCBTBQEHMN5GSVGTJCD2B7NHCZFK5FZ3QHTQ66JYDMNUI2UBWZJAYHI62VYVHVUGTO6SQ"]);
$ejsotp->otp->setLabel('[email protected]');
$ejsotp->otp->setIssuer("My Service");

var_dump($ejsotp->otp->verify("input code from your Google Authenticator")); // if true the code is valid
echo 'Current OTP: ' . $ejsotp->otp->now() . PHP_EOL;
```
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"minimum-stability": "stable",
"require": {
"php": ">=7.3",
"spomky-labs/otphp": "^10.0",
"endroid/qr-code": "^4.4",
"furqansiddiqui/bip39-mnemonic-php": "^0.1.4"
Expand Down
8 changes: 4 additions & 4 deletions src/HOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public function generateQr(?string $logo=null, bool $setLabel=false, int $size=2
return $this->writer->write($qrCode, $logo, $label);
}

public function generateBackupCodes(string $entropy, ?WordList $wordList=null) {
public function generateBackupCodes(string $entropy, ?WordList $wordList=null): array {
$wordList = $wordList ?? WordList::English();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic()->words;
}

public function reverseMnemonic($words) {
return $this->bip39::Words($words);
public function reverseMnemonic($words): string {
return $this->bip39::Words($words)->entropy;
}
}

8 changes: 4 additions & 4 deletions src/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,13 @@ public function generateQr(?string $logo=null, bool $setLabel=false, int $size=2
return $this->writer->write($qrCode, $logo, $label);
}

public function generateBackupCodes(string $entropy, ?WordList $wordList=null) {
public function generateBackupCodes(string $entropy, ?WordList $wordList=null): array {
$wordList = $wordList ?? WordList::English();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic();
return $this->bip39->wordlist($wordList)->useEntropy(hash("md5",$entropy))->mnemonic()->words;
}

public function reverseMnemonic($words) {
return $this->bip39::Words($words);
public function reverseMnemonic($words): string {
return $this->bip39::Words($words)->entropy;
}
}

0 comments on commit e98a721

Please sign in to comment.