import { IEncryptedBlob, IEncryptedBlobJSON, IEncryptedBlobOptions } from '../types' import { bufferToString, isStringOrBuffer } from '../util/buffer' import { Buffer } from '../util/types' import { encrypt, decrypt, createSecret } from '../util/secretbox' import * as sodium from 'sodium-universal' import codecs, { Codec, CodecName, CodecOption, InType, OutType, SupportedCodec } from '@consento/codecs' import { exists, Inspectable } from '../util' import { InspectOptions } from 'inspect-custom-symbol' import prettyHash from 'pretty-hash' const { crypto_kdf_CONTEXTBYTES: CRYPTO_KDF_CONTEXTBYTES, crypto_kdf_BYTES_MAX: CRYPTO_KDF_BYTES_MAX, crypto_kdf_derive_from_key: kdfDeriveFromKey, sodium_malloc: malloc } = sodium.default const deriveContext = Buffer.from('conotify') if (deriveContext.length !== CRYPTO_KDF_CONTEXTBYTES) { throw new Error(`sodium context bytesize changed, we are in trouble! ${deriveContext.length} != ${CRYPTO_KDF_CONTEXTBYTES}`) } function deriveKdfKey (key: Uint8Array, index: number = 1): Uint8Array { const derivedKey = malloc(CRYPTO_KDF_BYTES_MAX) kdfDeriveFromKey(derivedKey, index, deriveContext, key) return derivedKey } export function isEncryptedBlob (input: any): input is IEncryptedBlob { if (typeof input !== 'object' || input === null) { return false } if (!(input.secretKey instanceof Uint8Array)) { return false } const { path } = input if (!Array.isArray(path)) { return false } for (const element of path) { if (typeof element !== 'string') { return false } } return input.size === null || input.size === undefined || typeof input.size === 'number' } export function isEncryptedBlobOfCodec (input: any, codec: TCodec): input is IEncryptedBlob> { if (!isEncryptedBlob(input)) { return false } return codecs(codec).name === input.codec } export function isEncryptedBlobJSON (input: IEncryptedBlobJSON | IEncryptedBlob): input is IEncryptedBlobJSON { return typeof input.secretKey === 'string' } export function isEncryptedBlobJSONOfCodec (input: IEncryptedBlobJSON | IEncryptedBlob>, codec: TCodec): input is IEncryptedBlobJSON> { return typeof input.secretKey === 'string' && codecs(codec).name === input.codec } export class EncryptedBlob extends Inspectable implements IEncryptedBlob> { _secretKey?: Uint8Array _secretKeyHex?: string _locationKey?: Uint8Array _locationKeyBase32?: string codec: Codec size?: number | undefined _path?: string[] constructor ({ secretKey, codec, size }: IEncryptedBlobOptions) { super() if (typeof secretKey === 'string') { this._secretKeyHex = secretKey } else { this._secretKey = secretKey } this.codec = codecs(codec, 'binary') this.size = size } get secretKey (): Uint8Array { if (this._secretKey === undefined) { this._secretKey = Buffer.from(this._secretKeyHex as unknown as string, 'hex') } return this._secretKey } get secretKeyHex (): string { if (this._secretKeyHex === undefined) { this._secretKeyHex = bufferToString(this._secretKey as unknown as Uint8Array, 'hex') } return this._secretKeyHex } get locationKey (): Uint8Array { if (this._locationKey === undefined) { this._locationKey = deriveKdfKey(this.secretKey).slice(0, 10) } return this._locationKey } get locationKeyBase32 (): string { if (this._locationKeyBase32 === undefined) { this._locationKeyBase32 = codecs['base32-c'].decode(this.locationKey).toLowerCase() } return this._locationKeyBase32 } get path (): string[] { if (this._path === undefined) { this._path = this.locationKeyBase32.split(/(.{4})/).filter(Boolean) } return this._path } decrypt (encrypted: Uint8Array): OutType { return this.codec.decode(decrypt(this.secretKey, encrypted)) } toJSON (): IEncryptedBlobJSON> { const result: IEncryptedBlobJSON> = { secretKey: this.secretKeyHex } if (exists(this.size)) { result.size = this.size } if (this.codec !== codecs.binary) { result.codec = this.codec.name } return result } _inspect (_: number, { stylize }: InspectOptions): string { // eslint-disable-next-line @typescript-eslint/restrict-template-expressions const size = typeof this.size === 'number' ? `,${stylize(this.size, 'number')}` : '' // eslint-disable-next-line @typescript-eslint/restrict-template-expressions return `EncryptedBlob(${stylize(this.codec.name)}|${stylize(prettyHash(this.locationKeyBase32), 'string')}${size})` } } export function toEncryptedBlob (input: string | Uint8Array): IEncryptedBlob export function toEncryptedBlob (input: string | Uint8Array, codec: TCodec): IEncryptedBlob> export function toEncryptedBlob > (input: TBlob): TBlob export function toEncryptedBlob (input: IEncryptedBlobJSON): IEncryptedBlob export function toEncryptedBlob (input: string | Uint8Array | IEncryptedBlob | IEncryptedBlobJSON, codec?: CodecOption): IEncryptedBlob { if (isStringOrBuffer(input)) { return new EncryptedBlob({ secretKey: input, codec }) } if (isEncryptedBlobJSON(input)) { return new EncryptedBlob(input as IEncryptedBlobJSON) } return input } export function encryptBlob (encodable: InType, codec?: TCodec): { blob: IEncryptedBlob> encrypted: Uint8Array } { const secretKey = createSecret() const codecObj = codecs(codec, 'binary') const encrypted = encrypt(secretKey, codecObj.encode(encodable)) return { blob: new EncryptedBlob({ secretKey, codec: codec as TCodec, size: encrypted.byteLength }), encrypted } }