![]() |
|
[ASK] Random String Pada VB.NET - Printable Version +- HN-Community (https://hacker-newbie.org) +-- Forum: Miscellaneous (https://hacker-newbie.org/forum-86.html) +--- Forum: Coding/Programing (https://hacker-newbie.org/forum-19.html) +--- Thread: [ASK] Random String Pada VB.NET (/thread-17849.html) |
[ASK] Random String Pada VB.NET - Rukanda - 08-06-2013 assalamualaikum, maaf nih nanya melulu, kemaren saya sempet belajar buat random string menggunakan PHP, tiba tiba terbesit di pikiran "apa bisa buat random string di vb.net atau vb6 ??" saya udah sempet baca baca beberapa referensi tapi masih belom kepikiran gimana alurnya.. kalo di sini ada yang tau atau pernah buat tolong di share ya om..
RE: [ASK] Random String Pada VB.NET - ') or '1'='1-- - 08-06-2013 Pake "Crypto" =) Code: string RandomString(int length, string allowedChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789") {
// You could add support for non-alphanumeric characters if you want
if (length < 0) throw new ArgumentOutOfRangeException("length", "length cannot be less than zero.");
if (string.IsNullOrEmpty(allowedChars)) throw new ArgumentException("allowedChars may not be empty.");
const int byteSize = 0x100;
var allowedCharSet = new HashSet<char>(allowedChars).ToArray();
if (byteSize < allowedCharSet.Length) throw new ArgumentException(String.Format("allowedChars may contain no more than {0} characters.", byteSize));
using (var rng = new System.Security.Cryptography.RNGCryptoServiceProvider()) {
var result = new StringBuilder();
var buf = new byte[128];
while (result.Length < length) {
rng.GetBytes(buf);
for (var i = 0; i < buf.Length && result.Length < length; ++i) {
var outOfRangeStart = byteSize - (byteSize % allowedCharSet.Length);
if (outOfRangeStart <= buf[i]) continue;
result.Append(allowedCharSet[buf[i] % allowedCharSet.Length]);
}
}
return result.ToString();
}
}Jalankan perintah apabila button di click: Code: private void button1_Click(object sender, EventArgs e)
{
MessageBox.Show(RandomString(15));
}RE: [ASK] Random String Pada VB.NET - kuz-inc - 08-07-2013 VB6 : Code: Public Function RandomString( _
ByVal Length As Long, _
Optional charset As String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789" _
) As String
Dim chars() As Byte, Value() As Byte, chrUprBnd As Long, i As Long
If Length > 0& Then
Randomize
chars = charset
chrUprBnd = Len(charset) - 1&
Length = (Length * 2&) - 1&
ReDim Value(Length) As Byte
For i = 0& To Length Step 2&
Value(i) = chars(CLng(chrUprBnd * Rnd) * 2&)
Next
End If
RandomString = Value
End Function
Private Sub Command1_Click()
MsgBox RandomString(20)
End Sub
|