using System.Security.Cryptography;
using ZB.MOM.WW.ScadaBridge.Commons.Types.Transport;
namespace ZB.MOM.WW.ScadaBridge.Transport.Serialization;
///
/// Builds a for a freshly serialized bundle.
/// Stamps the current format/schema version, captures the SHA-256 hash of the
/// raw (post-encryption) content bytes, and copies through the supplied summary
/// and content entries verbatim.
///
public sealed class ManifestBuilder
{
public const int CurrentBundleFormatVersion = 1;
public const string CurrentSchemaVersion = "1.0";
///
/// Builds a with the current format version, a SHA-256 content hash,
/// and all supplied metadata.
///
/// Environment label identifying where the bundle was exported from.
/// Username of the operator who performed the export.
/// ScadaBridge version string stamped in the manifest.
/// Encryption metadata when the content is encrypted; null for plain bundles.
/// High-level summary of artifact counts.
/// Per-entry content table describing each artifact in the bundle.
/// Raw content bytes whose SHA-256 hash is computed and stamped.
/// A fully populated .
public BundleManifest Build(
string sourceEnvironment,
string exportedBy,
string scadaBridgeVersion,
EncryptionMetadata? encryption,
BundleSummary summary,
IReadOnlyList contents,
byte[] contentBytes)
{
ArgumentNullException.ThrowIfNull(sourceEnvironment);
ArgumentNullException.ThrowIfNull(exportedBy);
ArgumentNullException.ThrowIfNull(scadaBridgeVersion);
ArgumentNullException.ThrowIfNull(summary);
ArgumentNullException.ThrowIfNull(contents);
ArgumentNullException.ThrowIfNull(contentBytes);
var contentHash = "sha256:" + Convert.ToHexString(SHA256.HashData(contentBytes)).ToLowerInvariant();
return new BundleManifest(
BundleFormatVersion: CurrentBundleFormatVersion,
SchemaVersion: CurrentSchemaVersion,
CreatedAtUtc: DateTimeOffset.UtcNow,
SourceEnvironment: sourceEnvironment,
ExportedBy: exportedBy,
ScadaBridgeVersion: scadaBridgeVersion,
ContentHash: contentHash,
Encryption: encryption,
Summary: summary,
Contents: contents);
}
}