Skip to content

Instantly share code, notes, and snippets.

View rianjs's full-sized avatar

Rian Stockbower rianjs

View GitHub Profile
@rianjs
rianjs / StringTrimExtensions.cs
Created October 11, 2021 13:26
String extensions: Trim, TrimStart, TrimEnd with specific strings that you wish to trim
public static class StringTrimExtensions
{
public static string TrimEnd(this string input, string suffixToRemove, StringComparison comparisonType)
{
if (suffixToRemove == null)
{
return input;
}
return input?.EndsWith(suffixToRemove, comparisonType) ?? false
@rianjs
rianjs / ReadP12.cs
Created October 11, 2021 13:28
Decrypt a p12 certificate using C#
const string path = "/some/path/to/file.p12";
var contents = await File.ReadAllBytesAsync(path);
var securePass = "p12-file-password".ToSecureString();
var cert = new X509Certificate(contents, securePass);
@rianjs
rianjs / ToSecureString.cs
Created October 11, 2021 13:29
Convert string to SecureString using C#
public static SecureString ToSecureString(this string input)
{
var secureString = new SecureString();
var passwordCharacters = input.ToCharArray();
foreach (var character in passwordCharacters)
{
secureString.AppendChar(character);
}
@rianjs
rianjs / ExportToPem.cs
Created October 11, 2021 13:30
Export X509Certificate as PEM cert in C#
private static string ExportToPemCert(X509Certificate cert)
{
var exportedCert = cert.Export(X509ContentType.Cert);
var asBase64 = Convert.ToBase64String(exportedCert, Base64FormattingOptions.InsertLineBreaks);
var builder = new StringBuilder(2033);
builder.AppendLine("-----BEGIN CERTIFICATE-----");
builder.AppendLine(asBase64);
builder.AppendLine("-----END CERTIFICATE-----");
return builder.ToString();
@rianjs
rianjs / JsonSerializerUtils.cs
Created October 11, 2021 13:32
Getting sensible JsonSerializerOptions at runtime
public static class JsonSerializerUtils
{
public static JsonSerializerOptions GetJsonSerializerSettings()
{
#pragma warning disable 162
#if DEBUG
return GetFormattedJsonSettings();
#endif
return GetUnformattedJsonSettings();
@rianjs
rianjs / gitops.md
Last active March 13, 2023 01:20
Git operations

Common tasks

Delete merged branches

  • git remote prune origin prunes tracking branches not on the remote
  • git branch --merged lists branches that have been merged into the current branch
  • xargs git branch -d deletes branches listed on standard input
  • git branch --merged | xargs git branch -d plumbs everything together

Resolve a merge conflict

Let's suppose you have an automated merge failure going from the prod branch (33.0) to the test branch (33.1). So you will want to merge prod into test.

@rianjs
rianjs / administrative-ops.md
Last active February 6, 2022 14:08
Useful, idempotent T-SQL operations

Find connection string currently in use

select
    'data source=' + @@servername +
    ';initial catalog=' + db_name() +
    case type_desc
        when 'WINDOWS_LOGIN' 
            then ';trusted_connection=true'
 else
@rianjs
rianjs / bash
Created May 22, 2022 18:42
Reformat large json file
cat ugly.json | python3 -mjson.tool > pretty.json
@rianjs
rianjs / MultipleEnumeration1.cs
Created June 19, 2022 11:25
Skip-Take through collection
const int take = 3;
var range = Enumerable.Range(0, 10).ToList();
var ptr = 0;
while (range.Skip(ptr).Take(take).Any())
{
foreach (var element in range.Skip(ptr).Take(take))
Console.Write(element + ", ");
ptr += take;
}
@rianjs
rianjs / Program.cs
Created June 28, 2022 10:12
Old C# console app template with file-based namespacing and implicit usings
namespace SomeNamespace;
// Because doing anything non-trivial in C# requires the boilerplate anyway
public class Program
{
public static async Task Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}