Skip to content

Instantly share code, notes, and snippets.

import TextManipulationHelper from './text-manipulation';
import CryptographyService from './cryptography.service';
const decrypt = async (cipherText: string, secretKey: string, initialVector: string) => {
const keyBuffer = TextManipulationHelper.convertBase64ToStream(secretKey);
const decryptionKey = await CryptographyService.parseKey(keyBuffer);
const cipherBuffer = TextManipulationHelper.convertBase64ToStream(cipherText);
const ivBuffer = TextManipulationHelper.convertBase64ToStream<Uint8Array>(initialVector);
import TextManipulationHelper from './text-manipulation';
import CryptographyService from './cryptography.service';
const base64 = 'string-in-base64';
const { cipher, key, iv } = await CryptographyService.encrypt(base64);
const cipherText = TextManipulationHelper.convertStreamToBase64(cipher);
console.log('Cipher: ', cipherText);
console.log('Encryption key: ', key);
@AvocadoVenom
AvocadoVenom / cryptography.service.ts
Created February 9, 2023 10:05
A wrapper service for SubtleCrypto Browser API
import TextManipulationHelper from './text-manipulation';
import type { CryptographyManager, EncryptionKeyManager, EncryptionPayload } from './cryptography.model';
class CryptographyService<T extends AesKeyGenParams | HmacKeyGenParams | Pbkdf2Params = AesKeyGenParams>
implements EncryptionKeyManager, CryptographyManager
{
public readonly algorithm: T;
constructor(algo: T) {
@AvocadoVenom
AvocadoVenom / cryptography.model.ts
Created February 9, 2023 09:53
Data models for Cryptography operations
export interface EncryptionKeyManager {
parseKey: (key: ArrayBuffer) => Promise<CryptoKey>
readKey: (key: CryptoKey) => Promise<ArrayBuffer>
}
export interface CryptographyManager {
generateIv: () => Uint8Array
encrypt: (data: string, key: CryptoKey) => Promise<EncryptionPayload>
decrypt: (cipher: ArrayBuffer, key: CryptoKey, iv: Uint8Array) => Promise<string>
}
@AvocadoVenom
AvocadoVenom / text-manipulation.ts
Last active February 8, 2023 17:48
Text representation conversion
// Text representation conversion
const convertStreamToBase64 = (buffer: ArrayBuffer | Uint8Array): string =>
window.btoa(new Uint8Array(buffer).reduce((a, b) => a + String.fromCharCode(b), ''));
const convertBase64ToStream = <T extends ArrayBuffer | Uint8Array>(base64: string, mimeType?: string): T => {
let sanitized = base64;
if (mimeType) sanitized = sanitized.replace(`data:${mimeType};base64,`, '');
const bin = window.atob(sanitized);
import { devices, PlaywrightTestConfig } from '@playwright/test'
const config: PlaywrightTestConfig = {
projects: [
{
name: 'Desktop__Chromium',
use: { ...devices['Desktop Chromium'] }
},
{
name: 'Desktop__Safari',
// https://jestjs.io/docs/manual-mocks#mocking-methods-which-are-not-implemented-in-jsdom
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({
matches: false,
media: query,
onchange: null,
addEventListener: jest.fn(),
removeEventListener: jest.fn(),
dispatchEvent: jest.fn()
export interface DeviceState {
isDesktop: boolean
desktopOS: DesktopOS | undefined
isWindowsDesktop: boolean
isLinuxOrUnixDesktop: boolean
isMobile: boolean
mobileOS: MobileOS | undefined
isAndroidDevice: boolean
isAppleDevice: boolean
type DeviceOS = DesktopOS | MobileOS
const getDeviceOS = (): DeviceOS | undefined => getMobileOS() ?? getDesktopOS()
enum DesktopOS {
Linux = 'linux',
MacOS = 'mac_os',
Unix = 'unix',
Unknown = 'unknown',
Windows = 'windows'
}
const getDesktopOS = (): DesktopOS | undefined => {
if (isDesktopDevice()) {