Wednesday, August 05, 2020

How to quickly create cryptographic secrets using nodejs

This was stolen adapted from JWT Authentication Tutorial - Node.js by @DevSimplified.

First, start up node from a terminal. Then type the following:

This will simply 

First, start up node from a terminal. Then execute the following line:
The result will be a secret token.

Here's the same thing as an executable JavaScript file, which outputs the token as both hex and base64:

Sunday, April 07, 2019

How to update Ubuntu to the next release

This will work, apparently, even with pre-release software.

sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
sudo do-release-upgrade -d

Friday, March 30, 2018

Microsoft guidance on throwing exceptions

The rest of this post is a quote from this article at https://docs.microsoft.com/en-us/visualstudio/code-quality/ca2201-do-not-raise-reserved-exception-types:
The following exception types are too general to provide sufficient information to the user:

Throw Specific Exceptions
The following table shows parameters and which exceptions to throw when you validate the parameter, including the value parameter in the set accessor of a property:

Parameter DescriptionException
null reference System.ArgumentNullException
Outside the allowed range of values (such as an index for a collection or list) System.ArgumentOutOfRangeException
Invalid enum value System.ComponentModel.InvalidEnumArgumentException
Contains a format that does not meet the parameter specifications of a method (such as the format string for ToString(String)) System.FormatException
Otherwise invalid System.ArgumentException

Sunday, January 21, 2018

Git 2.16 and line endings

Git 2.16 introduces two new interesting options for dealing with line endings.

The first is the new --ignore-cr-at-eol option to git diff, which ignores changes in line endings in a diff, which otherwise sometimes lead to it appear that the entire file has changed (which, in a sense, it has).
git diff --cached --ignore-cr-at-eol
The second is the new --renormalize option to git add.
git add --renormalize .
To quote the man pages, git add --renormalize "is a new and safer way to record the fact that you are correcting the end-of-line convention and other "convert_to_git()" glitches in the in-repository data." So the way to normalize line endings in a repository has been revised to the following:
echo "* text=auto" >.gitattributes
git add --renormalize .
git status        # Show files that will be normalized
git commit -m "Introduce end-of-line normalization"
Note that the old way:
...
git read-tree --empty   # Clean index, force re-scan of working directory
git add .
...
will cause Git to delete files from the repository that are included in .gitignore. That might be a good thing for certain "undisciplined" repositories.

Friday, January 05, 2018

Easily building query strings in .NET

Instead of reinventing the wheel, try the following:
NameValueCollection queryString = System.Web.HttpUtility.ParseQueryString(string.Empty);

queryString["key1"] = "value1";
queryString["key2"] = "value2";

return queryString.ToString(); // Returns "key1=value1&key2=value2", all URL-encoded

Saturday, December 30, 2017

Be careful when using many HttpClient instances

Who knew? According to MSDN (emphasis added):
HttpClient is intended to be instantiated once and re-used throughout the life of an application. Instantiating an HttpClient class for every request will exhaust the number of sockets available under heavy loads.
See more here: Disposable, Finalizers, and HttpClient

Wednesday, December 13, 2017

Cool JSON and SQL-related links found on a Hacker News post

https://quicktype.io/ – Parses sample JSON and creates code to serialize/deserialize it in several different languages.

https://github.com/rspeele/Rezoom.SQL – Rezoom.SQL is an F# ORM for SQL databases using type providers, so it will automatically pick up the schema on build. A HN commenter claimed it has better type support than any other ORM, a statement perhaps to be taken with a grain of salt.

https://github.com/ReactiveX/IxJS – Interactive Extensions for JavaScript (IxJS). IxJS is a set of libraries to compose synchronous and asynchronous collections and Array#extras style composition in JavaScript