@@ -37,7 +37,7 @@ discard block |
||
| 37 | 37 | $keyBytes, |
| 38 | 38 | $text) |
| 39 | 39 | { |
| 40 | - $hash = hash_hmac ($crypto, $text, $keyBytes); |
|
| 40 | + $hash = hash_hmac($crypto, $text, $keyBytes); |
|
| 41 | 41 | return $hash; |
| 42 | 42 | } |
| 43 | 43 | |
@@ -48,7 +48,7 @@ discard block |
||
| 48 | 48 | * |
| 49 | 49 | * @return String a string with raw bytes |
| 50 | 50 | */ |
| 51 | - private static function _hexStr2Bytes($hex){ |
|
| 51 | + private static function _hexStr2Bytes($hex) { |
|
| 52 | 52 | return pack("H*", $hex); |
| 53 | 53 | } |
| 54 | 54 | |
@@ -97,95 +97,95 @@ discard block |
||
| 97 | 97 | $cryptoFunction = $components[1]; |
| 98 | 98 | $dataInput = strtolower($components[2]); // lower here so we can do case insensitive comparisons |
| 99 | 99 | |
| 100 | - if(stripos($cryptoFunction, "sha1")!==false) |
|
| 100 | + if (stripos($cryptoFunction, "sha1") !== false) |
|
| 101 | 101 | $crypto = "sha1"; |
| 102 | - if(stripos($cryptoFunction, "sha256")!==false) |
|
| 102 | + if (stripos($cryptoFunction, "sha256") !== false) |
|
| 103 | 103 | $crypto = "sha256"; |
| 104 | - if(stripos($cryptoFunction, "sha512")!==false) |
|
| 104 | + if (stripos($cryptoFunction, "sha512") !== false) |
|
| 105 | 105 | $crypto = "sha512"; |
| 106 | 106 | |
| 107 | - $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-")+1); |
|
| 107 | + $codeDigits = substr($cryptoFunction, strrpos($cryptoFunction, "-") + 1); |
|
| 108 | 108 | |
| 109 | 109 | // The size of the byte array message to be encrypted |
| 110 | 110 | // Counter |
| 111 | - if($dataInput[0] == "c" ) { |
|
| 111 | + if ($dataInput[0] == "c") { |
|
| 112 | 112 | // Fix the length of the HEX string |
| 113 | - while(strlen($counter) < 16) |
|
| 114 | - $counter = "0" . $counter; |
|
| 115 | - $counterLength=8; |
|
| 113 | + while (strlen($counter) < 16) |
|
| 114 | + $counter = "0".$counter; |
|
| 115 | + $counterLength = 8; |
|
| 116 | 116 | } |
| 117 | 117 | // Question |
| 118 | - if($dataInput[0] == "q" || |
|
| 119 | - stripos($dataInput, "-q")!==false) { |
|
| 120 | - while(strlen($question) < 256) |
|
| 121 | - $question = $question . "0"; |
|
| 122 | - $questionLength=128; |
|
| 118 | + if ($dataInput[0] == "q" || |
|
| 119 | + stripos($dataInput, "-q") !== false) { |
|
| 120 | + while (strlen($question) < 256) |
|
| 121 | + $question = $question."0"; |
|
| 122 | + $questionLength = 128; |
|
| 123 | 123 | } |
| 124 | 124 | |
| 125 | 125 | // Password |
| 126 | - if(stripos($dataInput, "psha1")!==false) { |
|
| 127 | - while(strlen($password) < 40) |
|
| 128 | - $password = "0" . $password; |
|
| 129 | - $passwordLength=20; |
|
| 126 | + if (stripos($dataInput, "psha1") !== false) { |
|
| 127 | + while (strlen($password) < 40) |
|
| 128 | + $password = "0".$password; |
|
| 129 | + $passwordLength = 20; |
|
| 130 | 130 | } |
| 131 | 131 | |
| 132 | - if(stripos($dataInput, "psha256")!==false) { |
|
| 133 | - while(strlen($password) < 64) |
|
| 134 | - $password = "0" . $password; |
|
| 135 | - $passwordLength=32; |
|
| 132 | + if (stripos($dataInput, "psha256") !== false) { |
|
| 133 | + while (strlen($password) < 64) |
|
| 134 | + $password = "0".$password; |
|
| 135 | + $passwordLength = 32; |
|
| 136 | 136 | } |
| 137 | 137 | |
| 138 | - if(stripos($dataInput, "psha512")!==false) { |
|
| 139 | - while(strlen($password) < 128) |
|
| 140 | - $password = "0" . $password; |
|
| 141 | - $passwordLength=64; |
|
| 138 | + if (stripos($dataInput, "psha512") !== false) { |
|
| 139 | + while (strlen($password) < 128) |
|
| 140 | + $password = "0".$password; |
|
| 141 | + $passwordLength = 64; |
|
| 142 | 142 | } |
| 143 | 143 | |
| 144 | 144 | // sessionInformation |
| 145 | - if(stripos($dataInput, "s064") !==false) { |
|
| 146 | - while(strlen($sessionInformation) < 128) |
|
| 147 | - $sessionInformation = "0" . $sessionInformation; |
|
| 145 | + if (stripos($dataInput, "s064") !== false) { |
|
| 146 | + while (strlen($sessionInformation) < 128) |
|
| 147 | + $sessionInformation = "0".$sessionInformation; |
|
| 148 | 148 | |
| 149 | - $sessionInformationLength=64; |
|
| 150 | - } else if(stripos($dataInput, "s128") !==false) { |
|
| 151 | - while(strlen($sessionInformation) < 256) |
|
| 152 | - $sessionInformation = "0" . $sessionInformation; |
|
| 149 | + $sessionInformationLength = 64; |
|
| 150 | + } else if (stripos($dataInput, "s128") !== false) { |
|
| 151 | + while (strlen($sessionInformation) < 256) |
|
| 152 | + $sessionInformation = "0".$sessionInformation; |
|
| 153 | 153 | |
| 154 | - $sessionInformationLength=128; |
|
| 155 | - } else if(stripos($dataInput, "s256") !==false) { |
|
| 156 | - while(strlen($sessionInformation) < 512) |
|
| 157 | - $sessionInformation = "0" . $sessionInformation; |
|
| 154 | + $sessionInformationLength = 128; |
|
| 155 | + } else if (stripos($dataInput, "s256") !== false) { |
|
| 156 | + while (strlen($sessionInformation) < 512) |
|
| 157 | + $sessionInformation = "0".$sessionInformation; |
|
| 158 | 158 | |
| 159 | - $sessionInformationLength=256; |
|
| 160 | - } else if(stripos($dataInput, "s512") !==false) { |
|
| 161 | - while(strlen($sessionInformation) < 128) |
|
| 162 | - $sessionInformation = "0" . $sessionInformation; |
|
| 159 | + $sessionInformationLength = 256; |
|
| 160 | + } else if (stripos($dataInput, "s512") !== false) { |
|
| 161 | + while (strlen($sessionInformation) < 128) |
|
| 162 | + $sessionInformation = "0".$sessionInformation; |
|
| 163 | 163 | |
| 164 | - $sessionInformationLength=64; |
|
| 165 | - } else if (stripos($dataInput, "s") !== false ) { |
|
| 164 | + $sessionInformationLength = 64; |
|
| 165 | + } else if (stripos($dataInput, "s") !== false) { |
|
| 166 | 166 | // deviation from spec. Officially 's' without a length indicator is not in the reference implementation. |
| 167 | 167 | // RFC is ambigious. However we have supported this in Tiqr since day 1, so we continue to support it. |
| 168 | - while(strlen($sessionInformation) < 128) |
|
| 169 | - $sessionInformation = "0" . $sessionInformation; |
|
| 168 | + while (strlen($sessionInformation) < 128) |
|
| 169 | + $sessionInformation = "0".$sessionInformation; |
|
| 170 | 170 | |
| 171 | - $sessionInformationLength=64; |
|
| 171 | + $sessionInformationLength = 64; |
|
| 172 | 172 | } |
| 173 | 173 | |
| 174 | 174 | |
| 175 | 175 | |
| 176 | 176 | // TimeStamp |
| 177 | - if($dataInput[0] == "t" || |
|
| 177 | + if ($dataInput[0] == "t" || |
|
| 178 | 178 | stripos($dataInput, "-t") !== false) { |
| 179 | - while(strlen($timeStamp) < 16) |
|
| 180 | - $timeStamp = "0" . $timeStamp; |
|
| 181 | - $timeStampLength=8; |
|
| 179 | + while (strlen($timeStamp) < 16) |
|
| 180 | + $timeStamp = "0".$timeStamp; |
|
| 181 | + $timeStampLength = 8; |
|
| 182 | 182 | } |
| 183 | 183 | |
| 184 | 184 | // Put the bytes of "ocraSuite" parameters into the message |
| 185 | 185 | |
| 186 | - $msg = array_fill(0,$ocraSuiteLength+$counterLength+$questionLength+$passwordLength+$sessionInformationLength+$timeStampLength+1, 0); |
|
| 186 | + $msg = array_fill(0, $ocraSuiteLength + $counterLength + $questionLength + $passwordLength + $sessionInformationLength + $timeStampLength + 1, 0); |
|
| 187 | 187 | |
| 188 | - for($i=0;$i<strlen($ocraSuite);$i++) { |
|
| 188 | + for ($i = 0; $i < strlen($ocraSuite); $i++) { |
|
| 189 | 189 | $msg[$i] = $ocraSuite[$i]; |
| 190 | 190 | } |
| 191 | 191 | |
@@ -194,9 +194,9 @@ discard block |
||
| 194 | 194 | |
| 195 | 195 | // Put the bytes of "Counter" to the message |
| 196 | 196 | // Input is HEX encoded |
| 197 | - if($counterLength > 0 ) { |
|
| 197 | + if ($counterLength > 0) { |
|
| 198 | 198 | $bArray = self::_hexStr2Bytes($counter); |
| 199 | - for ($i=0;$i<strlen($bArray);$i++) { |
|
| 199 | + for ($i = 0; $i < strlen($bArray); $i++) { |
|
| 200 | 200 | $msg [$i + $ocraSuiteLength + 1] = $bArray[$i]; |
| 201 | 201 | } |
| 202 | 202 | } |
@@ -204,36 +204,36 @@ discard block |
||
| 204 | 204 | |
| 205 | 205 | // Put the bytes of "question" to the message |
| 206 | 206 | // Input is text encoded |
| 207 | - if($questionLength > 0 ) { |
|
| 207 | + if ($questionLength > 0) { |
|
| 208 | 208 | $bArray = self::_hexStr2Bytes($question); |
| 209 | - for ($i=0;$i<strlen($bArray);$i++) { |
|
| 209 | + for ($i = 0; $i < strlen($bArray); $i++) { |
|
| 210 | 210 | $msg [$i + $ocraSuiteLength + 1 + $counterLength] = $bArray[$i]; |
| 211 | 211 | } |
| 212 | 212 | } |
| 213 | 213 | |
| 214 | 214 | // Put the bytes of "password" to the message |
| 215 | 215 | // Input is HEX encoded |
| 216 | - if($passwordLength > 0){ |
|
| 216 | + if ($passwordLength > 0) { |
|
| 217 | 217 | $bArray = self::_hexStr2Bytes($password); |
| 218 | - for ($i=0;$i<strlen($bArray);$i++) { |
|
| 218 | + for ($i = 0; $i < strlen($bArray); $i++) { |
|
| 219 | 219 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength] = $bArray[$i]; |
| 220 | 220 | } |
| 221 | 221 | } |
| 222 | 222 | |
| 223 | 223 | // Put the bytes of "sessionInformation" to the message |
| 224 | 224 | // Input is text encoded |
| 225 | - if($sessionInformationLength > 0 ){ |
|
| 225 | + if ($sessionInformationLength > 0) { |
|
| 226 | 226 | $bArray = self::_hexStr2Bytes($sessionInformation); |
| 227 | - for ($i=0;$i<strlen($bArray);$i++) { |
|
| 227 | + for ($i = 0; $i < strlen($bArray); $i++) { |
|
| 228 | 228 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength] = $bArray[$i]; |
| 229 | 229 | } |
| 230 | 230 | } |
| 231 | 231 | |
| 232 | 232 | // Put the bytes of "time" to the message |
| 233 | 233 | // Input is text value of minutes |
| 234 | - if($timeStampLength > 0){ |
|
| 234 | + if ($timeStampLength > 0) { |
|
| 235 | 235 | $bArray = self::_hexStr2Bytes($timeStamp); |
| 236 | - for ($i=0;$i<strlen($bArray);$i++) { |
|
| 236 | + for ($i = 0; $i < strlen($bArray); $i++) { |
|
| 237 | 237 | $msg [$i + $ocraSuiteLength + 1 + $counterLength + $questionLength + $passwordLength + $sessionInformationLength] = $bArray[$i]; |
| 238 | 238 | } |
| 239 | 239 | } |
@@ -260,23 +260,23 @@ discard block |
||
| 260 | 260 | static function _oath_truncate($hash, $length = 6) |
| 261 | 261 | { |
| 262 | 262 | // Convert to dec |
| 263 | - foreach(str_split($hash,2) as $hex) |
|
| 263 | + foreach (str_split($hash, 2) as $hex) |
|
| 264 | 264 | { |
| 265 | - $hmac_result[]=hexdec($hex); |
|
| 265 | + $hmac_result[] = hexdec($hex); |
|
| 266 | 266 | } |
| 267 | 267 | |
| 268 | 268 | // Find offset |
| 269 | 269 | $offset = $hmac_result[count($hmac_result) - 1] & 0xf; |
| 270 | 270 | |
| 271 | 271 | $v = strval( |
| 272 | - (($hmac_result[$offset+0] & 0x7f) << 24 ) | |
|
| 273 | - (($hmac_result[$offset+1] & 0xff) << 16 ) | |
|
| 274 | - (($hmac_result[$offset+2] & 0xff) << 8 ) | |
|
| 275 | - ($hmac_result[$offset+3] & 0xff) |
|
| 272 | + (($hmac_result[$offset + 0] & 0x7f) << 24) | |
|
| 273 | + (($hmac_result[$offset + 1] & 0xff) << 16) | |
|
| 274 | + (($hmac_result[$offset + 2] & 0xff) << 8) | |
|
| 275 | + ($hmac_result[$offset + 3] & 0xff) |
|
| 276 | 276 | ); |
| 277 | 277 | |
| 278 | 278 | // Prefix truncated string with 0's to ensure it always has the required length |
| 279 | - $v=str_pad($v, $length, "0", STR_PAD_LEFT); |
|
| 279 | + $v = str_pad($v, $length, "0", STR_PAD_LEFT); |
|
| 280 | 280 | |
| 281 | 281 | $v = substr($v, strlen($v) - $length); |
| 282 | 282 | return $v; |