user = $user; if (null !== $key) { [$identifier, $verifier] = explode(self::SEPARATOR, $key); $this->id = $identifier; $this->verifier = $this->hashVerifier($verifier); } } /** * Generate a unique identifier and return both the identifier and verifier. * * @return array [identifier, verifier] * @throws \Exception */ public function generate(): array { $random_str = hash('sha256', random_bytes(32)); $identifier = substr($random_str, 0, 16); $verifier = substr($random_str, 16, 32); $this->id = $identifier; $this->verifier = $this->hashVerifier($verifier); return [$identifier, $verifier]; } /** * @return string */ public function getId(): string { return $this->id; } /** * Verify an incoming API key against the verifier on this record. * * @return bool */ public function verify($verifier): bool { return hash_equals($this->verifier, $this->hashVerifier($verifier)); } /** * @return User */ public function getUser(): User { return $this->user; } /** * @AuditLog\AuditIdentifier * * @return string */ public function getComment(): ?string { return $this->comment; } /** * @param string $comment */ public function setComment(?string $comment): void { $this->comment = $this->_truncateString($comment); } public function jsonSerialize() { return [ 'id' => $this->id, 'comment' => $this->comment, ]; } /** * @param string $original * @return string The hashed verifier. */ protected function hashVerifier(string $original): string { return hash('sha512', $original); } }