I am trying to write verification code in PhP following these steps...
The verification code is calculated by:
- Verification code = integer(SHA256(the hash)[-2:-1]) mod 10000
- Calculate SHA256 from the hash, extract 2 rightmost bytes from the result, interpret them as a big-endian unsigned integer and take the last 4 digits in decimal form for display. SHA256 is always used here.
- Please mind that hash is a real hash byte value, not the Base64 form or the hexadecimal representation.
So far I have this code, but it's not working like I was hoping for:
function calculateVcode($hashString) {
$md = hash_init('sha256');
$hash = base64_decode($hashString);
hash_update($md, $hash);
$hash = hash_final($md, true);
$last = substr($hash, -2);
$code = bindec($last);
return substr(strval($code), -4);
}
$hashString = "Demo string for Authentication";
$md5Hash = md5($hashString);
$result = calculateVcode($md5Hash);`
Any ideas how to get this working or am I totally on the wrong path ?
Result could be like: Text used to generate a hash string “Auðkenni APP Authentication”
Hash string generated n/kRNhXaZ2jFKv8KlQX7ydgedXUmVy8b2O4xNq2ZxHteG7wOvCa0Kg3rY1JLOrOBXYQm+z2FRVwIv47w8gUb5g==
Verification code calculated from the hash 4141
base64_decode()on the result frommd5()?