/// <summary> /// Sets the default variables for the given <see cref="Argon2id"/> /// hasher instance. /// </summary> /// <param name="hasher"></param> private void HydrateHasher(Argon2id hasher) { hasher.DegreeOfParallelism = Environment.ProcessorCount; hasher.Iterations = 5; hasher.MemorySize = 131_072; // 128MiB hasher.Salt = CryptoRandom.GetBytes(32); }
public static string GetRandomName(int length) { byte[] buf = new byte[length]; CryptoRandom.GetBytes(buf); return(BitConverter.ToString(buf).Replace("-", String.Empty).Substring(0, length)); }
/// <inheritdoc /> public override byte[] Encrypt(byte[] key, byte[] data) { using var aes = Aes.Create(); if (!IsValidKeySize(key.Length)) { throw new ArgumentException("Key is not a valid size for AES encryption.", nameof(key)); } byte[] iv = new byte[IvSize]; CryptoRandom.GetBytes(iv); ICryptoTransform encryptor = aes.CreateEncryptor(key, iv); using var memoryStream = new MemoryStream(); using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write)) { memoryStream.Write(iv); cryptoStream.Write(data); } return(memoryStream.ToArray()); }