source-function-F_decodeBase32

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
Overview

Classes

Interfaces

Exceptions

Functions

  1: <?php
  2: //============================================================+
  3: // File name   : tce_functions_otp.php
  4: // Begin       : 2012-01-09
  5: // Last Update : 2012-11-22
  6: //
  7: // Description : Functions for One Time Password (OTP).
  8: //
  9: // Author: Nicola Asuni
 10: //
 11: // (c) Copyright:
 12: //               Nicola Asuni
 13: //               Tecnick.com LTD
 14: //               www.tecnick.com
 15: //               info@tecnick.com
 16: //
 17: // License:
 18: //    Copyright (C) 2004-2012  Nicola Asuni - Tecnick.com LTD
 19: //    See LICENSE.TXT file for more information.
 20: //============================================================+
 21: 
 22: /**
 23:  * @file
 24:  * Functions for One Time Password (OTP).
 25:  * @package com.tecnick.tcexam.shared
 26:  * @author Nicola Asuni
 27:  * @since 2012-01-09
 28:  */
 29: 
 30: /**
 31:  * Return a random One Time Password Secret Key (Base32 encoded).
 32:  * @return Base32 encoded key.
 33:  */
 34: function F_getRandomOTPkey()
 35: {
 36:     // dictionary
 37:     $dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
 38:     $key = '';
 39:     // generate a 16 char random secret key
 40:     for ($i = 0; $i < 16; ++$i) {
 41:         $key .= $dict[(rand(0, 31))];
 42:     }
 43:     return $key;
 44: }
 45: 
 46: /**
 47:  * Decode a Base32 encoded string.
 48:  * @param $code (string) Base32 code to be decoded.
 49:  * @return Decoded key.
 50:  */
 51: function F_decodeBase32($code)
 52: {
 53:     // dictionary
 54:     $dict = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567';
 55:     // remove invalid chars
 56:     $code = preg_replace('/[^'.$dict.']+/', '', $code);
 57:     $n = 0;
 58:     $j = 0;
 59:     $bin = '';
 60:     $len = strlen($code);
 61:     // for each char on code
 62:     for ($c = 0; $c < $len; ++$c) {
 63:         $n = ($n << 5);
 64:         $n = ($n + strpos($dict, $code[$c]));
 65:         $j = ($j + 5);
 66:         if ($j >= 8) {
 67:             $j = ($j - 8);
 68:             $bin .= chr(($n & (0xFF << $j)) >> $j);
 69:         }
 70:     }
 71:     return $bin;
 72: }
 73: 
 74: /**
 75:  * Get a One Time Password for the specified secret key.
 76:  * @param $otpkey (string) One Time Password secret key.
 77:  * @param $mtime (int) Reference time in microseconds.
 78:  * @return OTP
 79:  */
 80: function F_getOTP($otpkey, $mtime = 0)
 81: {
 82:     // get binary key
 83:     $binkey = F_decodeBase32($otpkey);
 84:     // get the current timestamp (the one time password changes every 30 seconds)
 85:     if ($mtime == 0) {
 86:         $mtime = microtime(true);
 87:     }
 88:     $time = floor($mtime / 30);
 89:     // convert timestamp into a binary string of 8 bytes
 90:     $bintime = pack('N*', 0).pack('N*', $time);
 91:     // calculate the SHA1 hash
 92:     $hash = hash_hmac('sha1', $bintime, $binkey, true);
 93:     // get offset
 94:     $offset = (ord($hash[19]) & 0xf);
 95:     // one time password
 96:     $otp = ((((ord($hash[($offset + 0)]) & 0x7f) << 24 )
 97:         | ((ord($hash[($offset + 1)]) & 0xff) << 16 )
 98:         | ((ord($hash[($offset + 2)]) & 0xff) << 8 )
 99:         | (ord($hash[($offset + 3)]) & 0xff)) % pow(10, 6));
100:     return $otp;
101: }
102: 
103: //============================================================+
104: // END OF FILE
105: //============================================================+
106: 
 

© 2004-2018 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy