UNPKG

lavva.webbluetooth

Version:

Library implementing WebBluetooth custom functionality if underlying platform does support it

50 lines (49 loc) 2.37 kB
export interface SessionIdPayload { validFrom: number; sessionId: number; validTo: number; } declare const byteBrand: unique symbol; export type Byte = number & { readonly [byteBrand]: void; }; export declare const toByte: (n: number) => Byte; export declare class LavvaBluetoothEncryptionHelper { /** Parse MAC like "AA:BB:CC:DD:EE:FF" or "AABBCCDDEEFF" into 6 bytes */ static parseMac(macStr: string): Uint8Array; /** Your MAC->key derivation (32 bytes) */ static calcChacha20Key(macStr: string): Uint8Array; /** 12-byte nonce from payload (little-endian uint32 x3) */ static getNonce({ validFrom, sessionId, validTo }: SessionIdPayload): Uint8Array; /** * ChaCha20 stream cipher (IETF): XOR plaintext with keystream. * Default counter=1 (common when reserving counter=0 for Poly1305 key derivation). */ static chacha20EncryptBytes(key32: Uint8Array, nonce12: Uint8Array, plaintext: Uint8Array, counter?: number): Uint8Array; /** Decryption is identical (XOR again) */ static chacha20DecryptBytes(key32: Uint8Array, nonce12: Uint8Array, ciphertext: Uint8Array, counter?: number): Uint8Array; /** Encrypt hex string (bytes in hex) -> ciphertext hex */ static chacha20EncryptHex(key32: Uint8Array, nonce12: Uint8Array, plaintextHex: string, counter?: number): string; /** Decrypt hex string (ciphertext hex) -> plaintext hex */ static chacha20DecryptHex(key32: Uint8Array, nonce12: Uint8Array, ciphertextHex: string, counter?: number): string; private readonly key32; constructor(macStr: string); encryptHexWithSession(payload: SessionIdPayload, plaintextHex: string, counter?: number): string; decryptHexWithSession(payload: SessionIdPayload, ciphertextHex: string, counter?: number): string; getKeyBytes(): Uint8Array; private static readonly SIGMA; private static chacha20Xor; /** Write 64-byte keystream block into `out64` */ private static chacha20Block; private static quarterRound; private static rotl32; private static u8ToU32LE; private static u32ToU8LE; static bytesToHex(bytes: Uint8Array): string; static hexToBytes(hex: string): Uint8Array; private static readonly HEX_LUT; private static hexNibble; static stringToHex(str: string): string; static hexToString(hex: string): string; } export {};