Skip to content

Instantly share code, notes, and snippets.

View yetanotherchris's full-sized avatar
🔏
;ÿÿÿÿrules

Chris S. yetanotherchris

🔏
;ÿÿÿÿrules
View GitHub Profile
@yetanotherchris
yetanotherchris / gist:4746446
Last active December 12, 2015 08:48
Unique identifiers: Base64
private static Random _random = new Random();
public static string Base64Hash()
{
byte[] buffer = GetRandom(4);
return Convert.ToBase64String(buffer);
}
// <summary>
// This is used by all Unique identifier examples
@yetanotherchris
yetanotherchris / gist:4746620
Created February 9, 2013 19:10
Unique identifiers: number hash
public static string NumberHash()
{
byte[] buffer = GetRandom(2);
return string.Format("{0:00}-{1:00}",buffer[0],buffer[1]);
}
@yetanotherchris
yetanotherchris / gist:4746625
Last active December 12, 2015 08:49
Unique identifiers: alphanumeric
public static string AlphaNumeric()
{
string s = "abcdefghijklmnopqrstuvwxyz0123456789";
StringBuilder builder = new StringBuilder();
Random random = new Random();
for (int i = 0; i < 4; i++)
{
int index = _random.Next(1, 36);
builder.Append(s[index]);
}
@yetanotherchris
yetanotherchris / gist:4746641
Last active December 12, 2015 08:49
Unique identifiers: url hash
public static string HostnameHash(Uri uri)
{
// e.g. Uri uri = new Uri("http://www.yetanotherchris.info/csharp/example");
// gives eaafc653
int hashcode = uri.GetHashCode();
Console.Write(string.Format("{0:X}", hashcode).ToLower());
}
@yetanotherchris
yetanotherchris / gist:4746643
Last active December 12, 2015 08:49
Unique identifiers: time hashcode
public static string TimeToHexString()
{
long ms = DateTime.Now.Second;
long ms2 = DateTime.Now.Millisecond;
return string.Format("{0:X}{1:X}", ms, ms2).ToLower();
}
@yetanotherchris
yetanotherchris / gist:4746646
Last active December 12, 2015 08:49
Unique identifiers: ticks
public static string TicksToString()
{
long ticks = DateTime.Now.Ticks;
return string.Format("{0:X}", ticks).ToLower();
}
@yetanotherchris
yetanotherchris / gist:4746664
Created February 9, 2013 19:19
Unique identifiers: MD5
public static string RandomMD5()
{
byte[] buffer = GetRandom(16);
MD5 md5 = System.Security.Cryptography.MD5.Create();
byte[] output = md5.ComputeHash(buffer);
StringBuilder builder = new StringBuilder();
for (int i = 0; i < output.Length; i++)
builder.AppendFormat("{0:x2}", output[i]);
@yetanotherchris
yetanotherchris / gist:4746671
Last active February 14, 2017 21:20
Unique identifiers: Base62
public static string Base62Random()
{
int random = _random.Next();
return Base62ToString(random);
}
private static string Base62ToString(long value)
{
// Divides the number by 64, so how many 64s are in
// 'value'. This number is stored in Y.
@yetanotherchris
yetanotherchris / gist:4746689
Created February 9, 2013 19:26
State design pattern example
namespace DesignPatterns
{
/// <summary>
/// Default State that every state inherits from.
/// </summary>
public abstract class State
{
/// <summary>
/// Holds the current state we're in.
/// </summary>
@yetanotherchris
yetanotherchris / c-sharp-pronounceable-password-generator.cs
Last active January 8, 2018 16:41
C# Pronounceable password generator
using System;
using System.Collections;
using System.Text;
// Example usage
void Main()
{
PronounceablePasswordGenerator generator = new PronounceablePasswordGenerator();
int numPasswords = 5;
int passwordLength = 10;