summaryrefslogtreecommitdiffstats
path: root/src/Utilities/Cryptography.cs
blob: cdf4984fc56a7a824bbed2b24093345afb2c0e75 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using System.Security.Cryptography;
using System.Text;

namespace IOL.Fagprove.Utilities
{
    public class Cryptography
    {
        public static string RandomString(int length = 12)
        {
            var chars = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ".ToCharArray();
            var data = new byte[length];
            using (var crypto = new RNGCryptoServiceProvider())
            {
                crypto.GetBytes(data);
            }

            var result = new StringBuilder(length);
            foreach (var b in data)
            {
                result.Append(chars[b % (chars.Length)]);
            }

            return result.ToString();
        }
    }
}