-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRandomUtils.cs
115 lines (98 loc) · 4.41 KB
/
RandomUtils.cs
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace HackerInsideOneTimePadGenerator {
public class RandomUtils {
public static string getRandomUniqueString(int length, string spacer) {
const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder res = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
byte[] uintBuffer = new byte[sizeof(uint)];
char lettera;
while (length > 0) {
do {
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
lettera = valid[(int)(num % (uint)valid.Length)];
if (!res.ToString().Contains(lettera)) {
res.Append(lettera);
res.Append(spacer);
length--;
}
if (length == 0)
break;
//MessageBox.Show(length.ToString() + " " + res.ToString());
} while (res.ToString().Contains(lettera));
}
res.Append(" ");
return res.ToString();
}
}
public static string getRandomUniqueString(int length, StringBuilder res) {
const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXY";
StringBuilder res1 = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
byte[] uintBuffer = new byte[sizeof(uint)];
char lettera;
while (length > 0) {
do {
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
lettera = valid[(int)(num % (uint)valid.Length)];
if (!res.ToString().Contains(lettera) && !res1.ToString().Contains(lettera)) {
res1.Append(lettera);
length--;
}
if (length == 0)
break;
//MessageBox.Show(length.ToString() + " " + res.ToString());
} while (res.ToString().Contains(lettera) && res1.ToString().Contains(lettera));
}
res1.Append(" ");
return res1.ToString();
}
}
public static string RandomNumSequence(int length) {
const string valid = "1234567890";
StringBuilder res = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
byte[] uintBuffer = new byte[sizeof(uint)];
while (length-- > 0) {
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
res.Append(valid[(int)(num % (uint)valid.Length)]);
}
}
return res.ToString();
}
public static string RandomHexSequence(int length) {
const string valid = "123456789ABCDEF";
StringBuilder res = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
byte[] uintBuffer = new byte[sizeof(uint)];
while (length-- > 0) {
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
res.Append(valid[(int)(num % (uint)valid.Length)]);
}
}
return res.ToString();
}
public static string RandomString(int length) {
const string valid = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
StringBuilder res = new StringBuilder();
using (RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider()) {
byte[] uintBuffer = new byte[sizeof(uint)];
while (length-- > 0) {
rng.GetBytes(uintBuffer);
uint num = BitConverter.ToUInt32(uintBuffer, 0);
res.Append(valid[(int)(num % (uint)valid.Length)]);
}
}
return res.ToString();
}
}
}