Skip to content

Instantly share code, notes, and snippets.

View AArnott's full-sized avatar

Andrew Arnott AArnott

View GitHub Profile
@AArnott
AArnott / StrongNameTokenFromPublicKey.cs
Last active July 14, 2017 12:39
Demonstrates calculating a strong name token from a public key
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
namespace ConsoleApp43
{
static class Program
{
static void Main(string[] args)
@AArnott
AArnott / Get-DependenciesOfNuGetPackage.ps1
Created December 6, 2016 22:35
Downloads the list of nuget packages that depend on a named package
$PackageId = "Microsoft.VisualStudio.Threading"
$wr = Invoke-WebRequest "http://packages.nuget.org/v1/FeedService.svc/Packages?`$filter=substringof(%27$PackageId%27,%20Dependencies)%20eq%20true&`$select=Id,Version,Dependencies"
if ($wr.StatusCode -ne 200) {
Write-Error $wr.StatusDescription
return
}
$xml = [xml]$wr.content
$xml.feed.entry.properties
@AArnott
AArnott / C#RoslynCrashBug.cs
Created March 14, 2016 03:16
Repro for C# roslyn crash bug
namespace PCLCrypto
{
public class KeyFormatterTests
{
/*
Modulus: a4d5f49f3298500af851b031d27754fd63b8df7f37508b2bea15794ae706abc4cc790d5c8f4bac7ac46ac770b53830a28e97fd3bd9d2afdd18b8db9266965413
Exponent: 010001
P: e6505d775acbc8077462f0cdbe22a59fc6c75758a9a097211bc4e071c963e415
D: 6b87270cb2f4a9427ebacb35b516235b28b271198bfbfecda6e65b39817bd8907b0e7051b74ddb728f1f29220cef00095d63c224d5a148e14e15a9cb4c6849
Q: b73823d2929601f4f95050e17de1587841cbdc4152444f2352d9f83f54d71987
@AArnott
AArnott / RSAParameterCalculation.cs
Created February 7, 2016 06:17
Calculate full RSA private key parameters from the P and Q parameters
using System;
using System.Numerics;
using System.Security.Cryptography;
using System.Text;
using Xunit;
public class RSA
{
[Fact]
public void WikipediaExample()
#ref System.Numerics.dll
var rsa = new RSACryptoServiceProvider(512);
var parameters = rsa.ExportParameters(true);
var p = new BigInteger(parameters.P.Reverse().ToArray());
var q = new BigInteger(parameters.Q.Reverse().ToArray());
var n = p * q;
Assert.Equal(parameters.Modulus.Reverse().ToArray(), n.ToByteArray());
@AArnott
AArnott / AwaitOnExclusiveTaskScheduler.cs
Last active February 6, 2016 21:33
Demonstrates awaiting on a TaskScheduler
using Microsoft.VisualStudio.Threading;
public class ThreadSafeDictionary<K, V>
{
private readonly TaskScheduler exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
private readonly Dictionary<K, V> inner = new Dictionary<K, V>();
public async Task AddThenRemoveAsync(K key, V value, CancellationToken cancellationToken = default(CancellationToken)) {
await this.exclusiveScheduler; // Enabled by Microsoft.VisualStudio.Threading extension method.
cancellationToken.ThrowIfCancellationRequested();
@AArnott
AArnott / ThreadSafeDictionaryWithAsyncSemaphore.cs
Created February 6, 2016 21:30
Demonstrates the using syntax of AsyncSemaphore.
using Microsoft.VisualStudio.Threading; // From the Microsoft.VisualStudio.Threading NuGet package
public class ThreadSafeDictionary<K, V>
{
private readonly AsyncSemaphore semaphore = new AsyncSemaphore(1);
private readonly Dictionary<K, V> inner = new Dictionary<K, V>();
public async Task AddThenRemoveAsync(K key, V value, CancellationToken cancellationToken = default(CancellationToken)) {
using (await this.semaphore.EnterAsync(cancellationToken)) {
this.inner.Add(key, value);
@AArnott
AArnott / MultiThreadedCallsSingleThreadedViaSemaphoreWithYield.cs
Last active February 6, 2016 21:12
Demonstrates holding a semaphore and yielding
public class ThreadSafeDictionary<K, V>
{
private readonly SemaphoreSlim semaphore = new SemaphoreSlim(1);
private readonly Dictionary<K, V> inner = new Dictionary<K, V>();
public async Task AddThenRemoveAsync(K key, V value, CancellationToken cancellationToken = default(CancellationToken)) {
await this.semaphore.WaitAsync(cancellationToken);
try {
this.inner.Add(key, value);
await Task.Delay(50); // still holding semaphore, so no one can get it!
@AArnott
AArnott / ExclusiveTaskSchedulerWithAsyncWork.cs
Created February 6, 2016 21:08
Demonstrates calling non-thread-safe code using an exclusive TaskScheduler with an async delegate.
public class ThreadSafeDictionary<K, V>
{
private readonly TaskScheduler exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
private readonly Dictionary<K, V> inner = new Dictionary<K, V>();
public Task AddThenRemoveAsync(K key, V value, CancellationToken cancellationToken = default(CancellationToken)) {
return TaskScheduler.Factory.StartNew(
async delegate {
this.inner.Add(key, value);
await Task.Delay(50);
@AArnott
AArnott / ThreadSafeDictionaryWithExclusiveTaskScheduler.cs
Last active February 6, 2016 21:09
Demonstrates using an exclusive TaskScheduler to call non-thread-safe code.
public class ThreadSafeDictionary<K, V>
{
private readonly TaskScheduler exclusiveScheduler = new ConcurrentExclusiveSchedulerPair().ExclusiveScheduler;
private readonly Dictionary<K, V> inner = new Dictionary<K, V>();
public Task AddAsync(K key, V value, CancellationToken cancellationToken = default(CancellationToken)) {
return TaskScheduler.Factory.StartNew(
() => this.inner.Add(key, value),
cancellationToken,
TaskCreationOptions.None,