mtcute/packages/core/src/utils/crypto/abstract.ts

60 lines
1.5 KiB
TypeScript
Raw Normal View History

import { MaybeAsync } from '../../types/index.js'
import { factorizePQSync } from './factorization.js'
2021-04-08 12:19:38 +03:00
export interface IEncryptionScheme {
2023-11-04 06:44:18 +03:00
encrypt(data: Uint8Array): Uint8Array
decrypt(data: Uint8Array): Uint8Array
}
2023-11-04 06:44:18 +03:00
export interface IAesCtr {
process(data: Uint8Array): Uint8Array
close?(): void
2021-04-08 12:19:38 +03:00
}
export interface ICryptoProvider {
initialize?(): MaybeAsync<void>
sha1(data: Uint8Array): Uint8Array
sha256(data: Uint8Array): Uint8Array
2021-04-08 12:19:38 +03:00
pbkdf2(
password: Uint8Array,
salt: Uint8Array,
iterations: number,
keylen?: number, // = 64
2023-09-24 01:32:22 +03:00
algo?: string, // sha1 or sha512 (default sha512)
): MaybeAsync<Uint8Array>
hmacSha256(data: Uint8Array, key: Uint8Array): MaybeAsync<Uint8Array>
2021-04-08 12:19:38 +03:00
2023-11-04 06:44:18 +03:00
createAesCtr(key: Uint8Array, iv: Uint8Array, encrypt: boolean): IAesCtr
2023-11-04 06:44:18 +03:00
createAesIge(key: Uint8Array, iv: Uint8Array): IEncryptionScheme
2021-04-08 12:19:38 +03:00
factorizePQ(pq: Uint8Array): MaybeAsync<[Uint8Array, Uint8Array]>
2023-11-04 06:44:18 +03:00
gzip(data: Uint8Array, maxSize: number): Uint8Array | null
gunzip(data: Uint8Array): Uint8Array
randomFill(buf: Uint8Array): void
randomBytes(size: number): Uint8Array
2021-04-08 12:19:38 +03:00
}
2022-08-29 16:22:57 +03:00
export abstract class BaseCryptoProvider {
abstract randomFill(buf: Uint8Array): void
factorizePQ(pq: Uint8Array) {
return factorizePQSync(this as unknown as ICryptoProvider, pq)
}
randomBytes(size: number) {
const buf = new Uint8Array(size)
this.randomFill(buf)
return buf
2021-04-08 12:19:38 +03:00
}
}
export type CryptoProviderFactory = () => ICryptoProvider