<?php

declare(strict_types=1);

namespace PhpWA\Crypto\Signal;

use InvalidArgumentException;
use PhpWA\Crypto\CryptoPrimitives;

final class SenderChainKey
{
      private $iteration;
      private $seed;
    public function __construct(int $iteration, string $seed) { if ($iteration < 0 || $iteration > 0xffffffff || strlen($seed) !== 32) { throw new InvalidArgumentException('Sender chain key is invalid.'); } $this->iteration = $iteration; $this->seed = $seed; }
    public function iteration(): int { return $this->iteration; }
    public function seed(): string { return $this->seed; }
    public function messageKey(): SenderMessageKey { return new SenderMessageKey($this->iteration, CryptoPrimitives::hmacSha256($this->seed, "\x01")); }
    public function next(): self { if ($this->iteration === 0xffffffff) { throw new InvalidArgumentException('Sender chain counter is exhausted.'); } return new self($this->iteration + 1, CryptoPrimitives::hmacSha256($this->seed, "\x02")); }
}
