Debian Codegraph @

vendor/jakobo/hotp-php/src/HOTP.php

raw
1 <?php
2
3 namespace jakobo\HOTP;
4
5 /**
6 * HOTP Class
7 * Based on the work of OAuth, and the sample implementation of HMAC OTP
8 * http://tools.ietf.org/html/draft-mraihi-oath-hmac-otp-04#appendix-D
9 * @author Jakob Heuser (firstname)@felocity.com
10 * @copyright 2011-2020
11 * @license BSD-3-Clause
12 * @version 1.0
13 */
14 class HOTP {
15 /**
16 * Generate a HOTP key based on a counter value (event based HOTP)
17 * @param string $key the key to use for hashing
18 * @param int $counter the number of attempts represented in this hashing
19 * @return HOTPResult a HOTP Result which can be truncated or output
20 */
21 public static function generateByCounter( string $key, int $counter ): HOTPResult {
22 // the counter value can be more than one byte long,
23 // so we need to pack it down properly.
24 $cur_counter = [ 0, 0, 0, 0, 0, 0, 0, 0 ];
25 for ( $i = 7; $i >= 0; $i-- ) {
26 $cur_counter[$i] = pack( 'C*', $counter );
27 $counter = $counter >> 8;
28 }
29
30 $bin_counter = implode( $cur_counter );
31
32 // Pad to 8 chars
33 if ( strlen( $bin_counter ) < 8 ) {
34 $bin_counter = str_repeat(
35 chr(0 ),
36 8 - strlen( $bin_counter )
37 ) . $bin_counter;
38 }
39
40 // HMAC
41 $hash = hash_hmac( 'sha1', $bin_counter, $key );
42
43 return new HOTPResult( $hash );
44 }
45
46 /**
47 * Generate a HOTP key based on a timestamp and window size
48 * @param string $key the key to use for hashing
49 * @param int $window the size of the window a key is valid for in seconds
50 * @param int|false $timestamp a timestamp to calculate for, defaults to time()
51 * @return HOTPResult a HOTP Result which can be truncated or output
52 */
53 public static function generateByTime( string $key, int $window, $timestamp = false ): HOTPResult {
54 if ( !$timestamp && $timestamp !== 0 ) {
55 // @codeCoverageIgnoreStart
56 $timestamp = self::getTime();
57 // @codeCoverageIgnoreEnd
58 }
59
60 $counter = intval( $timestamp / $window) ;
61
62 return self::generateByCounter( $key, $counter );
63 }
64
65 /**
66 * Generate a HOTP key collection based on a timestamp and window size
67 * all keys that could exist between a start and end time will be included
68 * in the returned array
69 * @param string $key the key to use for hashing
70 * @param int $window the size of the window a key is valid for in seconds
71 * @param int $min the minimum window to accept before $timestamp
72 * @param int $max the maximum window to accept after $timestamp
73 * @param int|false $timestamp a timestamp to calculate for, defaults to time()
74 * @return array of HOTPResult
75 */
76 public static function generateByTimeWindow( string $key, int $window, int $min = -1, int $max = 1, $timestamp = false ): array {
77 if ( !$timestamp && $timestamp !== 0 ) {
78 // @codeCoverageIgnoreStart
79 $timestamp = self::getTime();
80 // @codeCoverageIgnoreEnd
81 }
82
83 $counter = intval( $timestamp / $window );
84 $window = range( $min, $max );
85
86 $out = [];
87 foreach ( $window as $value ) {
88 $shift_counter = $counter + $value;
89 $out[$shift_counter] = self::generateByCounter( $key, $shift_counter );
90 }
91
92 return $out;
93 }
94
95 /**
96 * Gets the current time
97 * Ensures we are operating in UTC for the entire framework
98 * Restores the timezone on exit.
99 * @return int the current time
100 * @codeCoverageIgnore
101 */
102 public static function getTime(): int {
103 // PHP's time is always UTC
104 return time();
105 }
106 }