@@ -18,7 +18,111 @@ composer require lorddashme/php-two-factor-auth
1818
1919## Usage
2020
21- - @TODO
21+ ### HOTP
22+
23+ - To generate HMAC-based One-Time Password algorithm:
24+
25+ ``` php
26+ <?php
27+
28+ require __DIR__ . '/vendor/autoload.php';
29+
30+ use LordDashMe\TwoFactorAuth\RFC4226\HOTP;
31+ use LordDashMe\TwoFactorAuth\Utility\Base32;
32+
33+ $hotp = new HOTP(Base32::encode('P@ssw0rd!'));
34+ $hotp->setLength(6)
35+ ->prepare()
36+ ->generate();
37+
38+ echo $hotp->get(); // 444555
39+ ```
40+
41+ - To verify the generated HOTP:
42+
43+ ``` php
44+ <?php
45+
46+ require __DIR__ . '/vendor/autoload.php';
47+
48+ use LordDashMe\TwoFactorAuth\RFC4226\HOTP;
49+ use LordDashMe\TwoFactorAuth\Utility\Base32;
50+
51+ $hotp = new HOTP(Base32::encode('P@ssw0rd!'));
52+ $hotp->setLength(6)
53+ ->prepare();
54+
55+ echo $hotp->verify('444555'); // true
56+ ```
57+
58+ ### TOTP
59+
60+ - To generate Time-Based One-Time Password algorithm:
61+
62+ ``` php
63+ <?php
64+
65+ require __DIR__ . '/vendor/autoload.php';
66+
67+ use LordDashMe\TwoFactorAuth\RFC6238\TOTP;
68+ use LordDashMe\TwoFactorAuth\Utility\Base32;
69+
70+ $totp = new TOTP(Base32::encode('P@ssw0rd!'));
71+ $totp->setTimeRemainingInSeconds(30)
72+ ->setTimeAdjustments(10)
73+ ->setLength(6)
74+ ->prepare()
75+ ->generate();
76+
77+ echo $totp->get(); // 552344
78+ ```
79+
80+ - To verify the generated TOTP:
81+
82+ ``` php
83+ <?php
84+
85+ require __DIR__ . '/vendor/autoload.php';
86+
87+ use LordDashMe\TwoFactorAuth\RFC6238\TOTP;
88+ use LordDashMe\TwoFactorAuth\Utility\Base32;
89+
90+ $totp = new TOTP(Base32::encode('P@ssw0rd!'));
91+ $totp->setTimeRemainingInSeconds(30)
92+ ->setTimeAdjustments(10)
93+ ->setLength(6)
94+ ->prepare();
95+
96+ echo $totp->verify('552344'); // true
97+ ```
98+
99+ ### Google Authenticator Barcode Generation
100+
101+ - To generate a barcode image that will use by the Google Authenticator mobile app:
102+
103+ ``` php
104+ <?php
105+
106+ require __DIR__ . '/vendor/autoload.php';
107+
108+ use LordDashMe\TwoFactorAuth\Utility\Base32;
109+ use LordDashMe\TwoFactorAuth\GoogleAuthenticator\BarcodeURL;
110+ use LordDashMe\TwoFactorAuth\GoogleAuthenticator\TOTPFormat;
111+
112+ $secret = Base32::encode('P@ssw0rd!', false);
113+ $accountUser = 'reyesjoshuaclifford@gmail.com';
114+ $issuer = 'TwoFactorAuth';
115+ $digits = 6;
116+ $period = 30;
117+
118+ $totpFormat = new TOTPFormat($period);
119+ $barcodeURL = new BarcodeURL($secret, $accountUser, $issuer, $totpFormat);
120+ $barcodeURL->setAlgorithm('SHA1')
121+ ->setDigits($digits)
122+ ->build();
123+
124+ echo $barcodeURL->get(); // https://chart.googleapis.com/chart?chs=200x200&chld=M|0&cht=qr&chl=otpauth://totp/TwoFactorAuth:reyesjoshuaclifford@gmail.com?secret=KBAHG43XGBZGIII&algorithm=SHA1&digits=6&period=30&issuer=TwoFactorAuth
125+ ```
22126
23127## Other Reference
24128
0 commit comments