authenticate() public static method

Authenticate a string
public static authenticate ( string $message, AuthenticationKey $secretKey, mixed $encoding = Halite::ENCODE_BASE64URLSAFE ) : string
$message string
$secretKey AuthenticationKey
$encoding mixed
return string
示例#1
0
 /**
  * @covers Symmetric::authenticate()
  * @covers Symmetric::verify()
  */
 public function testAuthenticateFail()
 {
     $key = new AuthenticationKey(new HiddenString(\str_repeat('A', 32), true));
     $message = 'test message';
     $mac = Symmetric::authenticate($message, $key, true);
     // Test invalid message
     $this->assertFalse(Symmetric::verify('othermessage', $key, $mac, true));
     $r = \Sodium\randombytes_uniform(\mb_strlen($mac, '8bit'));
     $_mac = $mac;
     $_mac[$r] = \chr(\ord($_mac[$r]) ^ 1 << \Sodium\randombytes_uniform(8));
     // Test invalid signature
     $this->assertFalse(Symmetric::verify($message, $key, $_mac, true));
 }
示例#2
0
 /**
  * @param int $userID
  * @return string
  */
 public function createRecoveryToken(int $userID) : string
 {
     $this->db->beginTransaction();
     $selector = Base64UrlSafe::encode(\random_bytes(static::RECOVERY_SELECTOR_BYTES));
     $token = Base64UrlSafe::encode(\random_bytes(static::RECOVERY_TOKEN_BYTES));
     $state = State::instance();
     $hashedToken = Symmetric::authenticate($token . $userID, $state->keyring['auth.recovery_key']);
     $this->db->insert('airship_user_recovery', ['userid' => $userID, 'selector' => $selector, 'hashedtoken' => $hashedToken, 'created' => (new \DateTime('NOW'))->format(\AIRSHIP_DATE_FORMAT)]);
     if (!$this->db->commit()) {
         return '';
     }
     return $selector . $token;
 }