Prado\Security\TSecurityManager::validateData PHP Method

validateData() public method

Validates if data is tampered.
public validateData ( $data ) : string
return string the real data with HMAC stripped off. False if the data is tampered.
    public function validateData($data)
    {
        $len = $this->strlen($this->computeHMAC('test'));
        if ($this->strlen($data) < $len) {
            return false;
        }
        $hmac = $this->substr($data, 0, $len);
        $data2 = $this->substr($data, $len, $this->strlen($data));
        return $hmac === $this->computeHMAC($data2) ? $data2 : false;
    }

Usage Example

示例#1
0
 public function testValidateData()
 {
     $sec = new TSecurityManager();
     $sec->init(null);
     $sec->setValidationKey('aKey');
     $sec->setValidation('SHA1');
     $hashed = $sec->hashData('A text to hash');
     self::assertEquals('A text to hash', $sec->validateData($hashed));
     // try to alter the hashed data
     $hashed[45] = "z";
     self::assertFalse($sec->validateData($hashed));
     // and a test without tampered data
     self::assertFalse($sec->validateData('bad'));
 }