GetBytes() public method

Fills the elements of a specified array of bytes with random numbers.
public GetBytes ( byte buffer ) : void
buffer byte An array of bytes to contain random numbers.
return void
 /// <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);
 }
Beispiel #2
0
        public static string GetRandomName(int length)
        {
            byte[] buf = new byte[length];

            CryptoRandom.GetBytes(buf);
            return(BitConverter.ToString(buf).Replace("-", String.Empty).Substring(0, length));
        }
Beispiel #3
0
            /// <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());
            }