UNPKG

protoobject

Version:

A universal class for creating any JSON objects and simple manipulations with them.

99 lines (98 loc) 2.95 kB
/** * ProtoObjectCrypto - Cryptographic utilities for ProtoObject * @description Provides encryption, hashing, and digital signature capabilities * @author Siarhei Dudko <siarhei@dudko.dev> */ import { ProtoObject } from "./proto-object.js"; import type { ProtoObjectDynamicMethods } from "../types/dynamic-methods.js"; /** * Encryption algorithms supported */ export declare enum EncryptionAlgorithm { AES256 = "aes-256-cbc", AES192 = "aes-192-cbc", AES128 = "aes-128-cbc" } /** * Hash algorithms supported */ export declare enum HashAlgorithm { SHA256 = "sha256", SHA512 = "sha512", MD5 = "md5" } /** * Encrypted data structure */ export interface EncryptedData { algorithm: EncryptionAlgorithm; data: string; iv: string; salt: string; } /** * Static methods interface for Crypto ProtoObject classes */ export interface ProtoObjectCryptoStaticMethods<T extends ProtoObjectDynamicMethods<T>> { new (data?: Partial<T>): T; fromJSON<U extends ProtoObjectDynamicMethods<U>>(data: { [key: string]: unknown; }): U; } /** * Base class for Crypto-enabled ProtoObjects */ export declare class ProtoObjectCrypto<T extends ProtoObjectDynamicMethods<T>> extends ProtoObject<T> { private _hash?; constructor(data?: Partial<T>); /** * Generate hash of the object * @param algorithm - Hash algorithm to use * @returns Hash string */ generateHash(algorithm?: HashAlgorithm): string; /** * Get stored hash * @returns Hash string or undefined */ getHash(): string | undefined; /** * Encrypt object data * @param password - Encryption password * @param algorithm - Encryption algorithm * @returns Encrypted data structure */ encrypt(password: string, algorithm?: EncryptionAlgorithm): EncryptedData; /** * Decrypt encrypted data * @param encryptedData - Encrypted data structure * @param password - Decryption password * @returns Decrypted object data */ static decrypt(encryptedData: EncryptedData, password: string): any; /** * Generate random salt for encryption * @param length - Salt length in bytes * @returns Hex string salt */ static generateSalt(length?: number): string; /** * Verify hash of the object * @param expectedHash - Expected hash value * @param algorithm - Hash algorithm used * @returns True if hash matches */ verifyHash(expectedHash: string, algorithm?: HashAlgorithm): boolean; /** * Create encrypted backup of the object * @param password - Encryption password * @param algorithm - Encryption algorithm * @returns Encrypted backup data */ createEncryptedBackup(password: string, algorithm?: EncryptionAlgorithm): { hash: string; encrypted: EncryptedData; timestamp: number; algorithm: EncryptionAlgorithm; }; }