= 0; $i-- ) { // C for unsigned char, * for repeating to the end of the input data $cur_counter[ $i ] = pack( 'C*', $counter ); $counter = $counter >> 8; } $binary = implode( $cur_counter ); // Pad to 8 characters str_pad( $binary, 8, chr( 0 ), STR_PAD_LEFT ); return hash_hmac( 'sha1', $binary, $key ); } /** * Truncate * * @param string $hash * @param integer $length * @return boolean */ private static function oath_truncate( $hash, $length = 6 ) { $hashcharacters = str_split( $hash, 2 ); for ( $j = 0; $j < count( $hashcharacters ); $j++ ) { $hmac_result[] = hexdec( $hashcharacters[ $j ] ); } $offset = $hmac_result[19] & 0xf; return ( ( ( $hmac_result[ $offset + 0 ] & 0x7f ) << 24 ) | ( ( $hmac_result[ $offset + 1 ] & 0xff ) << 16 ) | ( ( $hmac_result[ $offset + 2 ] & 0xff ) << 8 ) | ( $hmac_result[ $offset + 3 ] & 0xff ) ) % pow( 10, $length ); } }