@heliomarpm/helpers
Version:
A library with many useful features
95 lines (94 loc) • 3.84 kB
TypeScript
export interface KeyPair {
publicKey: string;
privateKey: string;
}
export declare const Cryptor: {
/**
* Generates a SHA256 hash of the given text.
*
* @param text - The input text to hash.
* @returns A Promise that resolves to a hexadecimal string representing the SHA256 hash of the text.
*
* @example
* ```js
* const hashedPass = await Cryptor.hash('myPassword');
* console.log(hashedPass); // e.g., "9a0b...7f"
* ```
*/
hash(text: string): Promise<string>;
/**
* Compares a plain text string to a hashed string to determine if they are equivalent.
*
* @param text - The plaintext string to hash and compare.
* @param hash - The hash string to compare against the hashed text.
* @returns A Promise that resolves to `true` if the hashed text matches the given hash, `false` otherwise.
*
* @example
* ```js
* const isMatch = await Cryptor.compareHash("myPassword", hashedPass);
* console.log(isMatch); // Outputs true if 'example' hashes to 'hash', otherwise false
* ```
*/
compareHash(text: string, hash: string): Promise<boolean>;
/**
* Generates a cryptographically secure random salt value as a hexadecimal string.
*
* @param length - The length of the salt in bytes. Defaults to 16.
* @returns A Promise that resolves to a hexadecimal string representing the generated salt.
*
* @example
* ```js
* const salt = await Cryptor.generateSalt();
* console.log(salt); // Outputs a random hexadecimal string of length 16.
* ```
*/
generateSalt(length?: number): Promise<string>;
/**
* Generates a new 2048-bit RSA key pair and returns it as an object with `publicKey` and `privateKey` properties.
*
* @returns A Promise that resolves to an object with `publicKey` and `privateKey` properties, both as PEM-formatted strings.
*
* @example
* ```js
* const keyPair = await Cryptor.generateKeyPair();
* console.log(keyPair.publicKey); // Outputs the PEM-formatted public key
* console.log(keyPair.privateKey); // Outputs the PEM-formatted private key
* ```
*/
generateKeyPair(): Promise<KeyPair>;
/**
* Generates a digital signature for the given data using the private key.
*
* @param data - The data to sign.
* @param privateKey - The PEM-formatted private key to use for signing.
* @returns A Promise that resolves to the generated signature as a hexadecimal string.
*
* @example
* ```js
* const payload = JSON.stringify({id: 123, nome: "Heliomar", timestamp: Date.now()})
* const { publicKey, privateKey } = await Cryptor.generateSalt();
*
* const signature = await Cryptor.sign(payload, privateKey);
* console.log(Buffer.from(signature).toString("base64"));
*
* const isValid = await Cryptor.verify(payload, signature, publicKey);
* console.log(isValid); // Outputs true
* ```
*/
sign(data: string, privateKey: string): Promise<string>;
/**
* Verifies the digital signature of the given data using the public key.
*
* @param data - The data whose signature needs to be verified.
* @param signature - The hexadecimal string representing the signature to verify.
* @param publicKey - The PEM-formatted public key to use for verification.
* @returns A Promise that resolves to `true` if the signature is valid, `false` otherwise.
*
* @example
* ```js
* const isValid = await Cryptor.verify('Hello, world!', signature, publicKey);
* console.log(isValid); // Outputs true if the signature is valid, otherwise false
* ```
*/
verify(data: string, signature: string, publicKey: string): Promise<boolean>;
};