Skip to content

Instantly share code, notes, and snippets.


Thomas Konrad thomaskonrad

View GitHub Profile
thomaskonrad / StreamSlicer.ts
Created Feb 9, 2020
Splits a ReadableStream into chunks of a given size.
View StreamSlicer.ts
// This source code is taken from Firefox 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 / saveFile.ts
Created Feb 8, 2020
Downloading an Array Buffer via a "Save as" Dialog in the Browser
View saveFile.ts
// This source code is taken from Firefox 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 / splitFile.ts
Last active Feb 8, 2020
Split files into chunks of a given size using the FileReader API.
View splitFile.ts
// This source code is taken from Firefox 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 / AuthenticatedSecretKeyCryptography.ts
Created Feb 8, 2020
Authenticated Secret Key Cryptography (AEAD) in TypeScript
View AuthenticatedSecretKeyCryptography.ts
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 / RandomGenerator.test.ts
Created Feb 8, 2020
Random Number Generator in TypeScript: Jest Unit Test
View RandomGenerator.test.ts
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 / RanomGenerator.ts
Last active Feb 8, 2020
Random Number Generator in TypeScript
View RanomGenerator.ts
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;
You can’t perform that action at this time.