public Key DeriveKey( SharedSecret sharedSecret, ReadOnlySpan <byte> salt, ReadOnlySpan <byte> info, Algorithm algorithm, KeyFlags flags = KeyFlags.None) { if (sharedSecret == null) { throw Error.ArgumentNull_SharedSecret(nameof(sharedSecret)); } if (!_supportsSalt && !salt.IsEmpty) { throw Error.Argument_SaltNotSupported(nameof(salt)); } if (algorithm == null) { throw Error.ArgumentNull_Algorithm(nameof(algorithm)); } int keySize = algorithm.GetDefaultKeySize(); if (keySize > MaxOutputSize) { throw Error.NotSupported_CreateKey(); } SecureMemoryHandle keyHandle = null; byte[] publicKeyBytes = null; bool success = false; try { SecureMemoryHandle.Alloc(keySize, out keyHandle); DeriveKeyCore(sharedSecret.Handle, salt, info, keyHandle); algorithm.CreateKey(keyHandle, out publicKeyBytes); success = true; } finally { if (!success && keyHandle != null) { keyHandle.Dispose(); } } return(new Key(algorithm, flags, keyHandle, publicKeyBytes)); }
public Key( Algorithm algorithm, KeyFlags flags = KeyFlags.None) { if (algorithm == null) { throw Error.ArgumentNull_Algorithm(nameof(algorithm)); } int keySize = algorithm.GetDefaultKeySize(); SecureMemoryHandle keyHandle = null; byte[] publicKeyBytes = null; bool success = false; try { SecureMemoryHandle.Alloc(keySize, out keyHandle); SecureRandom.GenerateKeyCore(keyHandle); algorithm.CreateKey(keyHandle, out publicKeyBytes); success = true; } finally { if (!success && keyHandle != null) { keyHandle.Dispose(); } } keyHandle.MakeReadOnly(); _algorithm = algorithm; _flags = flags; _handle = keyHandle; _publicKey = (publicKeyBytes) != null ? new PublicKey(algorithm, publicKeyBytes) : null; }