/// <summary> /// Creates a new randomly generated key. /// </summary> /// <returns>A new randomly generated key.</returns> public static XChaChaKey Generate() { Sodium.Initialize(); GuardedMemoryHandle.Alloc(KeyLengthBytes, out var handle); var keySpan = handle.DangerousGetSpan(); crypto_secretstream_xchacha20poly1305_keygen(ref MemoryMarshal.GetReference(keySpan)); handle.MakeReadOnly(); return(new XChaChaKey(handle)); }
/// <summary> /// Create a new instance from an existing key. /// </summary> /// <param name="key">The key.</param> public XChaChaKey(ReadOnlySpan <byte> key) { Sodium.Initialize(); if (key.Length != KeyLengthBytes) { throw new ArgumentException("key has invalid length", nameof(key)); } GuardedMemoryHandle.Alloc(KeyLengthBytes, out this.handle); this.handle.Write(key); this.handle.MakeReadOnly(); }
internal XChaChaStreamState() { GuardedMemoryHandle.Alloc(Length, out this.handle); }