<?php

declare(strict_types=1);

namespace PhpWA\Crypto\Signal;

use InvalidArgumentException;

final class SenderMessageKey
{
    /** @var int */ private $iteration;
    /** @var string */ private $seed;
    /** @var string */ private $iv;
    /** @var string */ private $cipherKey;

    public function __construct(int $iteration, string $seed)
    {
        if ($iteration < 0 || $iteration > 0xffffffff || strlen($seed) !== 32) {
            throw new InvalidArgumentException('Sender message key is invalid.');
        }
        $derived = SignalKdf::derive($seed, str_repeat("\0", 32), 'WhisperGroup', 3);
        $this->iteration = $iteration;
        $this->seed = $seed;
        $this->iv = substr($derived[0], 0, 16);
        $this->cipherKey = substr($derived[0], 16, 16) . substr($derived[1], 0, 16);
    }
    public function iteration(): int { return $this->iteration; }
    public function seed(): string { return $this->seed; }
    public function iv(): string { return $this->iv; }
    public function cipherKey(): string { return $this->cipherKey; }
}
