3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * SMS Signin Gateway - HMAC class * @author adrian7 (adrian@studentmoneysaver.co.uk) * @version 1.1 */ /** * Generates/Validates HMAC signatures * Class HMAC * @link http://signin.studentmoneysaver.co.uk/docs/#API * @package App\Library */ class HMAC{ /** * Timezone */ const TZ = 'UTC'; /** * Cypher/algorithm to use * @see hash_algos() */ const CYPHER = 'sha256'; /** * Time frame in seconds, in which a message is considered valid */ const TIMEFRAME = 300; /** * Internal time format */ const TIME_FORMAT = 'Y-m-d H:i:s'; /** * Verifies a HMAC signature * @param $data * @param $signature * @param $privateKey * @param $timestamp * * @return bool */ public static function verify($data, $signature, $privateKey, $timestamp){ $now = self::timestamp(self::TZ); $tmin = ( $now - ( self::TIMEFRAME/2 ) ); $tmax = ( $now + ( self::TIMEFRAME/2 ) ); if( ( $timestamp < $tmin ) or ( $timestamp > $tmax ) ) return false; //out of time range $data = strval( $data ); $computed_sig = self::signature($data, $privateKey, $timestamp); return $signature == $computed_sig; } /** * Generates a HMAC signature * @param $data * @param $privateKey * @param null $timestamp * * @return string */ public static function signature($data, $privateKey, $timestamp=null){ $timestamp = empty($timestamp) ? self::timestamp(self::TZ) : intval($timestamp); $data = strval( $data ); $sig = base64_encode( hash_hmac(self::CYPHER, $data, $privateKey . '::' . date(self::TIME_FORMAT, $timestamp), true) ); echo "Time: " . $timestamp; return $sig; } /** * Generates timestamp based on timezone * @param string $tz * * @return int */ public static function timestamp($tz='UTC'){ $tz = new \DateTimeZone($tz); return date_create(NULL, $tz)->getTimestamp(); } /** * Validates a hash algorithm * @param $algo * @see hash_algos() * @return bool */ public static function isValidHashAlgo($algo){ $algos = hash_algos(); return in_array($algo, $algos); } } $timestamp = empty($timestamp) ? HMAC::timestamp(HMAC::TZ) : intval($timestamp); $url = "https://signin.studentmoneysaver.co.uk/api/?onSuccess=https%3A%2F%2Fwww.google.si%2Fsearch%3Fq%3Dsuccess&onFail=https%3A%2F%2Fwww.google.si%2Fsearch%3Fq%3Dfail&apikey=test-Yr82f2DowCdxRumRwAD8r66KMFF4GWDm&timestamp=$timestamp"; echo "Signature: " . HMAC::signature( $url, 'NP8T2NY2SR0XTNZ5', $timestamp);
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 2, Position 2 = 7
Branch analysis from position: 2
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 7
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/huiij
function name:  (null)
number of ops:  21
compiled vars:  !0 = $timestamp, !1 = $url
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  106     0  E >   ISSET_ISEMPTY_CV                                         !0
          1      > JMPZ                                                     ~2, ->7
          2    >   INIT_STATIC_METHOD_CALL                                  'HMAC', 'timestamp'
          3        SEND_VAL                                                 'UTC'
          4        DO_FCALL                                      0  $3      
          5        QM_ASSIGN                                        ~4      $3
          6      > JMP                                                      ->9
          7    >   CAST                                          4  ~5      !0
          8        QM_ASSIGN                                        ~4      ~5
          9    >   ASSIGN                                                   !0, ~4
  107    10        NOP                                                      
         11        FAST_CONCAT                                      ~7      'https%3A%2F%2Fsignin.studentmoneysaver.co.uk%2Fapi%2F%3FonSuccess%3Dhttps%253A%252F%252Fwww.google.si%252Fsearch%253Fq%253Dsuccess%26onFail%3Dhttps%253A%252F%252Fwww.google.si%252Fsearch%253Fq%253Dfail%26apikey%3Dtest-Yr82f2DowCdxRumRwAD8r66KMFF4GWDm%26timestamp%3D', !0
         12        ASSIGN                                                   !1, ~7
  108    13        INIT_STATIC_METHOD_CALL                                  'HMAC', 'signature'
         14        SEND_VAR                                                 !1
         15        SEND_VAL                                                 'NP8T2NY2SR0XTNZ5'
         16        SEND_VAR                                                 !0
         17        DO_FCALL                                      0  $9      
         18        CONCAT                                           ~10     'Signature%3A+', $9
         19        ECHO                                                     ~10
         20      > RETURN                                                   1

Class HMAC:
Function verify:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 47) Position 1 = 14, Position 2 = 16
Branch analysis from position: 14
2 jumps found. (Code = 43) Position 1 = 17, Position 2 = 18
Branch analysis from position: 17
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 16
filename:       /in/huiij
function name:  verify
number of ops:  29
compiled vars:  !0 = $data, !1 = $signature, !2 = $privateKey, !3 = $timestamp, !4 = $now, !5 = $tmin, !6 = $tmax, !7 = $computed_sig
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   47     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
          3        RECV                                             !3      
   49     4        INIT_STATIC_METHOD_CALL                                  'timestamp'
          5        SEND_VAL_EX                                              'UTC'
          6        DO_FCALL                                      0  $8      
          7        ASSIGN                                                   !4, $8
   51     8        SUB                                              ~10     !4, 150
          9        ASSIGN                                                   !5, ~10
   52    10        ADD                                              ~12     !4, 150
         11        ASSIGN                                                   !6, ~12
   54    12        IS_SMALLER                                       ~14     !3, !5
         13      > JMPNZ_EX                                         ~14     ~14, ->16
         14    >   IS_SMALLER                                       ~15     !6, !3
         15        BOOL                                             ~14     ~15
         16    > > JMPZ                                                     ~14, ->18
   55    17    > > RETURN                                                   <false>
   57    18    >   CAST                                          6  ~16     !0
         19        ASSIGN                                                   !0, ~16
   58    20        INIT_STATIC_METHOD_CALL                                  'signature'
         21        SEND_VAR_EX                                              !0
         22        SEND_VAR_EX                                              !2
         23        SEND_VAR_EX                                              !3
         24        DO_FCALL                                      0  $18     
         25        ASSIGN                                                   !7, $18
   60    26        IS_EQUAL                                         ~20     !1, !7
         27      > RETURN                                                   ~20
   62    28*     > RETURN                                                   null

End of function verify

Function signature:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 5, Position 2 = 10
Branch analysis from position: 5
1 jumps found. (Code = 42) Position 1 = 12
Branch analysis from position: 12
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 10
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/huiij
function name:  signature
number of ops:  35
compiled vars:  !0 = $data, !1 = $privateKey, !2 = $timestamp, !3 = $sig
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   72     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV_INIT                                        !2      null
   74     3        ISSET_ISEMPTY_CV                                         !2
          4      > JMPZ                                                     ~4, ->10
          5    >   INIT_STATIC_METHOD_CALL                                  'timestamp'
          6        SEND_VAL_EX                                              'UTC'
          7        DO_FCALL                                      0  $5      
          8        QM_ASSIGN                                        ~6      $5
          9      > JMP                                                      ->12
         10    >   CAST                                          4  ~7      !2
         11        QM_ASSIGN                                        ~6      ~7
         12    >   ASSIGN                                                   !2, ~6
   75    13        CAST                                          6  ~9      !0
         14        ASSIGN                                                   !0, ~9
   77    15        INIT_FCALL                                               'base64_encode'
         16        INIT_FCALL                                               'hash_hmac'
         17        SEND_VAL                                                 'sha256'
         18        SEND_VAR                                                 !0
         19        CONCAT                                           ~11     !1, '%3A%3A'
         20        INIT_FCALL                                               'date'
         21        SEND_VAL                                                 'Y-m-d+H%3Ai%3As'
         22        SEND_VAR                                                 !2
         23        DO_ICALL                                         $12     
         24        CONCAT                                           ~13     ~11, $12
         25        SEND_VAL                                                 ~13
         26        SEND_VAL                                                 <true>
         27        DO_ICALL                                         $14     
         28        SEND_VAR                                                 $14
         29        DO_ICALL                                         $15     
         30        ASSIGN                                                   !3, $15
   78    31        CONCAT                                           ~17     'Time%3A+', !2
         32        ECHO                                                     ~17
   79    33      > RETURN                                                   !3
   81    34*     > RETURN                                                   null

End of function signature

Function timestamp:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/huiij
function name:  timestamp
number of ops:  13
compiled vars:  !0 = $tz
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   90     0  E >   RECV_INIT                                        !0      'UTC'
   91     1        NEW                                              $1      'DateTimeZone'
          2        SEND_VAR_EX                                              !0
          3        DO_FCALL                                      0          
          4        ASSIGN                                                   !0, $1
          5        INIT_FCALL                                               'date_create'
          6        SEND_VAL                                                 null
          7        SEND_VAR                                                 !0
          8        DO_ICALL                                         $4      
          9        INIT_METHOD_CALL                                         $4, 'getTimestamp'
         10        DO_FCALL                                      0  $5      
         11      > RETURN                                                   $5
   92    12*     > RETURN                                                   null

End of function timestamp

Function isvalidhashalgo:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/huiij
function name:  isValidHashAlgo
number of ops:  10
compiled vars:  !0 = $algo, !1 = $algos
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  100     0  E >   RECV                                             !0      
  101     1        INIT_FCALL                                               'hash_algos'
          2        DO_ICALL                                         $2      
          3        ASSIGN                                                   !1, $2
          4        INIT_FCALL                                               'in_array'
          5        SEND_VAR                                                 !0
          6        SEND_VAR                                                 !1
          7        DO_ICALL                                         $4      
          8      > RETURN                                                   $4
  102     9*     > RETURN                                                   null

End of function isvalidhashalgo

End of class HMAC.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
186.3 ms | 1447 KiB | 19 Q