NextBytes() public static method

public static NextBytes ( byte buffer ) : void
buffer byte
return void
Example #1
0
        /// <summary>
        ///     Gets a random mac address.
        /// </summary>
        /// <param name="prefix">The prefix.</param>
        /// <param name="groupSplit">The group split.</param>
        /// <returns>The random mac address.</returns>
        public static string MacAddress(string prefix = null, char groupSplit = ':')
        {
            const int maxBufferLength = 6;

            int prefixesLength = prefix == null ? 0 : prefix.Split(groupSplit).Length;

            if (prefix != null && prefixesLength < maxBufferLength &&
                !prefix.EndsWith(groupSplit.ToString(), StringComparison.CurrentCulture))
            {
                prefix += groupSplit;
            }

            var buffer = new byte[maxBufferLength - prefixesLength];

            RandomNumber.NextBytes(buffer);
            string result = string.Concat(buffer.Select(x => x.ToString("X2", CultureInfo.CurrentCulture) + groupSplit));

            return(prefix + result.TrimEnd(groupSplit));
        }