# Yubikey OTP / OATH 

Library that parses / verifies OTPs generated by Yubikey in Yubico and OATH mode.

### Yubico OTP


Usage:

```
<?php

use Buckhill\Yubico\Modes\Yubikey\OTP;
use Buckhill\Yubico\Modes\Yubikey\Yubikey;
use Buckhill\Yubico\Modes\Yubikey\DecryptionResult;

$otp = new OTP('vvjclrcjlefnjvrldcflfrrjitjlnrtebuklrhlvtujn');

$yubikey = Yubikey::fromArray([
	'public_id'			=> 'vv jc lr cj le fn',
	'private_id' 		=> 'f8 d1 8d 05 c1 3a',
	'secret_key' 		=> 'e7 7d a9 46 de f1 2b b9 47 cd a0 f7 a4 45 cf 84',
	'use_counter' 		=> 0,
	'session_counter' 	=> 0	
]);

try
{
	/** @var $decrypted DecryptionResult */
	$decrypted = $otp->decrypt($yubikey);
	
	// At this point, we check the counters, private / public id etc to determine that OTP is indeed valid
	// An OTP is valid if:
	// 1) we manage to decrypt it using the key information
	// 2) CRC is verified
	// 3) Private ID in OTP is equal to Private ID in Yubikey
	// 4) Use counter in OTP is _greater_ than the one in Yubikey object
	// 5) Session counter in OTP is _equal_ or _greater_ than the one in Yubikey object	
	// There's a utility method available that performs all mentioned checks. If something fails, it will throw an instance of
	// Buckhill\Yubico\Exceptions\YubikeyValidationException
	
	$decrypted->validateAgainst($yubikey);	
}
catch(\Exception $e)
{
	echo $e->getMessage();
}

``` 
 
 
### Yubikey OATH 
 
 Usage:
 
```
<?php

use Buckhill\Yubico\Modes\OATH\OTP;
use Buckhill\Yubico\Modes\OATH\YubikeyOATH;

$otp = new OTP('ubnu69301011717622');

$yubikey = YubikeyOATH::fromArray([
	'identifier' => 'ubnu69 30 10 11',
	'secret' => 'd8 70 1f 49 98 b2 29 d7 c2 25 80 de 82 a2 27 e6 6c 9c cd 1f',
	'counter' => 0
]);

try
{
	$result = $yubikey->verify($otp);
	
	if(false === $result)
	{
		echo 'Invalid OTP';
	}
	else	
	{
		echo 'OTP verified, result found at position: ' . $result;
	}
}
catch(\Exception $e)
{
	echo $e->getMessage();
}

```

