Skip to content

Instantly share code, notes, and snippets.

@OllieJones
OllieJones / 1
Last active August 29, 2015 14:11
SELECT COUNT(*) AS customers
DATE(time_of_visit)
FROM traffic
GROUP BY DATE(time_of_visit)
ORDER BY DATE(time_of_visit)
@OllieJones
OllieJones / RandomString.sql
Last active February 25, 2016 21:01
Cryptographic quality random text string generation in SQL Server
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Description: Generate a 20-character cryptographically secure random string of uppercase letters and digits.
--- The entropy of such a string is about 100 bits: plenty.
-- Reference: https://social.msdn.microsoft.com/Forums/sqlserver/en-US/e4b5a6d1-1068-41e1-abd7-b3ec51d3f1c3/create-function-error?forum=transactsql
-- =============================================
CREATE VIEW RandomSeqView AS
@OllieJones
OllieJones / AddSignatureAlgs.cs
Created March 4, 2016 19:10
Add this code to your dotnet app to enable rsa-sha256, rsa-384, and rsa-512 signatures.
/// <summary>Declare the signature type for rsa-sha512</summary>
public class RsaPkCs1Sha512SignatureDescription : SignatureDescription
{
public RsaPkCs1Sha512SignatureDescription()
{
KeyAlgorithm = typeof(RSACryptoServiceProvider).FullName;
DigestAlgorithm = typeof(SHA512CryptoServiceProvider).FullName;
FormatterAlgorithm = typeof(RSAPKCS1SignatureFormatter).FullName;
DeformatterAlgorithm = typeof(RSAPKCS1SignatureDeformatter).FullName;
}
CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha512SignatureDescription),
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha512");
CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha384SignatureDescription),
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha384");
CryptoConfig.AddAlgorithm(typeof(RsaPkCs1Sha256SignatureDescription),
"http://www.w3.org/2001/04/xmldsig-more#rsa-sha256");
@OllieJones
OllieJones / explanation.md
Last active July 20, 2016 16:18
Detecting a disconnected RDP session in a unix / linux / bsd server.

Here is a way to obtain a list of disconnected xrdp sessions. It relies on the fact that the xrdp server is, in normal X session manager usage, the only client that establishes a TCP connection to the Xvnc X Window System display server. (The other client programs -- the ones doing things for the user -- use UNIX-domain sockets for their display connections).

When an xrdp session is active, the associated Xvnc display server has two TCP connections, one in the ESTABLISHED state, and the other in the LISTEN state. That looks something like this using the lsof(1) program.

$ sudo lsof  -b -w -n -c /^Xvnc$/b -a -iTCP:5900-5999 
COMMAND  PID  USER   FD   TYPE DEVICE SIZE/OFF NODE NAME
Xvnc    1625 guest    1u  IPv4 252910      0t0  TCP 127.0.0.1:5910 (LISTEN)
Xvnc    1625 guest    9u  IPv4 261226      0t0  TCP 127.0.0.1:5910->127.0.0.1:35242 (ESTABLISHED)

If the user of the remote session abandons it by closing the RDP connection (or, in the case of an Apache Guacamole RDP session

@OllieJones
OllieJones / cache.js
Last active December 3, 2016 02:45 — forked from bshamric/cache.js
I like phantomjs, but it doesn't directly support getting images from webpages without requesting them separately like in casperjs. I went through QTNetworking code in the phantomjs repo until I figured out where the cache was. To use this, have all three files in the same directory. Then modify test.js for whatever you need.Call phantom js with…
var pFs = require('fs');
//this is the path that QTNetwork classes uses for caching files for it's http client
//the path should be the one that has 16 folders labeled 0,1,2,3,...,F
exports.cachePath = '/path/to/phantomjs/cache/data/folder';
//object path
exports.objectPath = '/path/to/phantomjs/cache/objects';
//call this when you first encounter a resource//this is the extension used for files in the cache path
@OllieJones
OllieJones / nonce.js
Created October 3, 2017 12:28
Snippets of Javascript.
'use strict';
/* node.js nonce: crypto-quality random session key (filename safe base64, rfc4648) */
const btoa = require( 'btoa' );
const crypto = require( 'crypto' );
function nonce( length ) {
const rndLen = 1+(( (length)+(length<<1) +3)>>2);
const randomArray = crypto.randomBytes( rndLen );
@OllieJones
OllieJones / validateGithubWebhook.js
Created October 5, 2017 12:49
Github webhooks use a shared secret for validation. The webhook itself contains a header X-Hub-Signature containing a hash of the webhook body. This function checks that hash against the body.
'use strict';
const crypto = require( 'crypto' );
function validateGithub( secret, signature, rawBody ) {
if( (!signature) || signature.length === 0 ) return false;
if( (!rawBody) || rawBody.length === 0 ) return false;
try {
const splits = signature.split( '=' );
@OllieJones
OllieJones / streamPeek.js
Last active September 5, 2018 13:38
Read useful information from a MP4 data stream
/*
* this code examines a MP4 data stream.
* it looks at the appropriate atoms to determine width, height, and frame rate.
*
* It is a sleazy approach, the equivalent of grepping for necessary
* boxes rather than using box lengths to parse the file.
*/
function read32( buff, fourcc, offset ) {
let start = 0;
@OllieJones
OllieJones / stored_procs
Last active April 16, 2020 15:23
SQL Reporting By Time Intervals
DELIMITER $$
DROP FUNCTION IF EXISTS TRUNC_HOUR$$
CREATE
FUNCTION TRUNC_HOUR(datestamp DATETIME)
RETURNS DATETIME DETERMINISTIC NO SQL
COMMENT 'returns current hour'
RETURN DATE_FORMAT(datestamp, '%Y-%m-%d %H:00')$$
DROP FUNCTION IF EXISTS TRUNC_DAY$$