Skip to content

Instantly share code, notes, and snippets.

View gist:3e7b39f3282d0e38d082
//Prepare test data. Could be a set returned
//from a database query
var list = new List<int>();
for (int i = 0; i < 1000000; i++)
{
list.Add(i);
}
//Prepare a list to hold the results
//E.g list of all users born a certain year
var listOfInts = new List<IEnumerable<int>>();
@mclasson
mclasson / gist:2b3a49f98d8c3a56e118
Last active Aug 29, 2015
Linq deferred execution
View gist:2b3a49f98d8c3a56e118
//Prepare test data. Could be a set returned
//from a database query
var list = new List<int>();
for (int i = 0; i < 1000000; i++)
{
list.Add(i);
}
//Filter out a small subset of the data [zip code, annual income]
var listSmall = list.Where(i => i > 100000 && i < 150000);
View Validate
public static bool Validate(string passwordHash, string saltHash, string enteredPassword)
{
byte[] _password = System.Text.Encoding.UTF8.GetBytes(enteredPassword);
Rfc2898DeriveBytes keyEntered = new Rfc2898DeriveBytes(_password, Convert.FromBase64String(saltHash), 10000);
return Convert.ToBase64String(keyEntered.GetBytes(24)) == passwordHash;
}
View gist:5989543
public static HashedPassword Generate(string password)
{
byte[] _salt = new byte[8];
using (RNGCryptoServiceProvider csp = new RNGCryptoServiceProvider())
{
csp.GetBytes(_salt);
}
byte[] _password = System.Text.Encoding.UTF8.GetBytes(password);
Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(_password, _salt, 10000);
var _saltedPasswordHash = k1.GetBytes(24);
View HashedPassword
public struct HashedPassword
{
public string Password { get; set; }
public string Salt { get; set; }
}
View gist:5989608
var keyAndSalt = Hash.Generator.Generate("P@ssword2013");
bool isEqual1 = Hash.Generator.Validate(keyAndSalt.Password, keyAndSalt.Salt, "Password2013");
//isEqual1 == false
bool isEqual2 = Hash.Generator.Validate(keyAndSalt.Password, keyAndSalt.Salt, "P@ssword2013");
//isEqual2 == true
View gist:5989751
Rfc2898DeriveBytes k1 = new Rfc2898DeriveBytes(_password, _salt, 10000);
var _saltedPasswordHash = k1.GetBytes(12);
Debug.WriteLine(Convert.ToBase64String(_saltedPasswordHash));
_saltedPasswordHash = k1.GetBytes(12);
Debug.WriteLine(Convert.ToBase64String(_saltedPasswordHash));
Rfc2898DeriveBytes k2 = new Rfc2898DeriveBytes(_password, _salt, 10000);
_saltedPasswordHash = k2.GetBytes(24);
Debug.WriteLine(Convert.ToBase64String(_saltedPasswordHash));
View gist:5993946
[DataContract]
class Order
{
public DateTime CreatedOn { get; set; }
public List<OrderRow> Orderrows { get; set; }
}
[CollectionDataContract]
class OrderRow
{
[DataMember]
@mclasson
mclasson / gist:5993925
Last active Dec 19, 2015
How to connect to a Microsoft Azure Service Bus Queue
View gist:5993925
string connectionString = "<your connectionstring here>";
var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
if (!namespaceManager.QueueExists("orderqueue"))
{
namespaceManager.CreateQueue("orderqueue");
}
var Client = QueueClient.CreateFromConnectionString(connectionString, "orderqueue");
@mclasson
mclasson / gist:5993927
Last active Dec 19, 2015
How to receive message from Microsoft Azure Service Bus Queue
View gist:5993927
while (true)
{
BrokeredMessage message = Client.Receive();
if (message != null)
{
try
{
var msgStream = message.GetBody<Stream>();
StreamReader sr = new StreamReader(msgStream);