Skip to content

Instantly share code, notes, and snippets.

View hclewk's full-sized avatar

Kurtis Welch hclewk

View GitHub Profile
/// <summary>
/// Class used for conversion between byte array and Base32 notation
/// </summary>
internal sealed class Base32
{
/// <summary>
/// Size of the regular byte in bits
/// </summary>
private const int InByteSize = 8;
@hclewk
hclewk / ToDictionary.cs
Created December 31, 2016 18:00
Turn Anonymous Object into a Dictionary<string, string>
public static Dictionary<string, string> ToDictionary(this object values)
{
var result = new Dictionary<string, string>();
var type = values.GetType();
var properties = type.GetProperties();
foreach (var property in properties)
{
var name = property.Name;
var value = property.GetValue(values);
@hclewk
hclewk / Email.cs
Created December 31, 2016 18:01
Send Email from Gmail
public class Email
{
public string Subject { get; set; }
public string Body { get; set; }
public string To { get; set; }
public string From { get; set; }
public Email()
{
@hclewk
hclewk / HashtagUtil.cs
Created December 31, 2016 18:03
Turn any string into a hashtag in c#
//Input: "St. Patrick's Day: Awësöme tö-dö's"
//Output: "#StPatricksDayAwesomeToDos"
public static string MakeHashtag(string text)
{
text = RemoveDiacritics(CultureInfo.CurrentCulture.TextInfo.ToTitleCase(text));
text = Regex.Replace(text, @"[^a-zA-Z0-9]", "");
return "#" + text;
}
@hclewk
hclewk / HashUtil.cs
Created December 31, 2016 18:05
Get MD5 Hash
public static string GetHash(string str)
{
using (var md5 = MD5.Create())
{
return BitConverter.ToString(md5.ComputeHash(Encoding.UTF8.GetBytes(str))).Replace("-", "").ToLower();
}
}
@hclewk
hclewk / gzip.cs
Created December 31, 2016 18:05
Upload GZipped String to S3 - Fire and Forget
AmazonS3Client client = new AmazonS3Client(RegionEndpoint.USWest2);
MemoryStream mem = new MemoryStream();
using (GZipStream gz = new GZipStream(mem, CompressionLevel.Optimal, true))
{
var b = Encoding.UTF8.GetBytes(json);
gz.Write(b, 0, b.Length);
gz.Flush();
}
@hclewk
hclewk / gzipexample.cs
Created December 31, 2016 18:06
GZip a string in c#
MemoryStream mem = new MemoryStream();
using (GZipStream gz = new GZipStream(mem, CompressionLevel.Optimal, true))
{
var b = Encoding.UTF8.GetBytes(json);
gz.Write(b, 0, b.Length);
gz.Flush();
}
var data = mem.ToArray();
@hclewk
hclewk / dynamoToSns.js
Created December 31, 2016 18:08
DynamoDB Streams Events to SNS - NodeJS Lambda
var AWS = require('aws-sdk');
AWS.config.region = 'us-east-1';
var sns = new AWS.SNS();
exports.handler = function(event, context) {
var count = event.Records.length;
var completed = function(err, data){
@hclewk
hclewk / getFilename.js
Created December 31, 2016 18:10
Get Filename from URL in JS
var getFileName = function (url) {
return url.replace(/\?.*$/, "").replace(/.*\//, "");
}
@hclewk
hclewk / password.cs
Created December 31, 2016 18:11
Hash a password in c#
using System;
using System.Text;
using System.Security.Cryptography;
namespace PasswordHash
{
/// <summary>
/// Salted password hashing with PBKDF2-SHA1.
/// Author: havoc AT defuse.ca
/// www: http://crackstation.net/hashing-security.htm