Skip to content

Instantly share code, notes, and snippets.

@thomaskonrad
thomaskonrad / RanomGenerator.ts
Last active February 8, 2020 15:28
Random Number Generator in TypeScript
export default class RandomGenerator {
public static generateRandomBytes(length: number): Uint8Array {
return crypto.getRandomValues(new Uint8Array(length));
}
public static generateRandomNumber(min: number, max: number): number {
const range = max - min;
const maxGeneratedValue = 0xFFFFFFFF;
const possibleResultValues = range + 1;
const possibleGeneratedValues = maxGeneratedValue + 1;
@thomaskonrad
thomaskonrad / RandomGenerator.test.ts
Created February 8, 2020 15:21
Random Number Generator in TypeScript: Jest Unit Test
import { Crypto } from '@peculiar/webcrypto';
import RandomGenerator from '@/crypto/RandomGenerator';
Object.defineProperty(window, 'crypto', {
value: new Crypto(),
});
test('Random generator returns a 16 byte random sequence', () => {
const randomBytes1 = RandomGenerator.generateRandomBytes(16);
const randomBytes2 = RandomGenerator.generateRandomBytes(16);
@thomaskonrad
thomaskonrad / AuthenticatedSecretKeyCryptography.ts
Created February 8, 2020 15:22
Authenticated Secret Key Cryptography (AEAD) in TypeScript
export default class AuthenticatedSecretKeyCryptography {
public static readonly KEY_LENGTH_IN_BYTES = 16;
public static readonly IV_LENGTH_IN_BYTES = 16;
public static readonly TAG_LENGTH_IN_BYTES = 16;
private static readonly ALGORITHM = 'AES-GCM';
private readonly secretKey: CryptoKey;
private readonly tagLengthInBytes: number;
public constructor(secretKey: CryptoKey, tagLengthInBytes = AuthenticatedSecretKeyCryptography.TAG_LENGTH_IN_BYTES) {
@thomaskonrad
thomaskonrad / splitFile.ts
Last active April 29, 2023 11:24
Split files into chunks of a given size using the FileReader API.
// This source code is taken from Firefox Send (https://github.com/mozilla/send) and slightly modified.
export default async function splitFile(
file: File,
chunkSize: number,
callback: (chunk: Uint8Array, sequenceNumber: number) => void,
transformer?: (chunk: Uint8Array, chunkIndex: number) => any,
) {
const fileSize = file.size;
let offset = 0;
@thomaskonrad
thomaskonrad / saveFile.ts
Created February 8, 2020 16:33
Downloading an Array Buffer via a "Save as" Dialog in the Browser
// This source code is taken from Firefox Send (https://github.com/mozilla/send) and slightly modified.
export default async function saveFile(plaintext: ArrayBuffer, fileName: string, fileType: string) {
return new Promise((resolve, reject) => {
const dataView = new DataView(plaintext);
const blob = new Blob([dataView], { type: fileType });
if (navigator.msSaveBlob) {
navigator.msSaveBlob(blob, fileName);
return resolve();
@thomaskonrad
thomaskonrad / StreamSlicer.ts
Created February 9, 2020 14:02
Splits a ReadableStream into chunks of a given size.
// This source code is taken from Firefox Send (https://github.com/mozilla/send) and slightly modified.
export default class StreamSlicer {
protected chunkSize: number;
protected partialChunk: Uint8Array;
protected offset: number;
constructor(chunkSize: number) {
this.chunkSize = chunkSize;
this.partialChunk = new Uint8Array(this.chunkSize);
@thomaskonrad
thomaskonrad / executeConcurrently.test.ts
Created December 27, 2020 12:11
Script to execute multiple Promises concurrently with a certain concurrency limit
import executeConcurrently from '@/generic/executeConcurrently';
test('Concurrent execution works with decreasing execution time', async () => {
const numberOfExecutions = 10;
const resolvedExecutions = await testExecution(
numberOfExecutions,
3,
(counter: number) => Math.ceil(100 / counter),
);
blueprint:
name: Motion-activated Light with illuminance
description: Turn on a light when motion is detected and illuminance is below a set Lux level.
domain: automation
input:
action_id:
name: Action ID
description: 'That is your Action name in snake case, e.g., "automatic_wc_light".'
selector:
text:
@thomaskonrad
thomaskonrad / automatic-fan-with-motion.yaml
Last active March 27, 2022 09:51
A Home Assistant blueprint to automatically turn a fan on and off upon motion
blueprint:
name: Motion-activated Fan
description: A Home Assistant blueprint to automatically turn a fan on and off upon motion
domain: automation
input:
motion_entity:
name: Motion Sensor
selector:
entity:
fan_target:
@thomaskonrad
thomaskonrad / automatic-covers.yaml
Last active April 5, 2022 20:18
A Home Assistant blueprint to automatically set the cover position depending on the sun position
blueprint:
name: Automatic Covers
description: A Home Assistant blueprint to automatically set the cover position depending on the sun position
domain: automation
input:
shutter_automatic_input:
name: Shutter Automatic Input
selector:
entity:
domain: input_boolean